Crate miny

Source
Expand description

Repository Crates.io docs.rs MIT OR Apache-2.0

A Miny<T>1 is like a Box<T> with T stored inline for values less than a pointer in size. Requires nightly Rust2 & alloc

§Examples

let small = Miny::new(1_u8);
let large = Miny::new([1_usize; 32]);
// small is stored inline on the stack
assert!(Miny::on_stack(&small));
// large is stored with an allocation
assert!(!Miny::on_stack(&large));
// consume the miny and get back a value
let original = Miny::into_inner(large);
assert_eq!(original, [1; 32]);

To use unsized values, call unsize with a type or use the new_unsized shorthand3

let value = Miny::new_unsized::<[usize]>([1_usize; 32]);
// it's usable as a [usize]
assert_eq!(value.len(), 32);
// and you can consume it to get a boxed value
let boxed = Miny::into_box(value);
assert_eq!(boxed, Box::new([1_usize; 32]) as Box<[usize]>);

Or if you have a box you can directly convert it into a Miny

let large = Miny::from(Box::new([1_usize; 32]) as Box<[usize]>);
assert_eq!(large.len(), 32);
// this is slightly inefficient as it boxes and then un-boxes the value,
// prefer using `new` / `new_unsized` for this
let small = Miny::from(Box::new([1_u8, 2]) as Box<[u8]>); assert_eq!(small.len(), 2);

The name is because it originally was just a “mini Box<dyn Any>”, although it supports any type

Uses ptr_metadata (Reading the metadata pointer & storing it), layout_for_ptr (Determining value size without reading the value), and unsize (new_unsized & unsize functions) features

This is needed because the Miny layout is too weird for CoerceUnsized to work properly


  1.  
  2.  
  3.  

Structs§

Miny
Box<T> but with small data stored inline