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

Cannot put sized data into a StackBox / SmallBox with unsize feature #7

Closed
RReverser opened this issue Sep 28, 2018 · 2 comments
Closed

Comments

@RReverser
Copy link

Difference in definitions of new depending on a feature flag makes it impossible to create a simple sized box when needed. For example, the following code:

struct S { data: usize, }
let m = StackBox::<S, S1>::new(S { data: 10 }).unwrap();

compiles fine with default features, but fails with unsize with

the trait std::marker::Unsize<stackbox::tests::test_as_ptr::S> is not implemented for stackbox::tests::test_as_ptr::S

I'd suggest exposing same basic interface for sized values via new and create a separate feature-controlled new_unsized for when it's needed, so that compiler can distinguish and correctly resolve types in both cases.

@andylokandy
Copy link
Owner

andylokandy commented Sep 28, 2018

Thanks for bringing this up!

This is one of the limitations of smallbox now. You can't have a sized SmallBox with unsize feature flag on. The only formal solution is using CoerceUnsize, which can automatically coerce a sized StackBox into unsized one, or just keep it sized if coercion is not required. There is a problem on SmallBox that CoerceUnsize can't be implement for an enum. I also have a solution for it: Turn SmallBox into a struct and then track the tag information by ourselves. But it has a trade off that we have to define our own Box type to control the heap pointer, which means we would not be able to get a standard Box<T> out of it again.

I'd suggest exposing same basic interface for sized values via new and create a separate feature-controlled new_unsized for when it's needed, so that compiler can distinguish and correctly resolve types in both cases.

It definitely is a solution for the problem, I'll take it into consideration.

@andylokandy
Copy link
Owner

andylokandy commented Sep 30, 2018

CoerceUnsize is implemented for SmallBox and the new method only requires T: Sized now, so you can have either a sized SmallBox or an unsized SmallBox coerced from the sized one.

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

No branches or pull requests

2 participants