once_cell icon indicating copy to clipboard operation
once_cell copied to clipboard

Could `OnceBox` support unsized types?

Open CraftSpider opened this issue 3 years ago • 1 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.

CraftSpider avatar Feb 07 '22 03:02 CraftSpider

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

a1phyr avatar Feb 11 '22 14:02 a1phyr

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!

matklad avatar Oct 22 '22 19:10 matklad