#dst #stack #dynamically-sized #inline #allocation #fixed-size #stack-allocated

no-std stack_dst

A wrapper that allows storage of unsized values of up to a fixed size inline (without boxing)

18 releases

Uses old Rust 2015

0.8.1 Jun 25, 2023
0.7.2 Jul 20, 2022
0.7.1 Feb 26, 2022
0.6.1 Mar 3, 2021
0.0.1 Jun 2, 2015

#311 in Rust patterns

Download history 181/week @ 2024-06-12 192/week @ 2024-06-19 187/week @ 2024-06-26 127/week @ 2024-07-03 216/week @ 2024-07-10 548/week @ 2024-07-17 619/week @ 2024-07-24 474/week @ 2024-07-31 238/week @ 2024-08-07 320/week @ 2024-08-14 194/week @ 2024-08-21 255/week @ 2024-08-28 297/week @ 2024-09-04 279/week @ 2024-09-11 245/week @ 2024-09-18 231/week @ 2024-09-25

1,084 downloads per month
Used in 8 crates (via stylish-core)

MIT/Apache

70KB
1.5K SLoC

stack_dst

Inline (aka stack-allocated) dynamically-sized types, and collections of dyanmically-sized types using the same logic

Overview

This crate provides ways of storing DSTs directly within an allocation.

Basic usage

This crate covers two primary usecases

  • Value allows storing (and returning) a single DST within a fixed-size allocation
  • Stack and Fifo allow heterogeneous collections without needing to box each object.

Example

Unboxed closure

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" );

Status

  • Works for most test cases
  • miri is happy with it
  • Not rigourously tested across platforms

Minimum rust version

  • Uses MaybeUninit, so requires at least 1.36

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~245KB