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>
passesSelf
by value, so cannot be an object safe receiver, because that's a contradiction. (You would then be passingCell<dyn Trait>
asSelf
.)
&[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 validT
, you probably want (the moral equivalent of)Cell<&'unsafe UnsafeCell<T>>
. (Note that the outerCell
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 fromNonNull
(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 useUnsafeCell
).
- Indirection would be given, by specifying a
T: DispatchFromDyn<U>
bound NonNull
already implementsDispatchFromDyn
- The reason to use a
Cell
is so one can use the containedNonNull
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)