Skip to main content

Module slim_slice

Module slim_slice 

Source
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:

The following convenience conversion functions are provided:

  • from_slice converts &[T] -> SlimSlice<T>, panicking on overflow
  • from_slice_mut converts &mut [T] -> SlimSliceMut<T>, panicking on overflow
  • from_str converts &str -> SlimStr, panicking on overflow
  • from_str_mut converts &mut str -> SlimStrMut, panicking on overflow
  • from_string converts &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 provide FromIterator<A> for SlimSliceBox<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_into tries to convert the input to a slim type and forgets the input if an error occurred

  • SafelyExchangeable<T> is implemented to assert that Self is safely transmutable, including when stored in a collection, to type T

Structs§

LenTooLong
An error signifying that a container’s size was over u32::MAX.
SlimSlice
A shared reference to [T] limited to u32::MAX in length.
SlimSliceBox
Provides a slimmer version of Box<[T]> using u32 for its length instead of usize.
SlimSliceBoxCollected
A wrapper to achieve FromIterator<T> for Result<SlimSliceBox<T>, Vec<T>>.
SlimSliceMut
A mutable reference to [T] limited to u32::MAX in length.
SlimSmallSliceBox
SlimStr
A shared reference to str limited to u32::MAX in length.
SlimStrBox
Provides a slimmer version of Box<str> using u32 for its length instead of usize.
SlimStrMut
A mutable reference to str limited to u32::MAX in length.

Traits§

SafelyExchangeable
Implementors decree that Self can be safely transmuted to T, 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 &str into the slim limited version.
from_str_mut
Converts &mut str into the slim limited version.
from_string
Converts &str into the owned slim limited version.
try_into
Try to convert x into B and forget the container part of the error if any.