| Crates.io | small_len |
| lib.rs | small_len |
| version | 1.1.2 |
| created_at | 2024-07-02 00:03:57.926563+00 |
| updated_at | 2024-07-17 06:31:20.451916+00 |
| description | A small library for storing the length in the smallest internal type. |
| homepage | |
| repository | https://gitlab.com/magicfoodhand/small_len |
| max_upload_size | |
| id | 1289118 |
| size | 21,312 |
A simple enum and trait to ensure that the value returned by small_len() is always the smallest representation. This is meant to be used by fn_vm to support "infinite" commands, arguments, and registers.
NOTE: This is primarily meant for dynamic sized objects, String, [T], &[T], Vec, HashMap, and IndexMap (with feature indexmap); if you need an optimization for static bounds at compile time check out the smallnum crate.
For a generic number crate check out varnum.
use small_len::SmallLen;
fn main() {
let a = vec![1, 2, 3];
let c = a.small_len(); // Length::Byte(3)
let bytes = c.to_be_bytes();
let c = SmallLen::from_be_bytes(&bytes); // SmallLen::from_bytes() -> Length::Byte(3)
}
default: No extra features, includes Vecbumpalo: Adds Len implementation for bumpalo::Bump.bytes: Adds Len implementation for bytes::Bytes and bytes::BytesMut.indexmap: Adds Len implementation for indexmap::IndexMap.If you need to add SmallLen to another type, you can implement the Len trait.
impl <T> Len for Vec<T> {
#[inline]
fn length(&self) -> Length {
self.len().into() // Length::new(self.len())
}
}
The Length enum also implements the following traits for easier use (Length | SmallLength | usize):