Skip to content

Commit

Permalink
Make AsRef and AsMut reflexive (BREAKING).
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed Jan 29, 2017
1 parent 4be49e1 commit 4a9884d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 61 deletions.
14 changes: 0 additions & 14 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,20 +1818,6 @@ impl<T: fmt::Debug> fmt::Debug for Vec<T> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<Vec<T>> for Vec<T> {
fn as_ref(&self) -> &Vec<T> {
self
}
}

#[stable(feature = "vec_as_mut", since = "1.5.0")]
impl<T> AsMut<Vec<T>> for Vec<T> {
fn as_mut(&mut self) -> &mut Vec<T> {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for Vec<T> {
fn as_ref(&self) -> &[T] {
Expand Down
42 changes: 16 additions & 26 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ pub trait TryFrom<T>: Sized {
// GENERIC IMPLS
////////////////////////////////////////////////////////////////////////////////

// As is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized> AsRef<T> for T {
fn as_ref(&self) -> &T {
self
}
}

// As lifts over &
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
Expand All @@ -248,6 +256,14 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
// }
// }

// AsMut is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized> AsMut<T> for T {
fn as_mut(&mut self) -> &mut T {
self
}
}

// AsMut lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
Expand Down Expand Up @@ -288,29 +304,3 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T> {
U::try_from(self)
}
}

////////////////////////////////////////////////////////////////////////////////
// CONCRETE IMPLS
////////////////////////////////////////////////////////////////////////////////

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T] {
fn as_ref(&self) -> &[T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsMut<[T]> for [T] {
fn as_mut(&mut self) -> &mut [T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<str> for str {
#[inline]
fn as_ref(&self) -> &str {
self
}
}
7 changes: 0 additions & 7 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,6 @@ impl ops::Index<ops::RangeFull> for CString {
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl AsRef<CStr> for CStr {
fn as_ref(&self) -> &CStr {
self
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl AsRef<CStr> for CString {
fn as_ref(&self) -> &CStr {
Expand Down
7 changes: 0 additions & 7 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,6 @@ impl ToOwned for OsStr {
fn to_owned(&self) -> OsString { self.to_os_string() }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsStr {
fn as_ref(&self) -> &OsStr {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for OsString {
fn as_ref(&self) -> &OsStr {
Expand Down
7 changes: 0 additions & 7 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,13 +2129,6 @@ impl cmp::Ord for Path {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<Path> for Path {
fn as_ref(&self) -> &Path {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<Path> for OsStr {
fn as_ref(&self) -> &Path {
Expand Down

0 comments on commit 4a9884d

Please sign in to comment.