|
2 | 2 |
|
3 | 3 | Fallible allocation functions for the Rust standard library's [`alloc::vec::Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html) type. |
4 | 4 |
|
5 | | -These functions are designed to be usable with `#![no_std]` and `#[cfg(no_global_oom_handling)]`(see <https://github.com/rust-lang/rust/pull/84266>) enabled. |
| 5 | +These functions are designed to be usable with `#![no_std]`, `#[cfg(no_global_oom_handling)]` (see |
| 6 | +<https://github.com/rust-lang/rust/pull/84266>) enabled and Allocators (see <https://github.com/rust-lang/wg-allocators>). |
6 | 7 |
|
7 | 8 | ## Usage |
8 | 9 |
|
9 | | -The recommended way to add these functions to `Vec` is by adding a `use` declaration for the entire module: `use fallible_vec::*`: |
| 10 | +The recommended way to add these functions to `Vec` is by adding a `use` declaration for the |
| 11 | +`FallibleVec` trait: `use fallible_vec::FallibleVec`: |
10 | 12 | ```rust |
11 | | -use fallible_vec::*; |
| 13 | +use fallible_vec::{FallibleVec, try_vec}; |
| 14 | + |
12 | 15 | let mut vec = try_vec![1, 2]?; |
13 | 16 | vec.try_push(3)?; |
14 | 17 | assert_eq!(vec, [1, 2, 3]); |
15 | 18 | ``` |
16 | 19 |
|
17 | 20 | ## Panic Safety |
18 | 21 |
|
19 | | -These methods are "panic safe", meaning that if a call to external code (e.g., an iterator's `next()` method or an implementation of `Clone::clone()`) panics, then these methods will leave the `Vec` in a consistent state: |
| 22 | +These methods are "panic safe", meaning that if a call to external code (e.g., an iterator's |
| 23 | +`next()` method or an implementation of `Clone::clone()`) panics, then these methods will leave the |
| 24 | +`Vec` in a consistent state: |
20 | 25 | * `len()` will be less than or equal to `capacity()`. |
21 | | -* Items in `0..len()` will only be items originally in the `Vec` or items being added to the `Vec`. It will never include uninitialized memory, duplicated items or dropped items. |
22 | | -* Items originally (but no longer) in the `Vec` or being added to (but not yet in) the `Vec` may be leaked - any method that may leak items like this will have a note to specify its behavior. |
| 26 | +* Items in `0..len()` will only be items originally in the `Vec` or items being added to the `Vec`. |
| 27 | + It will never include uninitialized memory, duplicated items or dropped items. |
| 28 | +* Items originally (but no longer) in the `Vec` or being added to (but not yet in) the `Vec` may be |
| 29 | + leaked - any method that may leak items like this will have a note to specify its behavior. |
23 | 30 |
|
24 | 31 | The exact behavior of each method is specified in its documentation. |
25 | 32 |
|
26 | 33 | ## Code origin |
27 | 34 |
|
28 | | -Most of this code is forked from [Rust's Standard Library](https://github.com/rust-lang/rust). While we will attempt to keep the code and docs in sync, if you notice any issues please check if they have been fixed in the Standard Library first. |
| 35 | +Most of this code is forked from [Rust's Standard Library](https://github.com/rust-lang/rust). While |
| 36 | +we will attempt to keep the code and docs in sync, if you notice any issues please check if they |
| 37 | +have been fixed in the Standard Library first. |
29 | 38 |
|
30 | 39 | ## This API is incomplete |
31 | 40 |
|
32 | | -There are many more infallible functions on `Vec` which have not been ported yet. If there's a particular API that you're missing feel free to open a PR or file an Issue to get it added. |
33 | | - |
34 | | -## Why are these not already in the Standard Library |
35 | | - |
36 | | -There is a [PR to add these and more](https://github.com/rust-lang/rust/pull/95051) to the Standard Library, followed by an [RFC to discuss if it's a good idea or not to do so](https://github.com/rust-lang/rfcs/pull/3271). |
| 41 | +There are many more infallible functions on `Vec` which have not been ported yet. If there's a |
| 42 | +particular API that you're missing feel free to open a PR or file an Issue to get it added. |
| 43 | + |
| 44 | +## Why are these not already in the Standard Library? |
| 45 | + |
| 46 | +There is a [PR to add these and more](https://github.com/rust-lang/rust/pull/95051) to the Standard |
| 47 | +Library, followed by an [RFC to discuss if it's a good idea or not to do so](https://github.com/rust-lang/rfcs/pull/3271). |
| 48 | + |
| 49 | +## Why would I use this crate versus similar crates? |
| 50 | + |
| 51 | +In general, `fallible_vec` is only useful in situations where `#[cfg(no_global_oom_handling)]` is |
| 52 | +required, or if using the Allocator API (functions ending in `_in`). Other crates use APIs that |
| 53 | +don't exist when `#[cfg(no_global_oom_handling)]` is enabled (like `vec::push`), whereas |
| 54 | +`fallible_vec` reimplements each function to avoid these APIs and builds with `#[cfg(no_global_oom_handling)]` |
| 55 | +in its CI. |
| 56 | + |
| 57 | +`fallible_vec` focuses on `vec` alone, whereas other crates provide support for additional types |
| 58 | +(like `Box` and `HashMap`). |
| 59 | + |
| 60 | +Comparing `fallible_vec` to [`fallible_collections`](https://crates.io/crates/fallible_collections): |
| 61 | + |
| 62 | +| | `fallible_vec` v0.1.0 | `fallible_collections` v0.4.7 | |
| 63 | +|-------------------------------------------|:---------------------:|:-----------------------------:| |
| 64 | +| Supports `no_std` | X | X | |
| 65 | +| Supports `#[cfg(no_global_oom_handling)]` | X | | |
| 66 | +| Requires nightly | X | | |
| 67 | +| `vec::try_append` | | X | |
| 68 | +| `vec::try_extend` | X | | |
| 69 | +| `vec::try_extend_from_slice` | X | X | |
| 70 | +| `vec::try_insert` | X | X | |
| 71 | +| `vec::try_push` | X | X | |
| 72 | +| `vec::try_push_give_back` | | X | |
| 73 | +| `vec::try_resize` | X | X | |
| 74 | +| `vec::try_resize_with` | X | X | |
| 75 | +| `vec::try_splice_in` | X | | |
| 76 | +| `try_collect` | X | X | |
| 77 | +| `try_collect_in` | X | | |
| 78 | +| `try_from_iterator` | | X | |
| 79 | +| `try_with_capacity` | X | | |
| 80 | +| `try_with_capacity_in` | X | | |
| 81 | +| `try_vec!` | X | | |
| 82 | +| `try_vec_in!` | X | | |
| 83 | +| `Box::*` | | X | |
| 84 | +| `Arc::*` | | X | |
| 85 | +| `Rc::*` | | X | |
| 86 | +| `HashMap::*` | | X | |
| 87 | +| `try_format!` | | X | |
37 | 88 |
|
38 | 89 | ## Contributing |
39 | 90 |
|
|
0 commit comments