Skip to content

Commit

Permalink
Auto merge of #35627 - apasel422:coerce-cell, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors authored Aug 23, 2016
2 parents 599f1b9 + 1fd791a commit 43204ff
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ impl<T: Copy> From<T> for Cell<T> {
}
}

#[unstable(feature = "coerce_unsized", issue = "27732")]
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}

/// A mutable memory location with dynamically checked borrow rules
///
/// See the [module-level documentation](index.html) for more.
Expand Down Expand Up @@ -793,6 +796,9 @@ impl<T> From<T> for RefCell<T> {
}
}

#[unstable(feature = "coerce_unsized", issue = "27732")]
impl<T: CoerceUnsized<U>, U> CoerceUnsized<RefCell<U>> for RefCell<T> {}

struct BorrowRef<'b> {
borrow: &'b Cell<BorrowFlag>,
}
Expand Down Expand Up @@ -1122,3 +1128,13 @@ impl<T> From<T> for UnsafeCell<T> {
UnsafeCell::new(t)
}
}

#[unstable(feature = "coerce_unsized", issue = "27732")]
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}

#[allow(unused)]
fn assert_coerce_unsized(a: UnsafeCell<&i32>, b: Cell<&i32>, c: RefCell<&i32>) {
let _: UnsafeCell<&Send> = a;
let _: Cell<&Send> = b;
let _: RefCell<&Send> = c;
}

0 comments on commit 43204ff

Please sign in to comment.