├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | # Slice patterns were 1.26, but we need NLL slice patterns, 4 | # thus our MSRV is 1.31 as the first with the 2018 edition. 5 | - 1.31.0 6 | - stable 7 | - beta 8 | - nightly 9 | matrix: 10 | allow_failures: 11 | - rust: nightly 12 | fast_finish: true 13 | 14 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "arraytools" 3 | version = "0.1.5" 4 | edition = "2018" 5 | 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/scottmcm/arraytools" 8 | documentation = "https://docs.rs/arraytools/" 9 | readme = "README.md" 10 | authors = ["scottmcm"] 11 | description = "A variety of helpful methods for working with fixed-size arrays." 12 | keywords = ["array", "no_std"] 13 | categories = ["no-std", "rust-patterns"] 14 | 15 | [dependencies] 16 | # none! 17 | 18 | [badges] 19 | travis-ci = { repository = "scottmcm/arraytools" } 20 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arraytools 2 | 3 | A variety of helpful methods for working with fixed-size arrays. 4 | 5 | [![docs.rs-hosted documentation](https://docs.rs/arraytools/badge.svg)](https://docs.rs/arraytools) 6 | [![travis build status](https://travis-ci.com/scottmcm/arraytools.svg)](https://travis-ci.com/scottmcm/arraytools) 7 | [![crates.io latest version](https://meritbadge.herokuapp.com/arraytools)](https://crates.io/crates/arraytools) 8 | 9 | ## Examples 10 | 11 | `Iterator`-like methods over arrays: 12 | 13 | ```rust 14 | use arraytools::ArrayTools; 15 | 16 | assert_eq!([1, 2, 3].map(|x| x+1), [2, 3, 4]); 17 | assert_eq!([1, 2].zip(["one", "two"]), [(1, "one"), (2, "two")]); 18 | ``` 19 | 20 | Ways to simplify array creation: 21 | 22 | ```rust 23 | use arraytools::ArrayTools; 24 | 25 | let mut state = 1; 26 | assert_eq!(<[_; 4]>::generate(|| { state *= 2; state }), [2, 4, 8, 16]); 27 | assert_eq!(<[usize; 4]>::indices(), [0, 1, 2, 3]); 28 | 29 | let s = "hello".to_string(); // Something `!Copy` 30 | assert_eq!(<[String; 3]>::repeat(s).as_ref_array(), ["hello", "hello", "hello"]); 31 | ``` 32 | 33 | Conversion to and from homogeneous tuples: 34 | 35 | ```rust 36 | use arraytools::ArrayTools; 37 | 38 | let mut array = [2, 3, 5, 7, 11]; 39 | assert_eq!(array.into_tuple(), (2, 3, 5, 7, 11)); 40 | array = ArrayTools::from_tuple((1, 1, 2, 3, 5)); 41 | assert_eq!(array, [1, 1, 2, 3, 5]); 42 | ``` 43 | 44 | ## Usage 45 | 46 | How to use with cargo: 47 | 48 | ```toml 49 | [dependencies] 50 | arraytools = "0.1" 51 | ``` 52 | 53 | How to use in your 2018-edition crate: 54 | 55 | ```rust 56 | use arraytools::ArrayTools; 57 | ``` 58 | 59 | Because this needs non-`Copy` slice patterns, it needs at least **Rust 1.31.0**. 60 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![forbid(unsafe_code)] 2 | #![cfg_attr(not(test), no_std)] 3 | 4 | //! This crate offers the [`ArrayTools`] extension trait, which provides 5 | //! a variety of helpful methods for working with fixed-size arrays. 6 | //! 7 | //! [`ArrayTools`]: trait.ArrayTools.html 8 | //! 9 | //! # Examples 10 | //! 11 | //! `Iterator`-like methods over arrays: 12 | //! 13 | //! ```rust 14 | //! use arraytools::ArrayTools; 15 | //! 16 | //! assert_eq!([1, 2, 3].map(|x| x+1), [2, 3, 4]); 17 | //! assert_eq!([1, 2].zip(["one", "two"]), [(1, "one"), (2, "two")]); 18 | //! ``` 19 | //! 20 | //! Ways to simplify array creation: 21 | //! 22 | //! ```rust 23 | //! use arraytools::ArrayTools; 24 | //! 25 | //! let mut state = 1; 26 | //! assert_eq!(<[_; 4]>::generate(|| { state *= 2; state }), [2, 4, 8, 16]); 27 | //! assert_eq!(<[usize; 4]>::indices(), [0, 1, 2, 3]); 28 | //! 29 | //! let s = "hello".to_string(); // Something `!Copy` 30 | //! assert_eq!(<[String; 3]>::repeat(s).as_ref_array(), ["hello", "hello", "hello"]); 31 | //! ``` 32 | //! 33 | //! Conversion to and from homogeneous tuples: 34 | //! 35 | //! ```rust 36 | //! use arraytools::ArrayTools; 37 | //! 38 | //! let mut array = [2, 3, 5, 7, 11]; 39 | //! assert_eq!(array.into_tuple(), (2, 3, 5, 7, 11)); 40 | //! array = ArrayTools::from_tuple((1, 1, 2, 3, 5)); 41 | //! assert_eq!(array, [1, 1, 2, 3, 5]); 42 | //! ``` 43 | //! 44 | //! Like `Option`, most combinators here take `self`. To not move something, 45 | //! you can use [`.as_ref_array()`] or [`.as_mut_array()`]: 46 | //! 47 | //! [`.as_ref_array()`]: trait.ArrayTools.html#method.as_ref_array 48 | //! [`.as_mut_array()`]: trait.ArrayTools.html#method.as_mut_array 49 | //! 50 | //! ```rust 51 | //! use arraytools::ArrayTools; 52 | //! 53 | //! struct SevenStrings([String; 7]); 54 | //! impl SevenStrings { 55 | //! fn push_str(&mut self, s: &str) { 56 | //! self.0.as_mut_array().for_each(|x: &mut String| x.push_str(s)); 57 | //! } 58 | //! } 59 | //! ``` 60 | //! 61 | 62 | use self::traits::*; 63 | 64 | /// An extension trait for working with fixed-length arrays. 65 | /// 66 | /// Use it with 67 | /// ```rust 68 | /// use arraytools::ArrayTools; 69 | /// ``` 70 | /// 71 | /// For an overview, see the [crate-level documentation](index.html). 72 | /// 73 | /// (This trait is sealed; you are not allowed to implement it yourself.) 74 | pub trait ArrayTools: Sized + Sealed { 75 | /// The type of the elements in this array 76 | /// 77 | /// ```rust 78 | /// # type T = usize; 79 | /// # const N: usize = 1; 80 | /// use arraytools::ArrayTools; 81 | /// 82 | /// # fn _foo() where 83 | /// [T; N]: ArrayTools 84 | /// # {} 85 | /// ``` 86 | type Element; 87 | 88 | /// The number of the elements in this array 89 | /// 90 | /// ```rust 91 | /// # type T = usize; 92 | /// # const N: usize = 1; 93 | /// use arraytools::ArrayTools; 94 | /// 95 | /// assert_eq!(<[T; N] as ArrayTools>::LEN, N); 96 | /// ``` 97 | const LEN: usize; 98 | 99 | /// Extracts a slice containing the entire array. 100 | /// 101 | /// ```rust 102 | /// # type T = usize; 103 | /// # const N: usize = 1; 104 | /// use arraytools::ArrayTools; 105 | /// 106 | /// let array: [i32; 5] = [1, 2, 3, 4, 5]; 107 | /// let slice: &[i32] = array.as_slice(); 108 | /// assert_eq!(slice.len(), 5); 109 | /// ``` 110 | fn as_slice(&self) -> &[Self::Element]; 111 | 112 | /// Extracts a mutable slice containing the entire array. 113 | /// 114 | /// ```rust 115 | /// # type T = usize; 116 | /// # const N: usize = 1; 117 | /// use arraytools::ArrayTools; 118 | /// 119 | /// let mut array: [i32; 5] = [1, 2, 3, 4, 5]; 120 | /// let slice: &mut [i32] = array.as_mut_slice(); 121 | /// assert_eq!(slice.len(), 5); 122 | /// ``` 123 | fn as_mut_slice(&mut self) -> &mut [Self::Element]; 124 | 125 | /// The homogeneous tuple type equivalent to this array type. 126 | /// 127 | /// ```rust 128 | /// # type T = usize; 129 | /// use arraytools::ArrayTools; 130 | /// 131 | /// # fn _foo() where 132 | /// [T; 4]: ArrayTools 133 | /// # {} 134 | /// ``` 135 | type Tuple; 136 | 137 | /// Converts a homogeneous tuple into the equivalent array. 138 | /// 139 | /// Type: `(T, T, ..., T) -> [T; N]` 140 | /// 141 | /// ```rust 142 | /// use arraytools::ArrayTools; 143 | /// 144 | /// assert_eq!(<[_; 3]>::from_tuple((1, 2, 3)), [1, 2, 3]); 145 | /// ``` 146 | fn from_tuple(tuple: Self::Tuple) -> Self; 147 | 148 | /// Converts this array into the equivalent homogeneous tuple. 149 | /// 150 | /// Type: `[T; N] -> (T, T, ..., T)` 151 | /// 152 | /// ```rust 153 | /// use arraytools::ArrayTools; 154 | /// 155 | /// assert_eq!([1, 2, 3].into_tuple(), (1, 2, 3)); 156 | /// ``` 157 | fn into_tuple(self) -> Self::Tuple; 158 | 159 | /// Builds an array by calling the provided function. 160 | /// 161 | /// Type: `F -> [T; N]` 162 | /// - when `N <= 1` this requires `F: FnOnce() -> T` 163 | /// - when `N > 1` this requires `F: FnMut() -> T` 164 | /// 165 | /// ```rust 166 | /// use arraytools::ArrayTools; 167 | /// 168 | /// let mut x = 1; 169 | /// let array: [_; 5] = ArrayTools::generate(|| { x *= 2; x }); 170 | /// assert_eq!(array, [2, 4, 8, 16, 32]); 171 | /// ``` 172 | fn generate(f: F) -> Self 173 | where Self: ArrayGenerate 174 | { 175 | ArrayGenerate::generate(f) 176 | } 177 | 178 | /// Builds an array by cloning the provided value. 179 | /// 180 | /// Type: `T -> [T; N]` 181 | /// 182 | /// ```rust 183 | /// use arraytools::ArrayTools; 184 | /// 185 | /// let mut v = Vec::with_capacity(10); 186 | /// v.push(42); 187 | /// let array: [_; 5] = ArrayTools::repeat(v); 188 | /// assert_eq!(array, [[42], [42], [42], [42], [42]]); 189 | /// assert_eq!(array[3].capacity(), 1); 190 | /// assert_eq!(array[4].capacity(), 10); // The last one is moved 191 | /// ``` 192 | fn repeat(x: T) -> Self 193 | where Self: ArrayRepeat 194 | { 195 | ArrayRepeat::repeat(x) 196 | } 197 | 198 | /// Builds an array containing a prefix of the provided iterator, 199 | /// or returns `None` if it didn't contain sufficient items. 200 | /// 201 | /// Type: `impl IntoIterator -> Option<[T; N]>` 202 | /// 203 | /// ```rust 204 | /// use arraytools::ArrayTools; 205 | /// 206 | /// assert_eq!(<[i16; 4]>::from_iter(1..), Some([1, 2, 3, 4])); 207 | /// 208 | /// assert_eq!(<[_; 4]>::from_iter(1..4), None); 209 | /// assert_eq!(<[_; 4]>::from_iter(1..5), Some([1, 2, 3, 4])); 210 | /// assert_eq!(<[_; 4]>::from_iter(1..6), Some([1, 2, 3, 4])); 211 | /// 212 | /// assert_eq!(<[u8; 1]>::from_iter(Some(1)), Some([1])); 213 | /// assert_eq!(<[u8; 1]>::from_iter(None), None); 214 | /// ``` 215 | fn from_iter(it: I) -> Option 216 | where Self: ArrayFromIter 217 | { 218 | ArrayFromIter::from_iter(it.into_iter()) 219 | } 220 | 221 | /// Builds the array `[0, 1, 2, ..., LEN-1]`. 222 | /// 223 | /// Type: `() -> [usize; N]` 224 | /// 225 | /// ```rust 226 | /// use arraytools::ArrayTools; 227 | /// 228 | /// let array: [_; 5] = ArrayTools::indices(); 229 | /// assert_eq!(array, [0, 1, 2, 3, 4]); 230 | /// ``` 231 | fn indices() -> Self 232 | where Self: ArrayIndices 233 | { 234 | ArrayIndices::indices() 235 | } 236 | 237 | /// Builds a new array by applying the provided function to each element of this array. 238 | /// 239 | /// Type: `([T; N], F) -> [U; N]` 240 | /// - when `N <= 1` this requires `F: FnOnce(T) -> U` 241 | /// - when `N > 1` this requires `F: FnMut(T) -> U` 242 | /// 243 | /// ```rust 244 | /// use arraytools::ArrayTools; 245 | /// 246 | /// assert_eq!([1, 10, 100].map(|x| x + 10), [11, 20, 110]); 247 | /// ``` 248 | #[must_use = "if you don't need the result, use `for_each`"] 249 | fn map(self, f: F) -> >::Output 250 | where Self: ArrayMap 251 | { 252 | ArrayMap::map(self, f) 253 | } 254 | 255 | /// Runs the provided function on each element of this array. 256 | /// 257 | /// Type: `([T; N], F) -> ()` 258 | /// - when `N <= 1` this requires `F: FnOnce(T) -> ()` 259 | /// - when `N > 1` this requires `F: FnMut(T) -> ()` 260 | /// 261 | /// ```rust 262 | /// use arraytools::ArrayTools; 263 | /// 264 | /// let mut array = [1, 10, 100]; 265 | /// array.as_mut_array().for_each(|x: &mut u8| *x += 10); 266 | /// assert_eq!(array, [11, 20, 110]); 267 | /// ``` 268 | fn for_each(self, f: F) 269 | where Self: ArrayMap 270 | { 271 | ArrayMap::map(self, f); 272 | } 273 | 274 | /// Combines two equal-length arrays into an array of tuples. 275 | /// 276 | /// Type: `([T; N], [U; N]) -> [(T, U); N]` 277 | /// 278 | /// ```rust 279 | /// use arraytools::ArrayTools; 280 | /// 281 | /// assert_eq!([10, 20, 30].zip([1.0, 2.0, 3.0]), [(10, 1.0), (20, 2.0), (30, 3.0)]); 282 | /// ``` 283 | fn zip(self, other: T) -> >::Output 284 | where Self: ArrayZip 285 | { 286 | ArrayZip::zip(self, other) 287 | } 288 | 289 | /// Combines two equal-length arrays using the provided function. 290 | /// 291 | /// Type: `([T; N], [U; N], F) -> [V; N]` 292 | /// - when `N <= 1` this requires `F: FnOnce(T, U) -> V` 293 | /// - when `N > 1` this requires `F: FnMut(T, U) -> V` 294 | /// 295 | /// ```rust 296 | /// use arraytools::ArrayTools; 297 | /// 298 | /// assert_eq!([10, 20, 30].zip_with([3, 2, 1], std::ops::Add::add), [13, 22, 31]); 299 | /// ``` 300 | fn zip_with(self, other: T, f: F) -> >::Output 301 | where Self: ArrayZipWith 302 | { 303 | ArrayZipWith::zip_with(self, other, f) 304 | } 305 | 306 | /// Builds an array of references to the elements of this array. 307 | /// 308 | /// Type: `&'a [T; N] -> [&'a T; N]` 309 | /// 310 | /// ```rust 311 | /// use arraytools::ArrayTools; 312 | /// 313 | /// let array = &[1, 2, 3, 4, 5]; 314 | /// assert_eq!(array.as_ref_array(), [&1, &2, &3, &4, &5]); 315 | /// ``` 316 | fn as_ref_array<'a>(&'a self) -> >::Output 317 | where Self: ArrayAsRef<'a> 318 | { 319 | ArrayAsRef::as_ref(self) 320 | } 321 | 322 | /// Builds an array of mutable references to the elements of this array. 323 | /// 324 | /// Type: `&'a mut [T; N] -> [&'a mut T; N]` 325 | /// 326 | /// ```rust 327 | /// use arraytools::ArrayTools; 328 | /// 329 | /// let array = &mut [1, 2, 3]; 330 | /// assert_eq!(array.as_ref_array(), [&mut 1, &mut 2, &mut 3]); 331 | /// ``` 332 | fn as_mut_array<'a>(&'a mut self) -> >::Output 333 | where Self: ArrayAsMut<'a> 334 | { 335 | ArrayAsMut::as_mut(self) 336 | } 337 | 338 | /// Appends an item to this array, returning the new array 339 | /// 340 | /// Type: `([T; N], T) -> [T; N+1]` 341 | /// 342 | /// ```rust 343 | /// use arraytools::ArrayTools; 344 | /// 345 | /// assert_eq!([1, 2].push_back(10), [1, 2, 10]); 346 | /// ``` 347 | #[must_use = "this returns the new array; it doesn't update the existing one"] 348 | fn push_back(self, item: U) -> >::Output 349 | where Self: ArrayPush 350 | { 351 | ArrayPush::push_back(self, item) 352 | } 353 | 354 | /// Prepends an item to this array, returning the new array 355 | /// 356 | /// Type: `([T; N], T) -> [T; N+1]` 357 | /// 358 | /// ```rust 359 | /// use arraytools::ArrayTools; 360 | /// 361 | /// assert_eq!([1, 2].push_front(10), [10, 1, 2]); 362 | /// ``` 363 | #[must_use = "this returns the new array; it doesn't update the existing one"] 364 | fn push_front(self, item: U) -> >::Output 365 | where Self: ArrayPush 366 | { 367 | ArrayPush::push_front(self, item) 368 | } 369 | 370 | /// Splits the last item off from this array, returning a tuple of 371 | /// an array of the other elements and the split-off item. 372 | /// 373 | /// Type: `[T; N+1] -> ([T; N], T)` 374 | /// 375 | /// ```rust 376 | /// use arraytools::ArrayTools; 377 | /// 378 | /// assert_eq!([1, 2, 3].pop_back(), ([1, 2], 3)); 379 | /// ``` 380 | #[must_use = "this returns the new array; it doesn't update the existing one"] 381 | fn pop_back(self) -> (>::Output, U) 382 | where Self: ArrayPop 383 | { 384 | ArrayPop::pop_back(self) 385 | } 386 | 387 | /// Splits the first item off from this array, returning a tuple of 388 | /// an array of the other elements and the split-off item. 389 | /// 390 | /// Type: `[T; N+1] -> ([T; N], T)` 391 | /// 392 | /// ```rust 393 | /// use arraytools::ArrayTools; 394 | /// 395 | /// assert_eq!([1, 2, 3].pop_front(), ([2, 3], 1)); 396 | /// ``` 397 | #[must_use = "this returns the new array; it doesn't update the existing one"] 398 | fn pop_front(self) -> (>::Output, U) 399 | where Self: ArrayPop 400 | { 401 | ArrayPop::pop_front(self) 402 | } 403 | } 404 | 405 | mod traits { 406 | pub trait Sealed {} 407 | 408 | pub trait ArrayGenerate { 409 | fn generate(f: F) -> Self; 410 | } 411 | 412 | pub trait ArrayRepeat { 413 | fn repeat(x: T) -> Self; 414 | } 415 | 416 | pub trait ArrayFromIter { 417 | fn from_iter(it: I) -> Option where Self: Sized; 418 | } 419 | 420 | pub trait ArrayIndices { 421 | fn indices() -> Self; 422 | } 423 | 424 | pub trait ArrayMap { 425 | type Output; 426 | type OutputElement; 427 | fn map(array: Self, f: F) -> Self::Output; 428 | } 429 | 430 | pub trait ArrayZip { 431 | type Output; 432 | fn zip(array: Self, other: T) -> Self::Output; 433 | } 434 | 435 | pub trait ArrayZipWith { 436 | type Output; 437 | fn zip_with(array: Self, other: T, f: F) -> Self::Output; 438 | } 439 | 440 | pub trait ArrayAsRef<'a> { 441 | type Output: 'a; 442 | fn as_ref(array: &'a Self) -> Self::Output; 443 | } 444 | 445 | pub trait ArrayAsMut<'a> { 446 | type Output: 'a; 447 | fn as_mut(array: &'a mut Self) -> Self::Output; 448 | } 449 | 450 | pub trait ArrayPush { 451 | type Output; 452 | fn push_back(array: Self, item: T) -> Self::Output; 453 | fn push_front(array: Self, item: T) -> Self::Output; 454 | } 455 | 456 | pub trait ArrayPop { 457 | type Output; 458 | fn pop_back(array: Self) -> (Self::Output, T); 459 | fn pop_front(array: Self) -> (Self::Output, T); 460 | } 461 | } 462 | 463 | #[allow(unused_mut, unused_variables)] 464 | mod impls { 465 | use super::*; 466 | 467 | macro_rules! replace_ident { 468 | ($i:ident => $($j:tt)*) => ($($j)*) 469 | } 470 | 471 | macro_rules! array_by_cloning { 472 | ($x:ident:) => ( [] ); 473 | ($x:ident: $first:ident $($i:ident)*) => ( [$(replace_ident!($i => $x.clone()),)* $x] ); 474 | } 475 | 476 | macro_rules! impl_for_size { 477 | ($n:expr; $fn_trait:ident => $($i:ident)* / $($j:ident)*) => ( 478 | 479 | impl Sealed for [T; $n] {} 480 | impl ArrayTools for [T; $n] { 481 | type Element = T; 482 | const LEN: usize = $n; 483 | fn as_slice(&self) -> &[Self::Element] { self } 484 | fn as_mut_slice(&mut self) -> &mut [Self::Element] { self } 485 | 486 | type Tuple = ($(replace_ident!($i => T),)*); 487 | fn from_tuple(tuple: Self::Tuple) -> Self { 488 | let ($($i,)*) = tuple; 489 | [$($i,)*] 490 | } 491 | fn into_tuple(self) -> Self::Tuple { 492 | let [$($i,)*] = self; 493 | ($($i,)*) 494 | } 495 | } 496 | impl ArrayGenerate for [T; $n] 497 | where F: $fn_trait() -> T 498 | { 499 | fn generate(mut f: F) -> Self { 500 | [$(replace_ident!($i => f()),)*] 501 | } 502 | } 503 | impl ArrayRepeat for [T; $n] 504 | where T: Clone 505 | { 506 | fn repeat(x: T) -> Self { 507 | array_by_cloning!(x: $($i)*) 508 | } 509 | } 510 | impl ArrayFromIter for [T; $n] 511 | where I: Iterator 512 | { 513 | fn from_iter(mut it: I) -> Option { 514 | Some([$(replace_ident!($i => it.next()?),)*]) 515 | } 516 | } 517 | impl ArrayIndices for [usize; $n] { 518 | fn indices() -> Self { 519 | let mut i = 0; 520 | ArrayTools::generate(|| { let t = i; i += 1; t }) 521 | } 522 | } 523 | impl ArrayMap for [T; $n] 524 | where F: $fn_trait(T) -> U 525 | { 526 | type Output = [U; $n]; 527 | type OutputElement = U; 528 | fn map(array: Self, mut f: F) -> Self::Output { 529 | let [$($i,)*] = array; 530 | [$(f($i),)*] 531 | } 532 | } 533 | impl ArrayZip<[U; $n]> for [T; $n] { 534 | type Output = [(T, U); $n]; 535 | fn zip(array: Self, other: [U; $n]) -> Self::Output { 536 | let [$($i,)*] = array; 537 | let [$($j,)*] = other; 538 | [$(($i,$j),)*] 539 | } 540 | } 541 | impl ArrayZipWith<[U; $n], F> for [T; $n] 542 | where F: $fn_trait(T, U) -> V 543 | { 544 | type Output = [V; $n]; 545 | fn zip_with(array: Self, other: [U; $n], mut f: F) -> Self::Output { 546 | let [$($i,)*] = array; 547 | let [$($j,)*] = other; 548 | [$(f($i,$j),)*] 549 | } 550 | } 551 | impl<'a, T: 'a> ArrayAsRef<'a> for [T; $n] 552 | { 553 | type Output = [&'a T; $n]; 554 | fn as_ref(array: &'a Self) -> Self::Output { 555 | let [$($i,)*] = array; 556 | [$($i,)*] 557 | } 558 | } 559 | impl<'a, T: 'a> ArrayAsMut<'a> for [T; $n] 560 | { 561 | type Output = [&'a mut T; $n]; 562 | fn as_mut(array: &'a mut Self) -> Self::Output { 563 | let [$($i,)*] = array; 564 | [$($i,)*] 565 | } 566 | } 567 | impl ArrayPush for [T; $n] { 568 | type Output = [T; $n+1]; 569 | fn push_back(array: Self, item: T) -> Self::Output { 570 | let [$($i,)*] = array; 571 | [$($i,)* item] 572 | } 573 | fn push_front(array: Self, item: T) -> Self::Output { 574 | let [$($i,)*] = array; 575 | [item, $($i,)*] 576 | } 577 | } 578 | impl ArrayPop for [T; $n+1] { 579 | type Output = [T; $n]; 580 | fn pop_back(array: Self) -> (Self::Output, T) { 581 | let [$($i,)* item] = array; 582 | ([$($i,)*], item) 583 | } 584 | fn pop_front(array: Self) -> (Self::Output, T) { 585 | let [item, $($i,)*] = array; 586 | ([$($i,)*], item) 587 | } 588 | } 589 | 590 | ) 591 | } 592 | 593 | // 594 | impl_for_size!(0; FnOnce => /); 595 | impl_for_size!(1; FnOnce => a0 / b0); 596 | impl_for_size!(2; FnMut => a0 a1 / b0 b1); 597 | impl_for_size!(3; FnMut => a0 a1 a2 / b0 b1 b2); 598 | impl_for_size!(4; FnMut => a0 a1 a2 a3 / b0 b1 b2 b3); 599 | impl_for_size!(5; FnMut => a0 a1 a2 a3 a4 / b0 b1 b2 b3 b4); 600 | impl_for_size!(6; FnMut => a0 a1 a2 a3 a4 a5 / b0 b1 b2 b3 b4 b5); 601 | impl_for_size!(7; FnMut => a0 a1 a2 a3 a4 a5 a6 / b0 b1 b2 b3 b4 b5 b6); 602 | impl_for_size!(8; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 / b0 b1 b2 b3 b4 b5 b6 b7); 603 | impl_for_size!(9; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 / b0 b1 b2 b3 b4 b5 b6 b7 b8); 604 | impl_for_size!(10; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9); 605 | impl_for_size!(11; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10); 606 | impl_for_size!(12; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11); 607 | impl_for_size!(13; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12); 608 | impl_for_size!(14; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13); 609 | impl_for_size!(15; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14); 610 | impl_for_size!(16; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15); 611 | impl_for_size!(17; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16); 612 | impl_for_size!(18; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17); 613 | impl_for_size!(19; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18); 614 | impl_for_size!(20; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19); 615 | impl_for_size!(21; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20); 616 | impl_for_size!(22; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21); 617 | impl_for_size!(23; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22); 618 | impl_for_size!(24; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23); 619 | impl_for_size!(25; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24); 620 | impl_for_size!(26; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25); 621 | impl_for_size!(27; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26); 622 | impl_for_size!(28; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27); 623 | impl_for_size!(29; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28); 624 | impl_for_size!(30; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29); 625 | impl_for_size!(31; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30); 626 | impl_for_size!(32; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30 b31); 627 | } 628 | 629 | #[cfg(test)] 630 | mod tests { 631 | use super::ArrayTools; 632 | 633 | #[test] 634 | fn it_works() { 635 | let mut a = [1]; 636 | *a.as_mut_array()[0] = 2; 637 | assert_eq!(a, [2]); 638 | 639 | a = ArrayTools::from_tuple((3,)); 640 | assert_eq!(a, [3]); 641 | assert_eq!(a.into_tuple(), (3,)); 642 | 643 | let a = a.map(|x| x as f32); 644 | assert_eq!(a, [3.0]); 645 | 646 | let a0: [u8; 0] = []; 647 | let a1 = a0.push_back(Default::default()); 648 | assert_eq!(a1, [0]); 649 | let a2 = a1.push_back(2); 650 | assert_eq!(a2, [0, 2]); 651 | let b1 = a2.pop_back(); 652 | assert_eq!(b1, ([0], 2)); 653 | 654 | let iota: [_; 10] = ArrayTools::indices(); 655 | assert_eq!(iota, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); 656 | 657 | let mut v = Vec::with_capacity(111); 658 | v.push(1); 659 | let a: [_; 3] = ArrayTools::repeat(v); 660 | assert_eq!(a[0], [1]); 661 | assert_eq!(a[1], [1]); 662 | assert_eq!(a[2], [1]); 663 | assert_eq!(a[0].capacity(), 1); 664 | assert_eq!(a[1].capacity(), 1); 665 | assert_eq!(a[2].capacity(), 111); 666 | 667 | let sums = [1, 2, 3].zip_with([30, 20, 10], std::ops::Add::add); 668 | assert_eq!(sums, [31, 22, 13]); 669 | } 670 | 671 | #[test] 672 | fn from_iter_is_not_ambiguous_with_std() { 673 | #[allow(unused_imports)] 674 | use std::iter::FromIterator; 675 | <[i16; 5]>::from_iter(1..); 676 | } 677 | } 678 | --------------------------------------------------------------------------------