Add try_into_boxed
(and potentially into_boxed_inner
) for Rc<T>
/Arc<T>
where T: ?Sized
#269
Labels
try_into_boxed
(and potentially into_boxed_inner
) for Rc<T>
/Arc<T>
where T: ?Sized
#269
Proposal
Problem statement
Currently there is no stable way to convert
Rc/Arc<dyn Trait>
toBox<dyn Trait>
even thoughBox -> Rc/Arc
is covered by aFrom
implementation, and bothinto_inner
/try_unwrap
are there as well. Even a manual nightly solution has a lot of soundness footguns.Motivating examples or use cases
A lot of UI-oriented API's use
Arc
s for widget trees (an example being druid). They might be able to benefit from having object-safe methods takingBox<Self>
during tree updates. Any pattern where bulk multithreaded operations happen onArc
s happen to have synchronous execution inbetween should be a good fit for conversions of this sort.Even when
T: Sized
, an extra cost of moving the value is associated with unwrapping then boxing, which can be a detriment for big data structures (the case with arrays can usually be solved by cloning to Vec before resorting to TryFrom).Solution sketch
I propose adding a private function
mirroring pointer API, and then closely following implementation of
try_unwrap
, with the replacement ofptr::read
byBox::read_unsized
.Alternatives
There is a crate called rc-box made by CAD97 with over 100'000 downloads. Overall it's a better solution for modifying uniquely owned
Arc
without resorting to callingget_mut
each time (+more typesafe), and, unlikeArc -> Box
, it avoids reallocation. However, it's not object safe, and it can only produce semantics ofinto_boxed_inner
(otherwiseDerefMut
would be unsound).Implementing the conversion manually has to resort to a nightly feature
ptr_metadata
and is very dangerous just forinto_inner_boxed
.Links and related work
An old URLO thread
The text was updated successfully, but these errors were encountered: