about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2016-04-01 18:44:49 +0530
committerManish Goregaokar <manishsmail@gmail.com>2016-04-02 01:23:23 +0530
commitcf0a01a161ebbf01230f2e12b7ec8ce9544d4d5f (patch)
tree30a2090566bf99231359df78a2df4a96237fbd24 /src
parente004ce1a322739b21960154d6174e41cd9c9666b (diff)
parent33db2d65ffd5b988fdedc8ed91b395abaa57f777 (diff)
downloadrust-cf0a01a161ebbf01230f2e12b7ec8ce9544d4d5f.tar.gz
rust-cf0a01a161ebbf01230f2e12b7ec8ce9544d4d5f.zip
Rollup merge of #32652 - VFLashM:refcell_ref_coercion, r=alexcrichton
Added missing refcell ref/refmut coercions to unsized

Ref/RefMut should be coercible to unsized.
This commit adds a unit test and two missing CoerceUnsized implementations.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/cell.rs10
-rw-r--r--src/libcoretest/cell.rs20
2 files changed, 28 insertions, 2 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 4a9a1485063..b2cbc29b1c7 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -147,8 +147,8 @@
 use clone::Clone;
 use cmp::{PartialEq, Eq};
 use default::Default;
-use marker::{Copy, Send, Sync, Sized};
-use ops::{Deref, DerefMut, Drop, FnOnce};
+use marker::{Copy, Send, Sync, Sized, Unsize};
+use ops::{Deref, DerefMut, Drop, FnOnce, CoerceUnsized};
 use option::Option;
 use option::Option::{None, Some};
 
@@ -634,6 +634,9 @@ impl<'b, T: ?Sized> Ref<'b, T> {
     }
 }
 
+#[unstable(feature = "coerce_unsized", issue = "27732")]
+impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Ref<'b, U>> for Ref<'b, T> {}
+
 impl<'b, T: ?Sized> RefMut<'b, T> {
     /// Make a new `RefMut` for a component of the borrowed data, e.g. an enum
     /// variant.
@@ -766,6 +769,9 @@ impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
     }
 }
 
+#[unstable(feature = "coerce_unsized", issue = "27732")]
+impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RefMut<'b, U>> for RefMut<'b, T> {}
+
 /// The core primitive for interior mutability in Rust.
 ///
 /// `UnsafeCell<T>` is a type that wraps some `T` and indicates unsafe interior operations on the
diff --git a/src/libcoretest/cell.rs b/src/libcoretest/cell.rs
index cafffb5266f..c0b22274ee9 100644
--- a/src/libcoretest/cell.rs
+++ b/src/libcoretest/cell.rs
@@ -261,3 +261,23 @@ fn refcell_unsized() {
     let comp: &mut [i32] = &mut [4, 2, 5];
     assert_eq!(&*cell.borrow(), comp);
 }
+
+#[test]
+fn refcell_ref_coercion() {
+    let cell: RefCell<[i32; 3]> = RefCell::new([1, 2, 3]);
+    {
+        let mut cellref: RefMut<[i32; 3]> = cell.borrow_mut();
+        cellref[0] = 4;
+        let mut coerced: RefMut<[i32]> = cellref;
+        coerced[2] = 5;
+    }
+    {
+        let comp: &mut [i32] = &mut [4, 2, 5];
+        let cellref: Ref<[i32; 3]> = cell.borrow();
+        assert_eq!(&*cellref, comp);
+        let coerced: Ref<[i32]> = cellref;
+        assert_eq!(&*coerced, comp);
+    }
+}
+
+