From e7525cf6200e5b62a4b1a2f3131f68d946fb331e Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Thu, 26 Mar 2015 13:39:23 -0700 Subject: [PATCH] Revise use of conversion traits 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` 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. --- src/libcore/convert.rs | 16 +++++++++++ src/libstd/ffi/os_str.rs | 20 ++----------- src/libstd/path.rs | 48 +++++++------------------------- src/libstd/sys/unix/os_str.rs | 4 --- src/libstd/sys/windows/os_str.rs | 4 --- 5 files changed, 29 insertions(+), 63 deletions(-) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 65a226d37cbc..21f9b1f5513a 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -69,6 +69,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef for &'a mut T where T: AsRef { } } +// FIXME (#23442): replace the above impls for &/&mut with the following more general one: +// // As lifts over Deref +// impl AsRef for D where D::Target: AsRef { +// 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 { fn into(self) -> &'a mut U { @@ -83,6 +91,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut for &'a mut T where T: AsMut { } } +// FIXME (#23442): replace the above impl for &mut with the following more general one: +// // AsMut lifts over DerefMut +// impl AsMut for D where D::Target: AsMut { +// fn as_mut(&mut self) -> &mut U { +// self.deref_mut().as_mut() +// } +// } + // From implies Into impl Into for T where U: From { fn into(self) -> U { diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 5851c6e29980..24844ad09612 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -113,23 +113,9 @@ impl From 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> 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 50f79967f555..58d3ae9f7cfa 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1038,44 +1038,9 @@ 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() - } -} - -#[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)) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl From for PathBuf { - fn from(s: String) -> PathBuf { - PathBuf::from(OsString::from(s)) - } -} - -#[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()) +impl<'a, T: ?Sized + AsRef> From<&'a T> for PathBuf { + fn from(s: &'a T) -> PathBuf { + PathBuf::from(s.as_ref().to_os_string()) } } @@ -1086,6 +1051,13 @@ impl From for PathBuf { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl From for PathBuf { + fn from(s: String) -> PathBuf { + PathBuf::from(OsString::from(s)) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl> iter::FromIterator

for PathBuf { fn from_iter>(iter: I) -> PathBuf { diff --git a/src/libstd/sys/unix/os_str.rs b/src/libstd/sys/unix/os_str.rs index 3b8d18d87a0c..69d876a48a4b 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 ad1e6c4b0e72..91905ae7489d 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()) } }