matklad / once_cell

Rust library for single assignment cells and lazy statics without macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Could `OnceBox` support unsized types?

CraftSpider opened this issue · comments

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.

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>>).

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!