| 1 | Fallible Collections.rs |
| 2 | ============== |
| 3 | |
| 4 | Implement api on rust collection wich returns a result when an allocation error occurs. |
| 5 | This is inspired a lot by [RFC 2116](https://github.com/rust-lang/rfcs/blob/master/text/2116-alloc-me-maybe.md). |
| 6 | |
| 7 | The api currently propose a fallible interface for Vec, Box, Arc, Btree and Rc, |
| 8 | a TryClone trait wich is implemented for primitive rust traits and a fallible format macro. |
| 9 | |
| 10 | You can use this with try_clone_derive crate wich derive TryClone for your own types. |
| 11 | |
| 12 | # Getting Started |
| 13 | |
| 14 | [fallible collections is available on crates.io](https://crates.io/crates/fallible_collections). |
| 15 | It is recommended to look there for the newest released version, as well as links to the newest builds of the docs. |
| 16 | |
| 17 | At the point of the last update of this README, the latest published version could be used like this: |
| 18 | |
| 19 | Add the following dependency to your Cargo manifest... |
| 20 | Add feature std and rust_1_57 to use the stabilized try_reserve api and the std HashMap type. Obviously, you cannot combine it with the 'unstable' feature. |
| 21 | Add integration tests that can be run with the tiny_integration_tester command. |
| 22 | |
| 23 | ```toml |
| 24 | [dependencies] |
| 25 | fallible_collections = "0.4" |
| 26 | |
| 27 | # or |
| 28 | fallible_collections = { version = "0.4", features = ["std", "rust_1_57"] } |
| 29 | ``` |
| 30 | |
| 31 | ...and see the [docs](https://docs.rs/fallible_collections) for how to use it. |
| 32 | |
| 33 | # Example |
| 34 | |
| 35 | Exemple of using the FallibleBox interface. |
| 36 | ```rust |
| 37 | use fallible_collections::FallibleBox; |
| 38 | |
| 39 | fn main() { |
| 40 | // this crate an Ordinary box but return an error on allocation failure |
| 41 | let mut a = <Box<_> as FallibleBox<_>>::try_new(5).unwrap(); |
| 42 | let mut b = Box::new(5); |
| 43 | |
| 44 | assert_eq!(a, b); |
| 45 | *a = 3; |
| 46 | assert_eq!(*a, 3); |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | Exemple of using the FallibleVec interface. |
| 51 | ```rust |
| 52 | use fallible_collections::FallibleVec; |
| 53 | |
| 54 | fn main() { |
| 55 | // this crate an Ordinary Vec<Vec<u8>> but return an error on allocation failure |
| 56 | let a: Vec<Vec<u8>> = try_vec![try_vec![42; 10].unwrap(); 100].unwrap(); |
| 57 | let b: Vec<Vec<u8>> = vec![vec![42; 10]; 100]; |
| 58 | assert_eq!(a, b); |
| 59 | assert_eq!(a.try_clone().unwrap(), a); |
| 60 | ... |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ## License |
| 65 | |
| 66 | Licensed under either of |
| 67 | |
| 68 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 69 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 70 | |
| 71 | at your option. |
| 72 | |
| 73 | ### Contribution |
| 74 | |
| 75 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 76 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any |
| 77 | additional terms or conditions. |
| 78 |