about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-10-08 00:46:01 +0000
committerbors <bors@rust-lang.org>2015-10-08 00:46:01 +0000
commite362679bb6a76064442492fdd5e07f06854f5605 (patch)
treede4668230f97a23bcc1ce7c31efcf02a5714f198 /src/liballoc
parente50298f7b53d4f51c936c0f61e282d6cb7ff2a19 (diff)
parentdb76ac73309332927d04e4f8bf5d235dc6f32360 (diff)
downloadrust-e362679bb6a76064442492fdd5e07f06854f5605.tar.gz
rust-e362679bb6a76064442492fdd5e07f06854f5605.zip
Auto merge of #28811 - alexcrichton:as-ref-ptrs, r=aturon
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()
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/arc.rs5
-rw-r--r--src/liballoc/boxed.rs10
-rw-r--r--src/liballoc/rc.rs5
3 files changed, 20 insertions, 0 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs
index ceca44fc1ac..f66f1f13dcb 100644
--- a/src/liballoc/arc.rs
+++ b/src/liballoc/arc.rs
@@ -1148,3 +1148,8 @@ impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
         &**self
     }
 }
+
+#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
+impl<T: ?Sized> AsRef<T> for Arc<T> {
+    fn as_ref(&self) -> &T { &**self }
+}
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 1529187da06..629adf4649d 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -594,3 +594,13 @@ impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
         &mut **self
     }
 }
+
+#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
+impl<T: ?Sized> AsRef<T> for Box<T> {
+    fn as_ref(&self) -> &T { &**self }
+}
+
+#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
+impl<T: ?Sized> AsMut<T> for Box<T> {
+    fn as_mut(&mut self) -> &mut T { &mut **self }
+}
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index 3507f123a6f..2f0bf1281b3 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -1117,3 +1117,8 @@ impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
         &**self
     }
 }
+
+#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
+impl<T: ?Sized> AsRef<T> for Rc<T> {
+    fn as_ref(&self) -> &T { &**self }
+}