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.
This commit is contained in:
commit
cf0a01a161
|
@ -147,8 +147,8 @@
|
||||||
use clone::Clone;
|
use clone::Clone;
|
||||||
use cmp::{PartialEq, Eq};
|
use cmp::{PartialEq, Eq};
|
||||||
use default::Default;
|
use default::Default;
|
||||||
use marker::{Copy, Send, Sync, Sized};
|
use marker::{Copy, Send, Sync, Sized, Unsize};
|
||||||
use ops::{Deref, DerefMut, Drop, FnOnce};
|
use ops::{Deref, DerefMut, Drop, FnOnce, CoerceUnsized};
|
||||||
use option::Option;
|
use option::Option;
|
||||||
use option::Option::{None, Some};
|
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> {
|
impl<'b, T: ?Sized> RefMut<'b, T> {
|
||||||
/// Make a new `RefMut` for a component of the borrowed data, e.g. an enum
|
/// Make a new `RefMut` for a component of the borrowed data, e.g. an enum
|
||||||
/// variant.
|
/// 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.
|
/// The core primitive for interior mutability in Rust.
|
||||||
///
|
///
|
||||||
/// `UnsafeCell<T>` is a type that wraps some `T` and indicates unsafe interior operations on the
|
/// `UnsafeCell<T>` is a type that wraps some `T` and indicates unsafe interior operations on the
|
||||||
|
|
|
@ -261,3 +261,23 @@ fn refcell_unsized() {
|
||||||
let comp: &mut [i32] = &mut [4, 2, 5];
|
let comp: &mut [i32] = &mut [4, 2, 5];
|
||||||
assert_eq!(&*cell.borrow(), comp);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue