impl<T> PointerLike for {Rc,Arc,Weak}<T>
can't exist but should
#134591
Labels
C-feature-request
Category: A feature request, i.e: not implemented / a PR.
F-dyn_star
`#![feature(dyn_star)]`
The unstable trait
PointerLike
must be implemented for any type which is to be coerced to adyn*
type. However, the only built-in smart pointer type it is currently implemented for isBox
, and this is a special case in the compiler.I believe it would be useful for
PointerLike
to be implemented for, among other things,Rc
,Arc
, and their correspondingWeak
types. This would enable use cases such as polymorphic collections likeVec<dyn* SomeTrait>
where each element consists of a (strong or weak) reference-counted pointer to other data structures, whereas the current available option isVec<Box<dyn SomeTrait>>
which results in an inefficient extra allocation and double-indirection for each item.However, it is currently impossible even to modify the standard library to meet this requirement. This is because the compiler requires that the type implementing
PointerLike
be either primitive orrepr(transparent)
, and types likeRc
cannot berepr(transparent)
because they have an allocator field:The only non-zero-sized field of
Rc<T>
isptr
, butRc<T, SomeNonZstAllocator>
is non-zero-sized, which disqualifies the entireRc
type from being able to haverepr(transparent)
, and therefore means you can't even implementPointerLike
forRc<T>
=Rc<T, Global>
.It seems to me that, therefore, it would be useful to adjust the design of
PointerLike
’s implementation restriction — or ofrepr(transparent)
, or something — so thatimpl PointerLike<T> for Rc<T>
is possible.Box
manages to implementPointerLike
only by way of having been granted a unique “isBox
” exception. This exception could be generalized to other standard library types, and that would address being able to useRc
and friends indyn*
, but it would privilege the standard library in a way that has no path to stabilization.(If
dyn*
settles into being solely an async trait implementation detail and not a language feature, then this entire issue is arguably moot since users won't be getting to writeVec<dyn* Trait>
at all — butPointerLike
would still be potentially useful as a trait bound to people trying to solve similar problems in libraries.)For an example of the kind of code I’m interested in writing:
This code does not compile, but the only reason it does not is that the
PointerLike
implementation is rejected. You can prove this by wrapping theWeak
in an extraBox
, and, it will compile and run on current nightly.@rustbot label +F-dyn_star
The text was updated successfully, but these errors were encountered: