trybox

Crates.iotrybox
lib.rstrybox
version
sourcesrc
created_at2024-10-24 03:17:55.093078
updated_at2024-12-02 07:29:20.516972
descriptionstable, `no_std`-compatible, fallible heap allocation
homepagehttps://crates.io/crates/trybox
repositoryhttps://github.com/aatifsyed/trybox
max_upload_size
id1420823
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | 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
Aatif Syed (aatifsyed)

documentation

https://docs.rs/trybox

README

Stable, no_std-compatible, fallible heap allocation for [Box].

Basic usage is as follows:

match trybox::new(1) {
    Ok(heaped) => {
        let _: Box<i32> = heaped;
    }
    Err(ErrorWith(stacked)) => {
        let _: i32 = stacked; // failed object is returned on the stack
    },
}

You may drop the object after allocation failure instead, choosing to e.g propogate or wrap the [Error].

fn fallible<T>(x: T) -> Result<Box<T>, Box<dyn std::error::Error + Send + Sync>> {
    Ok(trybox::or_drop(x)?)
}

Care has been taken to optimize the size of [Error] down to a single usize:

assert_eq!(size_of::<trybox::Error>(), size_of::<usize>());

And to provide ergonomic error messages:

memory allocation of 4 bytes (for type i32) failed
memory allocation of 2.44 kibibytes (for type [u8; 2500]) failed

Conversions to [std::io::Error] and [std::io::ErrorKind::OutOfMemory] are provided when the "std" feature is enabled:

fn fallible<T>(x: T) -> std::io::Result<Box<T>> {
    Ok(trybox::or_drop(x)?)
}

Comparison with other crates

Commit count: 12

cargo fmt