Expand description
Defines slimmer versions of slices, both shared, mutable, and owned.
They are slimmer in the sense that whereas e.g.,
size_of::<Box<[T]>>() == 16, on a 64-bit machine,
a SlimSliceBox<T> only takes up 12 bytes.
These 4 bytes in difference can help
when these types are stored in enum variants
due to alignment and the space needed for storing tags.
The difference size (4 bytes), is due to storing the length as a u32
rather than storing the length as a usize (u64 on 64-bit machine).
This implies that the length can be at most u32::MAX,
so no more elements than that can be stored or pointed to with these types.
Because hitting u32::MAX is substantially more likely than u64::MAX,
the risk of overflow is greater.
To mitigate this issue, rather than default to panicking,
this module tries, for the most part,
to force its user to handle any overflow
when converting to the slimmer types.
The slimmer types include:
SlimSliceBox<T>, a slimmer version ofBox<[T]>SlimSmallSliceBox<T, N>, a slimmer version ofSmallVec<[T; N]>but without the growing functionality.SlimStrBox, a slimmer version ofBox<str>SlimSlice<'a, T>, a slimmer version of&'a [T]SlimSliceMut<'a, T>, a slimmer version of&'a mut [T]SlimStr<'a>, a slimmer version of&'a strSlimStrMut<'a>, a slimmer version of&'a mut str
The following convenience conversion functions are provided:
from_sliceconverts&[T] -> SlimSlice<T>, panicking on overflowfrom_slice_mutconverts&mut [T] -> SlimSliceMut<T>, panicking on overflowfrom_strconverts&str -> SlimStr, panicking on overflowfrom_str_mutconverts&mut str -> SlimStrMut, panicking on overflowfrom_stringconverts&str -> SlimStrBox, panicking on overflow
These conversions should be reserved for cases where it is known
that the length <= u32::MAX and should be used sparingly.
Some auxiliary and utility functionality is provided:
-
SlimSliceBoxCollected<T>exists to indirectly provideFromIterator<A>forSlimSliceBox<T> -
LenTooLong<T>, the error type when a conversion to a slimmer type would result in a length overflow. Optionally, the to-convert object is provided back to the user for handling -
try_intotries to convert the input to a slim type and forgets the input if an error occurred -
SafelyExchangeable<T>is implemented to assert thatSelfis safely transmutable, including when stored in a collection, to typeT
Structs§
- LenToo
Long - An error signifying that a container’s size was over
u32::MAX. - Slim
Slice - A shared reference to
[T]limited tou32::MAXin length. - Slim
Slice Box - Provides a slimmer version of
Box<[T]>usingu32for its length instead ofusize. - Slim
Slice BoxCollected - A wrapper to achieve
FromIterator<T> for Result<SlimSliceBox<T>, Vec<T>>. - Slim
Slice Mut - A mutable reference to
[T]limited tou32::MAXin length. - Slim
Small Slice Box - SlimStr
- A shared reference to
strlimited tou32::MAXin length. - Slim
StrBox - Provides a slimmer version of
Box<str>usingu32for its length instead ofusize. - Slim
StrMut - A mutable reference to
strlimited tou32::MAXin length.
Traits§
- Safely
Exchangeable - Implementors decree that
Selfcan be safely transmuted toT, including covariantly under a pointer.
Functions§
- from_
slice - Converts
&[T]into the slim limited version. - from_
slice_ mut - Converts
&mut [T]into the slim limited version. - from_
str - Converts
&strinto the slim limited version. - from_
str_ mut - Converts
&mut strinto the slim limited version. - from_
string - Converts
&strinto the owned slim limited version. - try_
into - Try to convert
xintoBand forget the container part of the error if any.