basedrop icon indicating copy to clipboard operation
basedrop copied to clipboard

Owned<T: ?Sized>

Open stijnfrishert opened this issue 3 years ago • 2 comments

Owned is the garbage collected equivalent of Box, yet at the same time T has to be Sized, while it doesn't have to be for Box. Sames goes for Shared and Arc.

Is this on purpose? Can we relax this trait bound?

I tried sending a Owned<dyn FnMut()> over to my audio thread, but couldn't. :)

Happy to submit a PR for this, if necessary.

stijnfrishert avatar Feb 01 '22 13:02 stijnfrishert

I would like for basedrop to support this, but it's not as simple as relaxing the trait bound. Owned::new needs to take a Sized value since Box::new does as well, and then there needs to be a way to convert e.g. an Owned<T> to an Owned<dyn Trait>. The ability to convert a Box<T> to a Box<dyn Trait> is builtin compiler functionality, and there is an RFC to allow custom smart pointer types to make use of it but it has not be stabilized. It might be possible to hand-roll an acceptable solution that works on stable, where converting to an unsized type requires an explicit method call (and there's the unsize crate which provides a pattern for doing so) but I haven't gotten the chance to really look into it yet.

You can work around this limitation for now by using an Owned<Box<dyn FnMut()>>. It's certainly not ideal, but it will work.

glowcoil avatar Feb 01 '22 15:02 glowcoil

The ability to convert a Box<T> to a Box<dyn Trait> is builtin compiler functionality

Ah, this explains a lot. I guess I'll work with an Owned<Box<T>> for now :)

stijnfrishert avatar Feb 01 '22 15:02 stijnfrishert