Crate stack_dst [−] [src]
Stack-based Dynamically-Sized Types
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 stack_dst::StackDST; use std::any::Any; 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
use stack_dst::StackDST; 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
StackDSTA |
Stack-allocated dynamically sized type |
Constants
DEFAULT_SIZE |
8 data words, plus one metadata |
Traits
DataBuf |
Trait used to represent the data buffer for StackDSTA. |
Type Definitions
StackDST |
Stack-allocated DST (using a default size) |