stack_box

Crates.iostack_box
lib.rsstack_box
version
sourcesrc
created_at2024-11-09 10:44:19.009436
updated_at2024-11-10 05:42:23.529961
descriptionstore unsize struct on stack with static check
homepage
repositoryhttps://github.com/Fancyflame/stack_box.git
max_upload_size
id1441971
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
FancyFlame (Fancyflame)

documentation

https://docs.rs/stack_box/latest/stack_box/index.html

README

stack_box

Put your tiny stuff on stack!

If you are sure your stuff has or uses less than an exact size, but the compiler doesn't see and force you to put them on heap, this crate is for you.

use std::fmt::Debug;

use stack_box::{stackbox_coerce, FitStackBox};

// alias to a `StackBox` that could exactly contains a `[u32; 3]`
type U32Box<T> = FitStackBox!(T, [u32; 3]);

fn main() {
    let u16_box = U32Box::new(16u16); // ok
    let u32_box = U32Box::new([32u32; 3]); // ok

    // U32Box::new(0u64); // panic at compile time
    // U32Box::new([0u32; 4]); // panic at compile time
    assert!(U32Box::new_runtime_checked(0u64).is_err()); // align too large
    assert!(U32Box::new_runtime_checked([0u32; 4]).is_err()); // size too large

    let arr: [U32Box<dyn Debug>; 2] = [coerce!(u16_box), coerce!(u32_box)];

    for x in arr {
        dbg!(&*x);
    }
}

The expansion of coerce macro is also safe rust code.

Commit count: 11

cargo fmt