Impl DispatchFromDyn for Cell #2

Previous thread:

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:

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)
1 Like

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