Debian Codegraph @

debian/missing-sources/unarray/README.md

raw symbols SCIP
1 # Unarray
2
3 [![Docs badge]][docs.rs]
4 [![crates badge]][crates.io]
5 [![github badge]][github]
6 [![license badge]][github]
7
8 Utilities for working with uninitialized arrays
9
10 - No dependencies
11 - `#[no_std]`
12 - No panics (all APIs return `Result` or `Option`)
13
14 This crate provides a few sets of APIs:
15
16 ### `uninit_buf` and `mark_initialized`
17
18 These are a pair of functions which are generally used as follows:
19 - stack-allocate an uninitialized array with `uninit_buf`
20 - initialize each element
21 - unsafely convert it to an initialized array with `mark_initialized`
22
23 For example:
24 ```rust
25 use unarray::*;
26
27 fn main() {
28 let mut buffer = uninit_buf::<i32; 10>();
29
30 for slot in &mut buffer {
31 slot.write(123);
32 }
33
34 let array = unsafe { mark_initialized(buffer) };
35
36 assert_eq!(array, [123; 10]);
37 }
38 ```
39
40 This is simple to understand, but still requires `unsafe`, which is hard to justify in many cases
41
42 ### `build_array_*`
43
44 Functions to build arrays from a length and a function that maps from index -> value:
45 ```rust
46 let even_numbers = build_array(|i| i * 2); // const generic length parameter inferred
47 assert_eq!(even_numbers, [0, 2, 4]);
48
49 let numbers = build_array_option::<usize, 3>(|i| 3.checked_sub(i));
50 assert_eq!(numbers, Some([3, 2, 1]));
51
52 let numbers = build_array_option::<usize, 5>(|i| 3.checked_sub(i));
53 assert_eq!(numbers, None); // since a single element failed, the whole operation failed
54 ```
55 There is also an equivalent `build_array_result` for `Result`-returning functions
56
57 ### Collecting iterators to arrays
58
59 It's fairly common to want to collect an iterator into an array, but this is currently tricky in
60 stable Rust, since iterators don't carry compile-time information about their length. Because of this,
61 arrays don't implement `FromIterator`, which is required for `.collect()` to work.
62
63 Instead, this library provides `ArrayFromIter`, which **does** implement `FromIterator`. This struct can
64 be destructured to get an `Option<[T, N]>`. If the iterator contained exactly `N` elements, this is `Some(array)`, otherwise, it is `None`:
65 ```rust
66 let iter = [1, 2, 3].into_iter();
67 match iter.collect() {
68 ArrayFromIter(Some([a, b, c])) => println!("exactly 3 elements: {a}, {b}, {c}"),
69 ArrayFromIter(None) => println!("not 3 elements"),
70 }
71 ```
72
73 ### `UnarrayArrayExt` extension trait
74
75 ```rust
76 // mapping an array via a `Result`
77 let strings = ["123", "234"];
78 let numbers = strings.map_result(|s| s.parse());
79 assert_eq!(numbers, Ok([123, 234]));
80
81 let bad_strings = ["123", "uh oh"];
82 let result = bad_strings.map_result(|s| s.parse::<i32>());
83 assert!(result.is_err()); // since one of the element fails, the whole operation fails
84 ```
85 There is also `map_option` for functions which return an `Option`
86
87 ## License
88
89 Licensed under either of
90
91 * Apache License, Version 2.0
92 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
93 * MIT license
94 ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
95
96 at your option.
97
98 ## Contribution
99
100 Unless you explicitly state otherwise, any contribution intentionally submitted
101 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
102 dual licensed as above, without any additional terms or conditions.
103
104
105
106 [Docs badge]: https://img.shields.io/badge/docs.rs-rustdoc-green
107 [docs.rs]: https://docs.rs/unarray/
108 [crates badge]: https://img.shields.io/crates/v/unarray
109 [crates.io]: https://crates.io/crates/unarray
110 [license badge]: https://img.shields.io/crates/l/unarray
111 [github badge]: https://img.shields.io/github/checks-status/cameron1024/unarray/master
112 [github]: https://github.com/cameron1024/unarray