Skip to content

Commit 41f8b1e

Browse files
committed
Auto merge of #22810 - japaric:cow-path, r=alexcrichton
The Path/PathBuf pair already implements the required `Borrow`/`ToOwned` traits and can be used in a `Cow` pointer, so why not? r? @alexcrichton
2 parents 610d169 + 2de7a7c commit 41f8b1e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Diff for: src/libstd/path.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
use core::prelude::*;
109109

110110
use ascii::*;
111-
use borrow::{Borrow, ToOwned, Cow};
111+
use borrow::{Borrow, IntoCow, ToOwned, Cow};
112112
use cmp;
113113
use iter::{self, IntoIterator};
114114
use mem;
@@ -987,6 +987,18 @@ impl Borrow<Path> for PathBuf {
987987
}
988988
}
989989

990+
impl IntoCow<'static, Path> for PathBuf {
991+
fn into_cow(self) -> Cow<'static, Path> {
992+
Cow::Owned(self)
993+
}
994+
}
995+
996+
impl<'a> IntoCow<'a, Path> for &'a Path {
997+
fn into_cow(self) -> Cow<'a, Path> {
998+
Cow::Borrowed(self)
999+
}
1000+
}
1001+
9901002
impl ToOwned for Path {
9911003
type Owned = PathBuf;
9921004
fn to_owned(&self) -> PathBuf { self.to_path_buf() }
@@ -1411,6 +1423,26 @@ mod tests {
14111423
);
14121424
);
14131425

1426+
#[test]
1427+
fn into_cow() {
1428+
use borrow::{Cow, IntoCow};
1429+
1430+
let static_path = Path::new("/home/foo");
1431+
let static_cow_path: Cow<'static, Path> = static_path.into_cow();
1432+
let pathbuf = PathBuf::new("/home/foo");
1433+
1434+
{
1435+
let path: &Path = &pathbuf;
1436+
let borrowed_cow_path: Cow<Path> = path.into_cow();
1437+
1438+
assert_eq!(static_cow_path, borrowed_cow_path);
1439+
}
1440+
1441+
let owned_cow_path: Cow<'static, Path> = pathbuf.into_cow();
1442+
1443+
assert_eq!(static_cow_path, owned_cow_path);
1444+
}
1445+
14141446
#[test]
14151447
#[cfg(unix)]
14161448
pub fn test_decompositions_unix() {

0 commit comments

Comments
 (0)