Crates.io | stack_dst |
lib.rs | stack_dst |
version | 0.8.1 |
source | src |
created_at | 2015-06-02 14:12:35.454326 |
updated_at | 2023-06-25 08:06:19.306058 |
description | A wrapper that allows storage of unsized values of up to a fixed size inline (without boxing) |
homepage | |
repository | https://github.com/thepowersgang/stack_dst-rs |
max_upload_size | |
id | 2285 |
size | 100,087 |
Inline (aka stack-allocated) dynamically-sized types, and collections of dyanmically-sized types using the same logic
This crate provides ways of storing DSTs directly within an allocation.
This crate covers two primary usecases
Value
allows storing (and returning) a single DST within a fixed-size allocationStack
and Fifo
allow heterogeneous collections without needing to box each object.One of the most obvious uses is to allow returning capturing closures without having to box them. In the example below, the closure
takes ownership of value
, and is then returned using a Value
use stack_dst::Value;
// The closure is stored in two 64-bit integers (one for the vtable, the other for the value)
fn make_closure(value: u64) -> Value<dyn Fn()->String, ::stack_dst::buffers::U64_2> {
if value < 0x10000 {
ValueA::new_stable(move || format!("Hello there! value={}", value), |v| v as _).ok().expect("Closure doesn't fit")
}
else {
ValueA::new_stable(move || format!("Hello there! value={:#x}", value), |v| v as _).ok().expect("Closure doesn't fit")
}
}
let closure = make_closure(12);
assert_eq!( closure(), "Hello there! value=12" );
MaybeUninit
, so requires at least 1.36Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.