diff options
| author | bors <bors@rust-lang.org> | 2015-02-26 07:01:18 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-02-26 07:01:18 +0000 |
| commit | 41f8b1e89b5ca0c79d7bca782ca44085624d4564 (patch) | |
| tree | 6ad85c8b272164c95caae35cf056b1369b704e5f /src/libstd | |
| parent | 610d1695d1e0f1bb4e59449d8ba70409b1dc610c (diff) | |
| parent | 2de7a7c9badf71978c53f07fc6b2db097a8d4213 (diff) | |
| download | rust-41f8b1e89b5ca0c79d7bca782ca44085624d4564.tar.gz rust-41f8b1e89b5ca0c79d7bca782ca44085624d4564.zip | |
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
Diffstat (limited to 'src/libstd')
| -rwxr-xr-x | src/libstd/path.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 88543ad85ed..b85a0dcec81 100755 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -108,7 +108,7 @@ use core::prelude::*; use ascii::*; -use borrow::{Borrow, ToOwned, Cow}; +use borrow::{Borrow, IntoCow, ToOwned, Cow}; use cmp; use iter::{self, IntoIterator}; use mem; @@ -987,6 +987,18 @@ impl Borrow<Path> for PathBuf { } } +impl IntoCow<'static, Path> for PathBuf { + fn into_cow(self) -> Cow<'static, Path> { + Cow::Owned(self) + } +} + +impl<'a> IntoCow<'a, Path> for &'a Path { + fn into_cow(self) -> Cow<'a, Path> { + Cow::Borrowed(self) + } +} + impl ToOwned for Path { type Owned = PathBuf; fn to_owned(&self) -> PathBuf { self.to_path_buf() } @@ -1412,6 +1424,26 @@ mod tests { ); #[test] + fn into_cow() { + use borrow::{Cow, IntoCow}; + + let static_path = Path::new("/home/foo"); + let static_cow_path: Cow<'static, Path> = static_path.into_cow(); + let pathbuf = PathBuf::new("/home/foo"); + + { + let path: &Path = &pathbuf; + let borrowed_cow_path: Cow<Path> = path.into_cow(); + + assert_eq!(static_cow_path, borrowed_cow_path); + } + + let owned_cow_path: Cow<'static, Path> = pathbuf.into_cow(); + + assert_eq!(static_cow_path, owned_cow_path); + } + + #[test] #[cfg(unix)] pub fn test_decompositions_unix() { t!("", |
