Skip to content

Commit 7d79a4f

Browse files
committed
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.
2 parents 55c398d + e7525cf commit 7d79a4f

File tree

5 files changed

+25
-59
lines changed

5 files changed

+25
-59
lines changed

src/libcore/convert.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
6969
}
7070
}
7171

72+
// FIXME (#23442): replace the above impls for &/&mut with the following more general one:
73+
// // As lifts over Deref
74+
// impl<D: ?Sized + Deref, U: ?Sized> AsRef<U> for D where D::Target: AsRef<U> {
75+
// fn as_ref(&self) -> &U {
76+
// self.deref().as_ref()
77+
// }
78+
// }
79+
7280
// AsMut implies Into
7381
impl<'a, T: ?Sized, U: ?Sized> Into<&'a mut U> for &'a mut T where T: AsMut<U> {
7482
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> {
8391
}
8492
}
8593

94+
// FIXME (#23442): replace the above impl for &mut with the following more general one:
95+
// // AsMut lifts over DerefMut
96+
// impl<D: ?Sized + Deref, U: ?Sized> AsMut<U> for D where D::Target: AsMut<U> {
97+
// fn as_mut(&mut self) -> &mut U {
98+
// self.deref_mut().as_mut()
99+
// }
100+
// }
101+
86102
// From implies Into
87103
impl<T, U> Into<U> for T where U: From<T> {
88104
fn into(self) -> U {

src/libstd/ffi/os_str.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,9 @@ impl From<String> for OsString {
113113
}
114114

115115
#[stable(feature = "rust1", since = "1.0.0")]
116-
impl<'a> From<&'a String> for OsString {
117-
fn from(s: &'a String) -> OsString {
118-
OsString { inner: Buf::from_str(s) }
119-
}
120-
}
121-
122-
#[stable(feature = "rust1", since = "1.0.0")]
123-
impl<'a> From<&'a str> for OsString {
124-
fn from(s: &'a str) -> OsString {
125-
OsString { inner: Buf::from_str(s) }
126-
}
127-
}
128-
129-
#[stable(feature = "rust1", since = "1.0.0")]
130-
impl<'a> From<&'a OsStr> for OsString {
131-
fn from(s: &'a OsStr) -> OsString {
132-
OsString { inner: s.inner.to_owned() }
116+
impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for OsString {
117+
fn from(s: &'a T) -> OsString {
118+
s.as_ref().to_os_string()
133119
}
134120
}
135121

src/libstd/path.rs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,23 +1038,16 @@ impl PathBuf {
10381038
}
10391039

10401040
#[stable(feature = "rust1", since = "1.0.0")]
1041-
impl<'a> From<&'a Path> for PathBuf {
1042-
fn from(s: &'a Path) -> PathBuf {
1043-
s.to_path_buf()
1041+
impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for PathBuf {
1042+
fn from(s: &'a T) -> PathBuf {
1043+
PathBuf::from(s.as_ref().to_os_string())
10441044
}
10451045
}
10461046

10471047
#[stable(feature = "rust1", since = "1.0.0")]
1048-
impl<'a> From<&'a str> for PathBuf {
1049-
fn from(s: &'a str) -> PathBuf {
1050-
PathBuf::from(OsString::from(s))
1051-
}
1052-
}
1053-
1054-
#[stable(feature = "rust1", since = "1.0.0")]
1055-
impl<'a> From<&'a String> for PathBuf {
1056-
fn from(s: &'a String) -> PathBuf {
1057-
PathBuf::from(OsString::from(s))
1048+
impl From<OsString> for PathBuf {
1049+
fn from(s: OsString) -> PathBuf {
1050+
PathBuf { inner: s }
10581051
}
10591052
}
10601053

@@ -1065,27 +1058,6 @@ impl From<String> for PathBuf {
10651058
}
10661059
}
10671060

1068-
#[stable(feature = "rust1", since = "1.0.0")]
1069-
impl<'a> From<&'a OsStr> for PathBuf {
1070-
fn from(s: &'a OsStr) -> PathBuf {
1071-
PathBuf::from(OsString::from(s))
1072-
}
1073-
}
1074-
1075-
#[stable(feature = "rust1", since = "1.0.0")]
1076-
impl<'a> From<&'a OsString> for PathBuf {
1077-
fn from(s: &'a OsString) -> PathBuf {
1078-
PathBuf::from(s.to_os_string())
1079-
}
1080-
}
1081-
1082-
#[stable(feature = "rust1", since = "1.0.0")]
1083-
impl From<OsString> for PathBuf {
1084-
fn from(s: OsString) -> PathBuf {
1085-
PathBuf { inner: s }
1086-
}
1087-
}
1088-
10891061
#[stable(feature = "rust1", since = "1.0.0")]
10901062
impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf {
10911063
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {

src/libstd/sys/unix/os_str.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ impl Buf {
4646
Buf { inner: s.into_bytes() }
4747
}
4848

49-
pub fn from_str(s: &str) -> Buf {
50-
Buf { inner: s.as_bytes().to_vec() }
51-
}
52-
5349
pub fn as_slice(&self) -> &Slice {
5450
unsafe { mem::transmute(&*self.inner) }
5551
}

src/libstd/sys/windows/os_str.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ impl Buf {
4545
Buf { inner: Wtf8Buf::from_string(s) }
4646
}
4747

48-
pub fn from_str(s: &str) -> Buf {
49-
Buf { inner: Wtf8Buf::from_str(s) }
50-
}
51-
5248
pub fn as_slice(&self) -> &Slice {
5349
unsafe { mem::transmute(self.inner.as_slice()) }
5450
}

0 commit comments

Comments
 (0)