Skip to content

Commit 54a1576

Browse files
authored
Rollup merge of rust-lang#97373 - dimpolo:cell_dispatch_from_dyn, r=dtolnay
impl DispatchFromDyn for Cell and UnsafeCell After some fruitful discussion on [Internals](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell-2/16520) here's my first PR to rust-lang/rust 🎉 Please let me know if there's something I missed. This adds `DispatchFromDyn` impls for `Cell`, `UnsafeCell` and `SyncUnsafeCell`. An existing test is also expanded to test the `Cell` impl (which requires the `UnsafeCell` impl) The different `RefCell` types can not implement `DispatchFromDyn` since they have more than one (non ZST) field. &nbsp; **Edit:** ### What: These changes allow one to make types like `MyRc`(code below), to be object safe method receivers after implementing `DispatchFromDyn` and `Deref` for them. This allows for code like this: ```rust struct MyRc<T: ?Sized>(Cell<NonNull<RcBox<T>>>); /* impls for DispatchFromDyn, CoerceUnsized and Deref for MyRc*/ trait Trait { fn foo(self: MyRc<Self>); } let impls_trait = ...; let rc = MyRc::new(impls_trait) as MyRc<dyn Trait>; rc.foo(); ``` Note: `Cell` and `UnsafeCell` won't directly become valid method receivers since they don't implement `Deref`. Making use of these changes requires a wrapper type and nightly features. ### Why: A custom pointer type with interior mutability allows one to store extra information in the pointer itself. These changes allow for such a type to be a method receiver. ### Examples: My use case is a cycle aware custom `Rc` implementation that when dropping a cycle marks some references dangling. On the [forum](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell/14762/8) andersk mentioned that they track if a `Gc` reference is rooted with an extra bit in the reference itself.
2 parents 82bf372 + 193fe9a commit 54a1576

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

core/src/cell.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ use crate::cmp::Ordering;
196196
use crate::fmt::{self, Debug, Display};
197197
use crate::marker::{PhantomData, Unsize};
198198
use crate::mem;
199-
use crate::ops::{CoerceUnsized, Deref, DerefMut};
199+
use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn};
200200
use crate::ptr::{self, NonNull};
201201

202202
mod lazy;
@@ -571,6 +571,16 @@ impl<T: Default> Cell<T> {
571571
#[unstable(feature = "coerce_unsized", issue = "18598")]
572572
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
573573

574+
// Allow types that wrap `Cell` to also implement `DispatchFromDyn`
575+
// and become object safe method receivers.
576+
// Note that currently `Cell` itself cannot be a method receiver
577+
// because it does not implement Deref.
578+
// In other words:
579+
// `self: Cell<&Self>` won't work
580+
// `self: CellWrapper<Self>` becomes possible
581+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
582+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T> {}
583+
574584
impl<T> Cell<[T]> {
575585
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
576586
///
@@ -2078,6 +2088,16 @@ impl<T> const From<T> for UnsafeCell<T> {
20782088
#[unstable(feature = "coerce_unsized", issue = "18598")]
20792089
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
20802090

2091+
// Allow types that wrap `UnsafeCell` to also implement `DispatchFromDyn`
2092+
// and become object safe method receivers.
2093+
// Note that currently `UnsafeCell` itself cannot be a method receiver
2094+
// because it does not implement Deref.
2095+
// In other words:
2096+
// `self: UnsafeCell<&Self>` won't work
2097+
// `self: UnsafeCellWrapper<Self>` becomes possible
2098+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
2099+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T> {}
2100+
20812101
/// [`UnsafeCell`], but [`Sync`].
20822102
///
20832103
/// This is just an `UnsafeCell`, except it implements `Sync`
@@ -2169,6 +2189,17 @@ impl<T> const From<T> for SyncUnsafeCell<T> {
21692189
//#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
21702190
impl<T: CoerceUnsized<U>, U> CoerceUnsized<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}
21712191

2192+
// Allow types that wrap `SyncUnsafeCell` to also implement `DispatchFromDyn`
2193+
// and become object safe method receivers.
2194+
// Note that currently `SyncUnsafeCell` itself cannot be a method receiver
2195+
// because it does not implement Deref.
2196+
// In other words:
2197+
// `self: SyncUnsafeCell<&Self>` won't work
2198+
// `self: SyncUnsafeCellWrapper<Self>` becomes possible
2199+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
2200+
//#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
2201+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}
2202+
21722203
#[allow(unused)]
21732204
fn assert_coerce_unsized(
21742205
a: UnsafeCell<&i32>,

0 commit comments

Comments
 (0)