Expand description

Support for storing dynamically-sized types within fixed-size allocations

  • The Value type provides a fixed size (7 word in the current version) buffer in which a trait object or array can be stored, without resorting to a heap allocation.
  • The Fifo and Stack types provide collection types (first-in-first-out and last-in-first-out).

Examples

An unboxed any

As a quick example - The following wraps a 64-bit integer up in an inline DST using the Any trait.

let dst = ValueA::<dyn Any, [usize; 2]>::new_stable(1234u64, |p| p as _)
    .ok().expect("Integer did not fit in allocation");
println!("dst as u64 = {:?}", dst.downcast_ref::<u64>());
println!("dst as i8 = {:?}", dst.downcast_ref::<i8>());

Stack-allocated closure!

The following snippet shows how small ('static) closures can be returned using this crate

fn make_closure(value: u64) -> ValueA<dyn FnMut()->String, [usize; 3]> {
    ValueA::new_stable(move || format!("Hello there! value={}", value), |p| p as _)
        .ok().expect("Closure doesn't fit")
}
let mut closure = make_closure(666);
assert_eq!( (&mut *closure)(), "Hello there! value=666" );

Custom allocation sizes/types

If you need larger alignment, you can use a different type for the backing array. (Note, that metadata uses at least one slot in the array)

This code panics, because i128 requires 8/16 byte alignment (usually)

let v: ValueA<dyn Any, [u32; 6]> = ValueA::new_stable(123i128, |p| p as _).unwrap();

This works, because the backing buffer has sufficient alignment

let v: ValueA<dyn Any, [u128; 2]> = ValueA::new_stable(123i128, |p| p as _).unwrap();

Features

alloc (default)

Provides the StackDstA::new_or_boxed method (if unsize feature is active too)

const_generics (default)

Uses value/constant generics to provide a slightly nicer API

unsize (optional)

Uses the nightly feature unsize to provide a more egonomic API (no need for the |p| p closures)

Re-exports

pub use list::FifoA;
pub use stack::StackA;
pub use value::ValueA;

Modules

Implementation of the FIFO list structure

Implementation of the LIFO stack structure

Implementation of the single-value structure Single DST stored inline

Traits

Marker trait used to check alignment

Trait used to represent a data buffer, typically you’ll passs a [usize; N] array.

Trait that indicates that a type is valid for any bit pattern

Type Definitions

A FIFO queue of DSTs

A single LIFO stack of DSTs

A single dynamically-sized value