[−][src]Crate stack_dst
Support for storing dynamically-sized types on the stack
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.
Examples
An unboxed any
As a quick example - The following wraps a 64-bit integer up in an inline DST using the Any trait.
use std::any::Any; use stack_dst::Value; let dst = Value::<dyn 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 this crate
use stack_dst::Value; fn make_closure(value: u64) -> Value<dyn FnMut()->String> { Value::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 a data buffer, typically you'll passs a |
Type Definitions
Value | Stack-allocated DST (using a default size) |