# Impl DispatchFromDyn for Cell #2

**URL:** <https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell-2/16520>\
**Category:** libs\
**Created:** [April 24, 2022, 3:15pm UTC](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell-2/16520 "2022-04-24T15:15:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimpolo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dimpolo/32/9320_2.png) [@dimpolo](https://internals.rust-lang.org/u/dimpolo)\
**Post date:** [April 24, 2022, 3:15pm UTC](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell-2/16520/1 "2022-04-24T15:15:27Z")

</div>

Previous thread:

> [@Impl DispatchFromDyn for Cell?](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell/14762):
>
> I wanted to add impl\<T: Trace + ?Sized + Unsize\<U\>, U: Trace + ?Sized\> DispatchFromDyn\<Gc\<U\>\> for Gc\<T\> {} to the gc crate, in order to allow object-safety of a method taking self: Gc\<Self\>, as is currently the case for self: Rc\<Self\>. But that doesn’t compile: error[E0277]: the trait bound `Cell\<NonNull\<GcBox\<T\>\>\>: DispatchFromDyn\<Cell\<NonNull\<GcBox\<U\>\>\>\>` is not satisfied --\> gc/src/lib.rs:56:1 | 56 | impl\<T: Trace + ?Sized + Unsize\<U\>, U: Trace + ?Sized\> DispatchFromDyn\<Gc\<U\>\> for Gc\<T…

I'd like to ask the same thing as OP. It seems like the previous comments don't fully address the problem.

So in short:

`Cell<NonNull<GcBox<T>>>` should be dispatchable.

The similar `CoerceUnsized` actually already has this impl:

```rust
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}

```

To address the last comment on the old thread:

> Dyn dispatch only works on an indirection to a trait object.
> 
> `Cell<Self>` passes `Self` by value, so cannot be an object safe receiver, because that's a contradiction. (You would then be passing `Cell<dyn Trait>` as `Self` .)
> 
> `&[mut] Cell<dyn Trait>` could at least _in theory_ perform dyn dispatch, but that's not what you're asking about.
> 
> `NonNull<T>` can't be an object safe receiver (currently) either, because it's currently _undecided_ if a dangling pointer still is required to have a valid vtable.
> 
> If `Gc<T>` is always guaranteed to at some point have pointed to some valid `T` , you probably want (the moral equivalent of) `Cell<&'unsafe UnsafeCell<T>>` . (Note that the outer `Cell` has _no impact_ on the mutability/aliasing guarantees of the pointee, just the pointer itself!) This doesn't currently exist in the language, but would guarantee all the guarantees provided by a reference, but with a user-enforced lifetime. The difference from `NonNull` (excepting variance) being guarantees of a) being aligned, b) valid pointer metadata describing a place that existed at some point, and c) accesses enforcing standard reference aliasing rules (so use `UnsafeCell` ).

- Indirection would be given, by specifying a `T: DispatchFromDyn<U>` bound
- `NonNull` already implements `DispatchFromDyn`
- The reason to use a `Cell` is so one can use the contained `NonNull` to store additional information that is exclusive to this reference ("reference is rooted" in OP's case, "reference is dangling" in something I'm working on)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 23, 2022, 3:15pm UTC](https://internals.rust-lang.org/t/impl-dispatchfromdyn-for-cell-2/16520/2 "2022-07-23T15:15:48Z")

</div>

Closed on July 23, 2022, 3:15pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
