Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned<T: ?Sized> #3

Open
stijnfrishert opened this issue Feb 1, 2022 · 2 comments · May be fixed by #8
Open

Owned<T: ?Sized> #3

stijnfrishert opened this issue Feb 1, 2022 · 2 comments · May be fixed by #8

Comments

@stijnfrishert
Copy link

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.

@glowcoil
Copy link
Owner

glowcoil commented Feb 1, 2022

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.

@stijnfrishert
Copy link
Author

The ability to convert a Box to a Box is builtin compiler functionality

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants