Skip to content

Commit 7690ec8

Browse files
committed
libstd: implement From<&Path|PathBuf> for Cow<Path>
1 parent eacd359 commit 7690ec8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/libstd/path.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,22 @@ impl<'a> IntoCow<'a, Path> for &'a Path {
11661166
}
11671167
}
11681168

1169+
#[stable(feature = "cow_from_path", since = "1.6.0")]
1170+
impl<'a> From<&'a Path> for Cow<'a, Path> {
1171+
#[inline]
1172+
fn from(s: &'a Path) -> Cow<'a, Path> {
1173+
Cow::Borrowed(s)
1174+
}
1175+
}
1176+
1177+
#[stable(feature = "cow_from_path", since = "1.6.0")]
1178+
impl<'a> From<PathBuf> for Cow<'a, Path> {
1179+
#[inline]
1180+
fn from(s: PathBuf) -> Cow<'a, Path> {
1181+
Cow::Owned(s)
1182+
}
1183+
}
1184+
11691185
#[stable(feature = "rust1", since = "1.0.0")]
11701186
impl ToOwned for Path {
11711187
type Owned = PathBuf;
@@ -2002,6 +2018,26 @@ mod tests {
20022018
assert_eq!(static_cow_path, owned_cow_path);
20032019
}
20042020

2021+
#[test]
2022+
fn into() {
2023+
use borrow::Cow;
2024+
2025+
let static_path = Path::new("/home/foo");
2026+
let static_cow_path: Cow<'static, Path> = static_path.into();
2027+
let pathbuf = PathBuf::from("/home/foo");
2028+
2029+
{
2030+
let path: &Path = &pathbuf;
2031+
let borrowed_cow_path: Cow<Path> = path.into();
2032+
2033+
assert_eq!(static_cow_path, borrowed_cow_path);
2034+
}
2035+
2036+
let owned_cow_path: Cow<'static, Path> = pathbuf.into();
2037+
2038+
assert_eq!(static_cow_path, owned_cow_path);
2039+
}
2040+
20052041
#[test]
20062042
#[cfg(unix)]
20072043
pub fn test_decompositions_unix() {

0 commit comments

Comments
 (0)