#fallible #collections

fallible_collections

a crate which adds fallible allocation api to std collections

9 unstable releases (3 breaking)

0.4.1 Apr 15, 2021
0.4.0 Jan 19, 2021
0.3.1 Dec 2, 2020
0.3.0 Oct 24, 2020
0.1.0 Oct 28, 2019

#172 in Rust patterns

Download history 149/week @ 2021-01-01 170/week @ 2021-01-08 393/week @ 2021-01-15 527/week @ 2021-01-22 1063/week @ 2021-01-29 1187/week @ 2021-02-05 1245/week @ 2021-02-12 1074/week @ 2021-02-19 1184/week @ 2021-02-26 1714/week @ 2021-03-05 1774/week @ 2021-03-12 2702/week @ 2021-03-19 2325/week @ 2021-03-26 2919/week @ 2021-04-02 2815/week @ 2021-04-09 1979/week @ 2021-04-16

6,927 downloads per month
Used in 30 crates (7 directly)

MIT/Apache

230KB
4.5K SLoC

Fallible Collections.rs

Implement api on rust collection wich returns a result when an allocation error occurs. This is inspired a lot by RFC 2116.

The api currently propose a fallible interface for Vec, Box, Arc, Btree and Rc, 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.

At the point of the last update of this README, the latest published version could be used like this:

Add the following dependency to your Cargo manifest...

[dependencies]
fallible_collections = "0.4.1"

...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

~445KB