A Box like type that keeps small objects on the stack and can allocate bigger objects as
Box, Rc or Arc. DerefMut is copy-on-write when the Smox is backed by Rc or Arc
Rationale
In generic contexts one often does not know how big the objects will be. If they are small enough they can be kept on the stack for better performance. If they are bigger they need to be allocated on the heap. Smox allows to do this in a transparent way.
Example
#
#
#
#
#
Limitations
Tand the underlyingHeapStoragebox type must implementCloneSmoxdoes not support dyn trait objectsSmoxdoes not support unsized types
Details
- In the current implementation, the given
SIZEis also the size of aSmoxor the size of a box types whatever is bigger. Current stable rust lacks thegeneric_const_exprssupport to constrain the size when heap storage is used to a single pointer. See below for nightly support. - The biggest spaces saving are when you can benefit from the copy-on-write behavior of
RcorArc. Use either of these when there is a chance that the objects will be cloned. - When your
Smoxshould stay as small as possible then try theMINIMAL_SIZEas size limit. Although this may not have the best performance, esp when it can not benefit from CoW.
Status
With v0.5.0 the the API stabilized for the current rust stable, eventually this will be
deprecated and superseeded once rust provides better support for const generics as shown in
the nightly features. The nightly support is still experimental and may change in future
versions.
Nightly Support
With the nightly feature enabled, Smox uses generic_const_exprs to optimize away unused
in-place storage. When T is larger than SIZE, then no space fo in-place storage is
reserved, storing on heap requires only a single pointer, when stored in_place it is
size_of::<T>().next_multiple_of(std::mem::size_of::<usize>()).
Note: Using Smox with generic type parameters in structs requires adding a where bound
[(); optimal_size::<T, SIZE>()]: to propagate the constraint. This is an inherent limitation
of generic_const_exprs.
#
#
#
#
#
#
Open Issues
The generic_const_exprs feature is far from complete. Eventually the rust team may specify
this functionaly in a completely other way. This crate will stabilize when the rust language
supports the required features in stable. The old stable implementation will then be abadoned
in favor of the new features. The pre-stabilization implementation will stay available in old
versions though.