Crate stack_dst [−] [src]
Support for storing dynamically-sized types on the stack
The StackDST
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.
Examples
An unboxed any
As a quick example - The following wraps a 64-bit integer up in a StackDST using the Any trait.
use std::any::Any; type StackDST<T> = stack_dst::Value<T>; let dst = StackDST::<Any>::new(1234u64).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 StackDST
type StackDST<T> = stack_dst::Value<T>; fn make_closure(value: u64) -> StackDST<FnMut()->String> { StackDST::new(move || format!("Hello there! value={}", value)).ok().expect("Closure doesn't fit") } let mut closure = make_closure(666); assert_eq!( (&mut *closure)(), "Hello there! value=666" );
Structs
StackA |
A fixed-capacity stack that can contain dynamically-sized types |
ValueA |
Stack-allocated dynamically sized type |
Traits
DataBuf |
Trait used to represent the data buffer for StackDSTA. |
Type Definitions
Value |
Stack-allocated DST (using a default size) |