Skip to content

Commit 43204ff

Browse files
authored
Auto merge of #35627 - apasel422:coerce-cell, r=alexcrichton
Implement `CoerceUnsized` for `{Cell, RefCell, UnsafeCell}` These impls are analogous to the one for `NonZero`. It's occasionally useful to be able to coerce the cell types when they're being used inside another abstraction. See Manishearth/rust-gc#17 for an example. r? @eddyb
2 parents 599f1b9 + 1fd791a commit 43204ff

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/cell.rs

+16
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ impl<T: Copy> From<T> for Cell<T> {
355355
}
356356
}
357357

358+
#[unstable(feature = "coerce_unsized", issue = "27732")]
359+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
360+
358361
/// A mutable memory location with dynamically checked borrow rules
359362
///
360363
/// See the [module-level documentation](index.html) for more.
@@ -793,6 +796,9 @@ impl<T> From<T> for RefCell<T> {
793796
}
794797
}
795798

799+
#[unstable(feature = "coerce_unsized", issue = "27732")]
800+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<RefCell<U>> for RefCell<T> {}
801+
796802
struct BorrowRef<'b> {
797803
borrow: &'b Cell<BorrowFlag>,
798804
}
@@ -1122,3 +1128,13 @@ impl<T> From<T> for UnsafeCell<T> {
11221128
UnsafeCell::new(t)
11231129
}
11241130
}
1131+
1132+
#[unstable(feature = "coerce_unsized", issue = "27732")]
1133+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
1134+
1135+
#[allow(unused)]
1136+
fn assert_coerce_unsized(a: UnsafeCell<&i32>, b: Cell<&i32>, c: RefCell<&i32>) {
1137+
let _: UnsafeCell<&Send> = a;
1138+
let _: Cell<&Send> = b;
1139+
let _: RefCell<&Send> = c;
1140+
}

0 commit comments

Comments
 (0)