diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-27 10:07:49 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-27 10:07:49 -0700 |
| commit | 7d79a4facd6cd26ff06d99ef4bbb2b35003de28e (patch) | |
| tree | 7de48d79407b29b20376345a1f8c9623449b2ab3 /src | |
| parent | 55c398d651e6b3e4f469325a35e029059ce36f1e (diff) | |
| parent | e7525cf6200e5b62a4b1a2f3131f68d946fb331e (diff) | |
| download | rust-7d79a4facd6cd26ff06d99ef4bbb2b35003de28e.tar.gz rust-7d79a4facd6cd26ff06d99ef4bbb2b35003de28e.zip | |
rollup merge of #23753: aturon/revise-convert
This commit revises `path` and `os_str` to use blanket impls for `From` on reference types. This both cuts down on the number of required impls, and means that you can pass through e.g. `T: AsRef<OsStr>` to `PathBuf::from` without an intermediate call to `as_ref`. It also makes a FIXME note for later generalizing the blanket impls for `AsRef` and `AsMut` to use `Deref`/`DerefMut`, once it is possible to do so.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/convert.rs | 16 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 20 | ||||
| -rw-r--r-- | src/libstd/path.rs | 40 | ||||
| -rw-r--r-- | src/libstd/sys/unix/os_str.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/windows/os_str.rs | 4 |
5 files changed, 25 insertions, 59 deletions
diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 65a226d37cb..21f9b1f5513 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -69,6 +69,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> { } } +// FIXME (#23442): replace the above impls for &/&mut with the following more general one: +// // As lifts over Deref +// impl<D: ?Sized + Deref, U: ?Sized> AsRef<U> for D where D::Target: AsRef<U> { +// fn as_ref(&self) -> &U { +// self.deref().as_ref() +// } +// } + // AsMut implies Into impl<'a, T: ?Sized, U: ?Sized> Into<&'a mut U> for &'a mut T where T: AsMut<U> { fn into(self) -> &'a mut U { @@ -83,6 +91,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> { } } +// FIXME (#23442): replace the above impl for &mut with the following more general one: +// // AsMut lifts over DerefMut +// impl<D: ?Sized + Deref, U: ?Sized> AsMut<U> for D where D::Target: AsMut<U> { +// fn as_mut(&mut self) -> &mut U { +// self.deref_mut().as_mut() +// } +// } + // From implies Into impl<T, U> Into<U> for T where U: From<T> { fn into(self) -> U { diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 5851c6e2998..24844ad0961 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -113,23 +113,9 @@ impl From<String> for OsString { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a String> for OsString { - fn from(s: &'a String) -> OsString { - OsString { inner: Buf::from_str(s) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a str> for OsString { - fn from(s: &'a str) -> OsString { - OsString { inner: Buf::from_str(s) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a OsStr> for OsString { - fn from(s: &'a OsStr) -> OsString { - OsString { inner: s.inner.to_owned() } +impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for OsString { + fn from(s: &'a T) -> OsString { + s.as_ref().to_os_string() } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 50f79967f55..58d3ae9f7cf 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1038,23 +1038,16 @@ impl PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a Path> for PathBuf { - fn from(s: &'a Path) -> PathBuf { - s.to_path_buf() +impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for PathBuf { + fn from(s: &'a T) -> PathBuf { + PathBuf::from(s.as_ref().to_os_string()) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a str> for PathBuf { - fn from(s: &'a str) -> PathBuf { - PathBuf::from(OsString::from(s)) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a String> for PathBuf { - fn from(s: &'a String) -> PathBuf { - PathBuf::from(OsString::from(s)) +impl From<OsString> for PathBuf { + fn from(s: OsString) -> PathBuf { + PathBuf { inner: s } } } @@ -1066,27 +1059,6 @@ impl From<String> for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a OsStr> for PathBuf { - fn from(s: &'a OsStr) -> PathBuf { - PathBuf::from(OsString::from(s)) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a OsString> for PathBuf { - fn from(s: &'a OsString) -> PathBuf { - PathBuf::from(s.to_os_string()) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl From<OsString> for PathBuf { - fn from(s: OsString) -> PathBuf { - PathBuf { inner: s } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf { fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf { let mut buf = PathBuf::new(); diff --git a/src/libstd/sys/unix/os_str.rs b/src/libstd/sys/unix/os_str.rs index 3b8d18d87a0..69d876a48a4 100644 --- a/src/libstd/sys/unix/os_str.rs +++ b/src/libstd/sys/unix/os_str.rs @@ -46,10 +46,6 @@ impl Buf { Buf { inner: s.into_bytes() } } - pub fn from_str(s: &str) -> Buf { - Buf { inner: s.as_bytes().to_vec() } - } - pub fn as_slice(&self) -> &Slice { unsafe { mem::transmute(&*self.inner) } } diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs index ad1e6c4b0e7..91905ae7489 100644 --- a/src/libstd/sys/windows/os_str.rs +++ b/src/libstd/sys/windows/os_str.rs @@ -45,10 +45,6 @@ impl Buf { Buf { inner: Wtf8Buf::from_string(s) } } - pub fn from_str(s: &str) -> Buf { - Buf { inner: Wtf8Buf::from_str(s) } - } - pub fn as_slice(&self) -> &Slice { unsafe { mem::transmute(self.inner.as_slice()) } } |
