#collection #fallible #allocation #oom #hash-map #try-reserve

no-std fallible_collections

a crate which adds fallible allocation api to std collections

19 releases

0.5.1 Nov 7, 2024
0.4.9 Jul 2, 2023
0.4.7 Mar 4, 2023
0.4.6 Nov 14, 2022
0.1.0 Oct 28, 2019

#118 in Rust patterns

Download history 14088/week @ 2024-07-26 12174/week @ 2024-08-02 18737/week @ 2024-08-09 16105/week @ 2024-08-16 22367/week @ 2024-08-23 14905/week @ 2024-08-30 20385/week @ 2024-09-06 10898/week @ 2024-09-13 19024/week @ 2024-09-20 25599/week @ 2024-09-27 11508/week @ 2024-10-04 18248/week @ 2024-10-11 17628/week @ 2024-10-18 16608/week @ 2024-10-25 12392/week @ 2024-11-01 23098/week @ 2024-11-08

73,713 downloads per month
Used in 50 crates (13 directly)

MIT/Apache

230KB
4.5K SLoC

Fallible Collections.rs

Implements APIs on Rust collections wich gracefully return a Result when an allocation error occurs. This is inspired a lot by RFC 2116.

There are APIs for a fallible interface for Vec, Box, BTree, HashMap, and a TryClone trait wich is implemented for primitive Rust traits and a fallible format macro. You can use this with try_clone_derive crate wich derive TryClone for your own types.

Getting Started

fallible_collections is available on crates.io. It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.

Add the following dependency to your Cargo manifest:

[dependencies]
fallible_collections = "0.5"

# or
fallible_collections = { version = "0.5", features = ["std"] }

...and see the docs for how to use it.

Example

Exemple of using the FallibleBox interface.

use fallible_collections::FallibleBox;

fn main() {
	// this crate an Ordinary box but return an error on allocation failure
	let mut a = <Box<_> as FallibleBox<_>>::try_new(5).unwrap();
	let mut b = Box::new(5);

	assert_eq!(a, b);
	*a = 3;
	assert_eq!(*a, 3);
}

Exemple of using the FallibleVec interface.

use fallible_collections::FallibleVec;

fn main() {
	// this crate an Ordinary Vec<Vec<u8>> but return an error on allocation failure
	let a: Vec<Vec<u8>> = try_vec![try_vec![42; 10].unwrap(); 100].unwrap();
	let b: Vec<Vec<u8>> = vec![vec![42; 10]; 100];
	assert_eq!(a, b);
	assert_eq!(a.try_clone().unwrap(), a);
	...
}

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

~0–455KB