Skip to content

Commit db76ac7

Browse files
committedOct 2, 2015
std: Add AsRef/AsMut impls to Box/Rc/Arc
These common traits were left off originally by accident from these smart pointers, and a past attempt (#26008) to add them was later reverted (#26160) due to unexpected breakge (#26096) occurring. The specific breakage in worry is the meaning of this return value changed: let a: Box<Option<T>> = ...; a.as_ref() Currently this returns `Option<&T>` but after this change it will return `&Option<T>` because the `AsRef::as_ref` method shares the same name as `Option::as_ref`. A [crater report][crater] of this change, however, has shown that the fallout of this change is quite minimal. These trait implementations are "the right impls to add" to these smart pointers and would enable various generalizations such as those in #27197. [crater]: https://gist.github.com/anonymous/0ba4c3512b07641c0f99 This commit is a breaking change for the above reasons mentioned, and the mitigation strategies look like any of: Option::as_ref(&a) a.as_ref().as_ref() (*a).as_ref()
1 parent d9d9ca1 commit db76ac7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎src/liballoc/arc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1148,3 +1148,8 @@ impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
11481148
&**self
11491149
}
11501150
}
1151+
1152+
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
1153+
impl<T: ?Sized> AsRef<T> for Arc<T> {
1154+
fn as_ref(&self) -> &T { &**self }
1155+
}

‎src/liballoc/boxed.rs

+10
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,13 @@ impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
594594
&mut **self
595595
}
596596
}
597+
598+
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
599+
impl<T: ?Sized> AsRef<T> for Box<T> {
600+
fn as_ref(&self) -> &T { &**self }
601+
}
602+
603+
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
604+
impl<T: ?Sized> AsMut<T> for Box<T> {
605+
fn as_mut(&mut self) -> &mut T { &mut **self }
606+
}

‎src/liballoc/rc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1117,3 +1117,8 @@ impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
11171117
&**self
11181118
}
11191119
}
1120+
1121+
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
1122+
impl<T: ?Sized> AsRef<T> for Rc<T> {
1123+
fn as_ref(&self) -> &T { &**self }
1124+
}

0 commit comments

Comments
 (0)