Skip to content

Commit cf0a01a

Browse files
committed
Rollup merge of rust-lang#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.
2 parents e004ce1 + 33db2d6 commit cf0a01a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/libcore/cell.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@
147147
use clone::Clone;
148148
use cmp::{PartialEq, Eq};
149149
use default::Default;
150-
use marker::{Copy, Send, Sync, Sized};
151-
use ops::{Deref, DerefMut, Drop, FnOnce};
150+
use marker::{Copy, Send, Sync, Sized, Unsize};
151+
use ops::{Deref, DerefMut, Drop, FnOnce, CoerceUnsized};
152152
use option::Option;
153153
use option::Option::{None, Some};
154154

@@ -634,6 +634,9 @@ impl<'b, T: ?Sized> Ref<'b, T> {
634634
}
635635
}
636636

637+
#[unstable(feature = "coerce_unsized", issue = "27732")]
638+
impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Ref<'b, U>> for Ref<'b, T> {}
639+
637640
impl<'b, T: ?Sized> RefMut<'b, T> {
638641
/// Make a new `RefMut` for a component of the borrowed data, e.g. an enum
639642
/// variant.
@@ -766,6 +769,9 @@ impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
766769
}
767770
}
768771

772+
#[unstable(feature = "coerce_unsized", issue = "27732")]
773+
impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RefMut<'b, U>> for RefMut<'b, T> {}
774+
769775
/// The core primitive for interior mutability in Rust.
770776
///
771777
/// `UnsafeCell<T>` is a type that wraps some `T` and indicates unsafe interior operations on the

src/libcoretest/cell.rs

+20
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,23 @@ fn refcell_unsized() {
261261
let comp: &mut [i32] = &mut [4, 2, 5];
262262
assert_eq!(&*cell.borrow(), comp);
263263
}
264+
265+
#[test]
266+
fn refcell_ref_coercion() {
267+
let cell: RefCell<[i32; 3]> = RefCell::new([1, 2, 3]);
268+
{
269+
let mut cellref: RefMut<[i32; 3]> = cell.borrow_mut();
270+
cellref[0] = 4;
271+
let mut coerced: RefMut<[i32]> = cellref;
272+
coerced[2] = 5;
273+
}
274+
{
275+
let comp: &mut [i32] = &mut [4, 2, 5];
276+
let cellref: Ref<[i32; 3]> = cell.borrow();
277+
assert_eq!(&*cellref, comp);
278+
let coerced: Ref<[i32]> = cellref;
279+
assert_eq!(&*coerced, comp);
280+
}
281+
}
282+
283+

0 commit comments

Comments
 (0)