Crates.io | trybox |
lib.rs | trybox |
version | 0.1.1 |
source | src |
created_at | 2024-10-24 03:17:55.093078 |
updated_at | 2024-10-24 03:46:37.985977 |
description | stable, `no_std`-compatible, fallible heap allocation |
homepage | https://crates.io/crates/trybox |
repository | https://github.com/aatifsyed/trybox |
max_upload_size | |
id | 1420823 |
size | 16,146 |
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)?)
}
fallacy-box
fallible_collections
TryBox
wrapper struct, or the FallibleBox
extension trait.