matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.84k stars 110 forks source link

Could `OnceBox` support unsized types? #171

Closed CraftSpider closed 1 year ago

CraftSpider commented 2 years ago

It seems like OnceBox should be able to support unsized types, as the get_or_init function expects the initializer to return a box, and stores the value behind a pointer. It would be nice to support unsized types here if there aren't any downsides to it.

a1phyr commented 2 years ago

The current implementation stores the Box as an AtomicPtr, so an unsized type is not possible (a pointer to an unsized value being two usize long).

As a workaround, you can add a level of boxing (eg Box<Box<dyn Trait>>).

matklad commented 1 year ago

Yeah, I don't think we can do this without double-word CAS. double-word CAS is available on many architectures, but it is still somewhat esoteric, so I'd perfer that to be a separate crate!