1 | //! Utilities for the array primitive type. |
2 | //! |
3 | //! *[See also the array primitive type](array).* |
4 | |
5 | #![stable (feature = "core_array" , since = "1.35.0" )] |
6 | |
7 | use crate::borrow::{Borrow, BorrowMut}; |
8 | use crate::cmp::Ordering; |
9 | use crate::convert::Infallible; |
10 | use crate::error::Error; |
11 | use crate::fmt; |
12 | use crate::hash::{self, Hash}; |
13 | use crate::intrinsics::transmute_unchecked; |
14 | use crate::iter::{UncheckedIterator, repeat_n}; |
15 | use crate::mem::{self, MaybeUninit}; |
16 | use crate::ops::{ |
17 | ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try, |
18 | }; |
19 | use crate::ptr::{null, null_mut}; |
20 | use crate::slice::{Iter, IterMut}; |
21 | |
22 | mod ascii; |
23 | mod drain; |
24 | mod equality; |
25 | mod iter; |
26 | |
27 | pub(crate) use drain::drain_array_with; |
28 | #[stable (feature = "array_value_iter" , since = "1.51.0" )] |
29 | pub use iter::IntoIter; |
30 | |
31 | /// Creates an array of type `[T; N]` by repeatedly cloning a value. |
32 | /// |
33 | /// This is the same as `[val; N]`, but it also works for types that do not |
34 | /// implement [`Copy`]. |
35 | /// |
36 | /// The provided value will be used as an element of the resulting array and |
37 | /// will be cloned N - 1 times to fill up the rest. If N is zero, the value |
38 | /// will be dropped. |
39 | /// |
40 | /// # Example |
41 | /// |
42 | /// Creating multiple copies of a `String`: |
43 | /// ```rust |
44 | /// #![feature(array_repeat)] |
45 | /// |
46 | /// use std::array; |
47 | /// |
48 | /// let string = "Hello there!" .to_string(); |
49 | /// let strings = array::repeat(string); |
50 | /// assert_eq!(strings, ["Hello there!" , "Hello there!" ]); |
51 | /// ``` |
52 | #[inline ] |
53 | #[unstable (feature = "array_repeat" , issue = "126695" )] |
54 | pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] { |
55 | from_trusted_iterator(iter:repeat_n(element:val, N)) |
56 | } |
57 | |
58 | /// Creates an array where each element is produced by calling `f` with |
59 | /// that element's index while walking forward through the array. |
60 | /// |
61 | /// This is essentially the same as writing |
62 | /// ```text |
63 | /// [f(0), f(1), f(2), …, f(N - 2), f(N - 1)] |
64 | /// ``` |
65 | /// and is similar to `(0..i).map(f)`, just for arrays not iterators. |
66 | /// |
67 | /// If `N == 0`, this produces an empty array without ever calling `f`. |
68 | /// |
69 | /// # Example |
70 | /// |
71 | /// ```rust |
72 | /// // type inference is helping us here, the way `from_fn` knows how many |
73 | /// // elements to produce is the length of array down there: only arrays of |
74 | /// // equal lengths can be compared, so the const generic parameter `N` is |
75 | /// // inferred to be 5, thus creating array of 5 elements. |
76 | /// |
77 | /// let array = core::array::from_fn(|i| i); |
78 | /// // indexes are: 0 1 2 3 4 |
79 | /// assert_eq!(array, [0, 1, 2, 3, 4]); |
80 | /// |
81 | /// let array2: [usize; 8] = core::array::from_fn(|i| i * 2); |
82 | /// // indexes are: 0 1 2 3 4 5 6 7 |
83 | /// assert_eq!(array2, [0, 2, 4, 6, 8, 10, 12, 14]); |
84 | /// |
85 | /// let bool_arr = core::array::from_fn::<_, 5, _>(|i| i % 2 == 0); |
86 | /// // indexes are: 0 1 2 3 4 |
87 | /// assert_eq!(bool_arr, [true, false, true, false, true]); |
88 | /// ``` |
89 | /// |
90 | /// You can also capture things, for example to create an array full of clones |
91 | /// where you can't just use `[item; N]` because it's not `Copy`: |
92 | /// ``` |
93 | /// # // TBH `array::repeat` would be better for this, but it's not stable yet. |
94 | /// let my_string = String::from("Hello" ); |
95 | /// let clones: [String; 42] = std::array::from_fn(|_| my_string.clone()); |
96 | /// assert!(clones.iter().all(|x| *x == my_string)); |
97 | /// ``` |
98 | /// |
99 | /// The array is generated in ascending index order, starting from the front |
100 | /// and going towards the back, so you can use closures with mutable state: |
101 | /// ``` |
102 | /// let mut state = 1; |
103 | /// let a = std::array::from_fn(|_| { let x = state; state *= 2; x }); |
104 | /// assert_eq!(a, [1, 2, 4, 8, 16, 32]); |
105 | /// ``` |
106 | #[inline ] |
107 | #[stable (feature = "array_from_fn" , since = "1.63.0" )] |
108 | pub fn from_fn<T, const N: usize, F>(f: F) -> [T; N] |
109 | where |
110 | F: FnMut(usize) -> T, |
111 | { |
112 | try_from_fn(cb:NeverShortCircuit::wrap_mut_1(f)).0 |
113 | } |
114 | |
115 | /// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call. |
116 | /// Unlike [`from_fn`], where the element creation can't fail, this version will return an error |
117 | /// if any element creation was unsuccessful. |
118 | /// |
119 | /// The return type of this function depends on the return type of the closure. |
120 | /// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`. |
121 | /// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`. |
122 | /// |
123 | /// # Arguments |
124 | /// |
125 | /// * `cb`: Callback where the passed argument is the current array index. |
126 | /// |
127 | /// # Example |
128 | /// |
129 | /// ```rust |
130 | /// #![feature(array_try_from_fn)] |
131 | /// |
132 | /// let array: Result<[u8; 5], _> = std::array::try_from_fn(|i| i.try_into()); |
133 | /// assert_eq!(array, Ok([0, 1, 2, 3, 4])); |
134 | /// |
135 | /// let array: Result<[i8; 200], _> = std::array::try_from_fn(|i| i.try_into()); |
136 | /// assert!(array.is_err()); |
137 | /// |
138 | /// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_add(100)); |
139 | /// assert_eq!(array, Some([100, 101, 102, 103])); |
140 | /// |
141 | /// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_sub(100)); |
142 | /// assert_eq!(array, None); |
143 | /// ``` |
144 | #[inline ] |
145 | #[unstable (feature = "array_try_from_fn" , issue = "89379" )] |
146 | pub fn try_from_fn<R, const N: usize, F>(cb: F) -> ChangeOutputType<R, [R::Output; N]> |
147 | where |
148 | F: FnMut(usize) -> R, |
149 | R: Try, |
150 | R::Residual: Residual<[R::Output; N]>, |
151 | { |
152 | let mut array: [MaybeUninit>; N] = [const { MaybeUninit::uninit() }; N]; |
153 | match try_from_fn_erased(&mut array, generator:cb) { |
154 | ControlFlow::Break(r: impl Residual<[impl Residual<…>; N]>) => FromResidual::from_residual(r), |
155 | ControlFlow::Continue(()) => { |
156 | // SAFETY: All elements of the array were populated. |
157 | try { unsafe { MaybeUninit::array_assume_init(array) } } |
158 | } |
159 | } |
160 | } |
161 | |
162 | /// Converts a reference to `T` into a reference to an array of length 1 (without copying). |
163 | #[stable (feature = "array_from_ref" , since = "1.53.0" )] |
164 | #[rustc_const_stable (feature = "const_array_from_ref_shared" , since = "1.63.0" )] |
165 | pub const fn from_ref<T>(s: &T) -> &[T; 1] { |
166 | // SAFETY: Converting `&T` to `&[T; 1]` is sound. |
167 | unsafe { &*(s as *const T).cast::<[T; 1]>() } |
168 | } |
169 | |
170 | /// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying). |
171 | #[stable (feature = "array_from_ref" , since = "1.53.0" )] |
172 | #[rustc_const_stable (feature = "const_array_from_ref" , since = "1.83.0" )] |
173 | pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] { |
174 | // SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound. |
175 | unsafe { &mut *(s as *mut T).cast::<[T; 1]>() } |
176 | } |
177 | |
178 | /// The error type returned when a conversion from a slice to an array fails. |
179 | #[stable (feature = "try_from" , since = "1.34.0" )] |
180 | #[derive (Debug, Copy, Clone)] |
181 | pub struct TryFromSliceError(()); |
182 | |
183 | #[stable (feature = "core_array" , since = "1.35.0" )] |
184 | impl fmt::Display for TryFromSliceError { |
185 | #[inline ] |
186 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
187 | #[allow (deprecated)] |
188 | self.description().fmt(f) |
189 | } |
190 | } |
191 | |
192 | #[stable (feature = "try_from" , since = "1.34.0" )] |
193 | impl Error for TryFromSliceError { |
194 | #[allow (deprecated)] |
195 | fn description(&self) -> &str { |
196 | "could not convert slice to array" |
197 | } |
198 | } |
199 | |
200 | #[stable (feature = "try_from_slice_error" , since = "1.36.0" )] |
201 | impl From<Infallible> for TryFromSliceError { |
202 | fn from(x: Infallible) -> TryFromSliceError { |
203 | match x {} |
204 | } |
205 | } |
206 | |
207 | #[stable (feature = "rust1" , since = "1.0.0" )] |
208 | impl<T, const N: usize> AsRef<[T]> for [T; N] { |
209 | #[inline ] |
210 | fn as_ref(&self) -> &[T] { |
211 | &self[..] |
212 | } |
213 | } |
214 | |
215 | #[stable (feature = "rust1" , since = "1.0.0" )] |
216 | impl<T, const N: usize> AsMut<[T]> for [T; N] { |
217 | #[inline ] |
218 | fn as_mut(&mut self) -> &mut [T] { |
219 | &mut self[..] |
220 | } |
221 | } |
222 | |
223 | #[stable (feature = "array_borrow" , since = "1.4.0" )] |
224 | impl<T, const N: usize> Borrow<[T]> for [T; N] { |
225 | fn borrow(&self) -> &[T] { |
226 | self |
227 | } |
228 | } |
229 | |
230 | #[stable (feature = "array_borrow" , since = "1.4.0" )] |
231 | impl<T, const N: usize> BorrowMut<[T]> for [T; N] { |
232 | fn borrow_mut(&mut self) -> &mut [T] { |
233 | self |
234 | } |
235 | } |
236 | |
237 | /// Tries to create an array `[T; N]` by copying from a slice `&[T]`. |
238 | /// Succeeds if `slice.len() == N`. |
239 | /// |
240 | /// ``` |
241 | /// let bytes: [u8; 3] = [1, 0, 2]; |
242 | /// |
243 | /// let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&bytes[0..2]).unwrap(); |
244 | /// assert_eq!(1, u16::from_le_bytes(bytes_head)); |
245 | /// |
246 | /// let bytes_tail: [u8; 2] = bytes[1..3].try_into().unwrap(); |
247 | /// assert_eq!(512, u16::from_le_bytes(bytes_tail)); |
248 | /// ``` |
249 | #[stable (feature = "try_from" , since = "1.34.0" )] |
250 | impl<T, const N: usize> TryFrom<&[T]> for [T; N] |
251 | where |
252 | T: Copy, |
253 | { |
254 | type Error = TryFromSliceError; |
255 | |
256 | #[inline ] |
257 | fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> { |
258 | <&Self>::try_from(slice).copied() |
259 | } |
260 | } |
261 | |
262 | /// Tries to create an array `[T; N]` by copying from a mutable slice `&mut [T]`. |
263 | /// Succeeds if `slice.len() == N`. |
264 | /// |
265 | /// ``` |
266 | /// let mut bytes: [u8; 3] = [1, 0, 2]; |
267 | /// |
268 | /// let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&mut bytes[0..2]).unwrap(); |
269 | /// assert_eq!(1, u16::from_le_bytes(bytes_head)); |
270 | /// |
271 | /// let bytes_tail: [u8; 2] = (&mut bytes[1..3]).try_into().unwrap(); |
272 | /// assert_eq!(512, u16::from_le_bytes(bytes_tail)); |
273 | /// ``` |
274 | #[stable (feature = "try_from_mut_slice_to_array" , since = "1.59.0" )] |
275 | impl<T, const N: usize> TryFrom<&mut [T]> for [T; N] |
276 | where |
277 | T: Copy, |
278 | { |
279 | type Error = TryFromSliceError; |
280 | |
281 | #[inline ] |
282 | fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> { |
283 | <Self>::try_from(&*slice) |
284 | } |
285 | } |
286 | |
287 | /// Tries to create an array ref `&[T; N]` from a slice ref `&[T]`. Succeeds if |
288 | /// `slice.len() == N`. |
289 | /// |
290 | /// ``` |
291 | /// let bytes: [u8; 3] = [1, 0, 2]; |
292 | /// |
293 | /// let bytes_head: &[u8; 2] = <&[u8; 2]>::try_from(&bytes[0..2]).unwrap(); |
294 | /// assert_eq!(1, u16::from_le_bytes(*bytes_head)); |
295 | /// |
296 | /// let bytes_tail: &[u8; 2] = bytes[1..3].try_into().unwrap(); |
297 | /// assert_eq!(512, u16::from_le_bytes(*bytes_tail)); |
298 | /// ``` |
299 | #[stable (feature = "try_from" , since = "1.34.0" )] |
300 | impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] { |
301 | type Error = TryFromSliceError; |
302 | |
303 | #[inline ] |
304 | fn try_from(slice: &'a [T]) -> Result<&'a [T; N], TryFromSliceError> { |
305 | slice.as_array().ok_or(err:TryFromSliceError(())) |
306 | } |
307 | } |
308 | |
309 | /// Tries to create a mutable array ref `&mut [T; N]` from a mutable slice ref |
310 | /// `&mut [T]`. Succeeds if `slice.len() == N`. |
311 | /// |
312 | /// ``` |
313 | /// let mut bytes: [u8; 3] = [1, 0, 2]; |
314 | /// |
315 | /// let bytes_head: &mut [u8; 2] = <&mut [u8; 2]>::try_from(&mut bytes[0..2]).unwrap(); |
316 | /// assert_eq!(1, u16::from_le_bytes(*bytes_head)); |
317 | /// |
318 | /// let bytes_tail: &mut [u8; 2] = (&mut bytes[1..3]).try_into().unwrap(); |
319 | /// assert_eq!(512, u16::from_le_bytes(*bytes_tail)); |
320 | /// ``` |
321 | #[stable (feature = "try_from" , since = "1.34.0" )] |
322 | impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] { |
323 | type Error = TryFromSliceError; |
324 | |
325 | #[inline ] |
326 | fn try_from(slice: &'a mut [T]) -> Result<&'a mut [T; N], TryFromSliceError> { |
327 | slice.as_mut_array().ok_or(err:TryFromSliceError(())) |
328 | } |
329 | } |
330 | |
331 | /// The hash of an array is the same as that of the corresponding slice, |
332 | /// as required by the `Borrow` implementation. |
333 | /// |
334 | /// ``` |
335 | /// use std::hash::BuildHasher; |
336 | /// |
337 | /// let b = std::hash::RandomState::new(); |
338 | /// let a: [u8; 3] = [0xa8, 0x3c, 0x09]; |
339 | /// let s: &[u8] = &[0xa8, 0x3c, 0x09]; |
340 | /// assert_eq!(b.hash_one(a), b.hash_one(s)); |
341 | /// ``` |
342 | #[stable (feature = "rust1" , since = "1.0.0" )] |
343 | impl<T: Hash, const N: usize> Hash for [T; N] { |
344 | fn hash<H: hash::Hasher>(&self, state: &mut H) { |
345 | Hash::hash(&self[..], state) |
346 | } |
347 | } |
348 | |
349 | #[stable (feature = "rust1" , since = "1.0.0" )] |
350 | impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] { |
351 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
352 | fmt::Debug::fmt(&&self[..], f) |
353 | } |
354 | } |
355 | |
356 | #[stable (feature = "rust1" , since = "1.0.0" )] |
357 | impl<'a, T, const N: usize> IntoIterator for &'a [T; N] { |
358 | type Item = &'a T; |
359 | type IntoIter = Iter<'a, T>; |
360 | |
361 | fn into_iter(self) -> Iter<'a, T> { |
362 | self.iter() |
363 | } |
364 | } |
365 | |
366 | #[stable (feature = "rust1" , since = "1.0.0" )] |
367 | impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] { |
368 | type Item = &'a mut T; |
369 | type IntoIter = IterMut<'a, T>; |
370 | |
371 | fn into_iter(self) -> IterMut<'a, T> { |
372 | self.iter_mut() |
373 | } |
374 | } |
375 | |
376 | #[stable (feature = "index_trait_on_arrays" , since = "1.50.0" )] |
377 | impl<T, I, const N: usize> Index<I> for [T; N] |
378 | where |
379 | [T]: Index<I>, |
380 | { |
381 | type Output = <[T] as Index<I>>::Output; |
382 | |
383 | #[inline ] |
384 | fn index(&self, index: I) -> &Self::Output { |
385 | Index::index(self as &[T], index) |
386 | } |
387 | } |
388 | |
389 | #[stable (feature = "index_trait_on_arrays" , since = "1.50.0" )] |
390 | impl<T, I, const N: usize> IndexMut<I> for [T; N] |
391 | where |
392 | [T]: IndexMut<I>, |
393 | { |
394 | #[inline ] |
395 | fn index_mut(&mut self, index: I) -> &mut Self::Output { |
396 | IndexMut::index_mut(self as &mut [T], index) |
397 | } |
398 | } |
399 | |
400 | /// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison). |
401 | #[stable (feature = "rust1" , since = "1.0.0" )] |
402 | impl<T: PartialOrd, const N: usize> PartialOrd for [T; N] { |
403 | #[inline ] |
404 | fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> { |
405 | PartialOrd::partial_cmp(&&self[..], &&other[..]) |
406 | } |
407 | #[inline ] |
408 | fn lt(&self, other: &[T; N]) -> bool { |
409 | PartialOrd::lt(&&self[..], &&other[..]) |
410 | } |
411 | #[inline ] |
412 | fn le(&self, other: &[T; N]) -> bool { |
413 | PartialOrd::le(&&self[..], &&other[..]) |
414 | } |
415 | #[inline ] |
416 | fn ge(&self, other: &[T; N]) -> bool { |
417 | PartialOrd::ge(&&self[..], &&other[..]) |
418 | } |
419 | #[inline ] |
420 | fn gt(&self, other: &[T; N]) -> bool { |
421 | PartialOrd::gt(&&self[..], &&other[..]) |
422 | } |
423 | } |
424 | |
425 | /// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison). |
426 | #[stable (feature = "rust1" , since = "1.0.0" )] |
427 | impl<T: Ord, const N: usize> Ord for [T; N] { |
428 | #[inline ] |
429 | fn cmp(&self, other: &[T; N]) -> Ordering { |
430 | Ord::cmp(&&self[..], &&other[..]) |
431 | } |
432 | } |
433 | |
434 | #[stable (feature = "copy_clone_array_lib" , since = "1.58.0" )] |
435 | impl<T: Copy, const N: usize> Copy for [T; N] {} |
436 | |
437 | #[stable (feature = "copy_clone_array_lib" , since = "1.58.0" )] |
438 | impl<T: Clone, const N: usize> Clone for [T; N] { |
439 | #[inline ] |
440 | fn clone(&self) -> Self { |
441 | SpecArrayClone::clone(self) |
442 | } |
443 | |
444 | #[inline ] |
445 | fn clone_from(&mut self, other: &Self) { |
446 | self.clone_from_slice(src:other); |
447 | } |
448 | } |
449 | |
450 | trait SpecArrayClone: Clone { |
451 | fn clone<const N: usize>(array: &[Self; N]) -> [Self; N]; |
452 | } |
453 | |
454 | impl<T: Clone> SpecArrayClone for T { |
455 | #[inline ] |
456 | default fn clone<const N: usize>(array: &[T; N]) -> [T; N] { |
457 | from_trusted_iterator(iter:array.iter().cloned()) |
458 | } |
459 | } |
460 | |
461 | impl<T: Copy> SpecArrayClone for T { |
462 | #[inline ] |
463 | fn clone<const N: usize>(array: &[T; N]) -> [T; N] { |
464 | *array |
465 | } |
466 | } |
467 | |
468 | // The Default impls cannot be done with const generics because `[T; 0]` doesn't |
469 | // require Default to be implemented, and having different impl blocks for |
470 | // different numbers isn't supported yet. |
471 | |
472 | macro_rules! array_impl_default { |
473 | {$n:expr, $t:ident $($ts:ident)*} => { |
474 | #[stable(since = "1.4.0" , feature = "array_default" )] |
475 | impl<T> Default for [T; $n] where T: Default { |
476 | fn default() -> [T; $n] { |
477 | [$t::default(), $($ts::default()),*] |
478 | } |
479 | } |
480 | array_impl_default!{($n - 1), $($ts)*} |
481 | }; |
482 | {$n:expr,} => { |
483 | #[stable(since = "1.4.0" , feature = "array_default" )] |
484 | impl<T> Default for [T; $n] { |
485 | fn default() -> [T; $n] { [] } |
486 | } |
487 | }; |
488 | } |
489 | |
490 | array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} |
491 | |
492 | impl<T, const N: usize> [T; N] { |
493 | /// Returns an array of the same size as `self`, with function `f` applied to each element |
494 | /// in order. |
495 | /// |
496 | /// If you don't necessarily need a new fixed-size array, consider using |
497 | /// [`Iterator::map`] instead. |
498 | /// |
499 | /// |
500 | /// # Note on performance and stack usage |
501 | /// |
502 | /// Unfortunately, usages of this method are currently not always optimized |
503 | /// as well as they could be. This mainly concerns large arrays, as mapping |
504 | /// over small arrays seem to be optimized just fine. Also note that in |
505 | /// debug mode (i.e. without any optimizations), this method can use a lot |
506 | /// of stack space (a few times the size of the array or more). |
507 | /// |
508 | /// Therefore, in performance-critical code, try to avoid using this method |
509 | /// on large arrays or check the emitted code. Also try to avoid chained |
510 | /// maps (e.g. `arr.map(...).map(...)`). |
511 | /// |
512 | /// In many cases, you can instead use [`Iterator::map`] by calling `.iter()` |
513 | /// or `.into_iter()` on your array. `[T; N]::map` is only necessary if you |
514 | /// really need a new array of the same size as the result. Rust's lazy |
515 | /// iterators tend to get optimized very well. |
516 | /// |
517 | /// |
518 | /// # Examples |
519 | /// |
520 | /// ``` |
521 | /// let x = [1, 2, 3]; |
522 | /// let y = x.map(|v| v + 1); |
523 | /// assert_eq!(y, [2, 3, 4]); |
524 | /// |
525 | /// let x = [1, 2, 3]; |
526 | /// let mut temp = 0; |
527 | /// let y = x.map(|v| { temp += 1; v * temp }); |
528 | /// assert_eq!(y, [1, 4, 9]); |
529 | /// |
530 | /// let x = ["Ferris" , "Bueller's" , "Day" , "Off" ]; |
531 | /// let y = x.map(|v| v.len()); |
532 | /// assert_eq!(y, [6, 9, 3, 3]); |
533 | /// ``` |
534 | #[must_use ] |
535 | #[stable (feature = "array_map" , since = "1.55.0" )] |
536 | pub fn map<F, U>(self, f: F) -> [U; N] |
537 | where |
538 | F: FnMut(T) -> U, |
539 | { |
540 | self.try_map(NeverShortCircuit::wrap_mut_1(f)).0 |
541 | } |
542 | |
543 | /// A fallible function `f` applied to each element on array `self` in order to |
544 | /// return an array the same size as `self` or the first error encountered. |
545 | /// |
546 | /// The return type of this function depends on the return type of the closure. |
547 | /// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`. |
548 | /// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`. |
549 | /// |
550 | /// # Examples |
551 | /// |
552 | /// ``` |
553 | /// #![feature(array_try_map)] |
554 | /// |
555 | /// let a = ["1" , "2" , "3" ]; |
556 | /// let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1); |
557 | /// assert_eq!(b, [2, 3, 4]); |
558 | /// |
559 | /// let a = ["1" , "2a" , "3" ]; |
560 | /// let b = a.try_map(|v| v.parse::<u32>()); |
561 | /// assert!(b.is_err()); |
562 | /// |
563 | /// use std::num::NonZero; |
564 | /// |
565 | /// let z = [1, 2, 0, 3, 4]; |
566 | /// assert_eq!(z.try_map(NonZero::new), None); |
567 | /// |
568 | /// let a = [1, 2, 3]; |
569 | /// let b = a.try_map(NonZero::new); |
570 | /// let c = b.map(|x| x.map(NonZero::get)); |
571 | /// assert_eq!(c, Some(a)); |
572 | /// ``` |
573 | #[unstable (feature = "array_try_map" , issue = "79711" )] |
574 | pub fn try_map<R>(self, f: impl FnMut(T) -> R) -> ChangeOutputType<R, [R::Output; N]> |
575 | where |
576 | R: Try<Residual: Residual<[R::Output; N]>>, |
577 | { |
578 | drain_array_with(self, |iter| try_from_trusted_iterator(iter.map(f))) |
579 | } |
580 | |
581 | /// Returns a slice containing the entire array. Equivalent to `&s[..]`. |
582 | #[stable (feature = "array_as_slice" , since = "1.57.0" )] |
583 | #[rustc_const_stable (feature = "array_as_slice" , since = "1.57.0" )] |
584 | pub const fn as_slice(&self) -> &[T] { |
585 | self |
586 | } |
587 | |
588 | /// Returns a mutable slice containing the entire array. Equivalent to |
589 | /// `&mut s[..]`. |
590 | #[stable (feature = "array_as_slice" , since = "1.57.0" )] |
591 | #[rustc_const_stable (feature = "const_array_as_mut_slice" , since = "CURRENT_RUSTC_VERSION" )] |
592 | pub const fn as_mut_slice(&mut self) -> &mut [T] { |
593 | self |
594 | } |
595 | |
596 | /// Borrows each element and returns an array of references with the same |
597 | /// size as `self`. |
598 | /// |
599 | /// |
600 | /// # Example |
601 | /// |
602 | /// ``` |
603 | /// let floats = [3.1, 2.7, -1.0]; |
604 | /// let float_refs: [&f64; 3] = floats.each_ref(); |
605 | /// assert_eq!(float_refs, [&3.1, &2.7, &-1.0]); |
606 | /// ``` |
607 | /// |
608 | /// This method is particularly useful if combined with other methods, like |
609 | /// [`map`](#method.map). This way, you can avoid moving the original |
610 | /// array if its elements are not [`Copy`]. |
611 | /// |
612 | /// ``` |
613 | /// let strings = ["Ferris" .to_string(), "♥" .to_string(), "Rust" .to_string()]; |
614 | /// let is_ascii = strings.each_ref().map(|s| s.is_ascii()); |
615 | /// assert_eq!(is_ascii, [true, false, true]); |
616 | /// |
617 | /// // We can still access the original array: it has not been moved. |
618 | /// assert_eq!(strings.len(), 3); |
619 | /// ``` |
620 | #[stable (feature = "array_methods" , since = "1.77.0" )] |
621 | #[rustc_const_unstable (feature = "const_array_each_ref" , issue = "133289" )] |
622 | pub const fn each_ref(&self) -> [&T; N] { |
623 | let mut buf = [null::<T>(); N]; |
624 | |
625 | // FIXME(const-hack): We would like to simply use iterators for this (as in the original implementation), but this is not allowed in constant expressions. |
626 | let mut i = 0; |
627 | while i < N { |
628 | buf[i] = &raw const self[i]; |
629 | |
630 | i += 1; |
631 | } |
632 | |
633 | // SAFETY: `*const T` has the same layout as `&T`, and we've also initialised each pointer as a valid reference. |
634 | unsafe { transmute_unchecked(buf) } |
635 | } |
636 | |
637 | /// Borrows each element mutably and returns an array of mutable references |
638 | /// with the same size as `self`. |
639 | /// |
640 | /// |
641 | /// # Example |
642 | /// |
643 | /// ``` |
644 | /// |
645 | /// let mut floats = [3.1, 2.7, -1.0]; |
646 | /// let float_refs: [&mut f64; 3] = floats.each_mut(); |
647 | /// *float_refs[0] = 0.0; |
648 | /// assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]); |
649 | /// assert_eq!(floats, [0.0, 2.7, -1.0]); |
650 | /// ``` |
651 | #[stable (feature = "array_methods" , since = "1.77.0" )] |
652 | #[rustc_const_unstable (feature = "const_array_each_ref" , issue = "133289" )] |
653 | pub const fn each_mut(&mut self) -> [&mut T; N] { |
654 | let mut buf = [null_mut::<T>(); N]; |
655 | |
656 | // FIXME(const-hack): We would like to simply use iterators for this (as in the original implementation), but this is not allowed in constant expressions. |
657 | let mut i = 0; |
658 | while i < N { |
659 | buf[i] = &raw mut self[i]; |
660 | |
661 | i += 1; |
662 | } |
663 | |
664 | // SAFETY: `*mut T` has the same layout as `&mut T`, and we've also initialised each pointer as a valid reference. |
665 | unsafe { transmute_unchecked(buf) } |
666 | } |
667 | |
668 | /// Divides one array reference into two at an index. |
669 | /// |
670 | /// The first will contain all indices from `[0, M)` (excluding |
671 | /// the index `M` itself) and the second will contain all |
672 | /// indices from `[M, N)` (excluding the index `N` itself). |
673 | /// |
674 | /// # Panics |
675 | /// |
676 | /// Panics if `M > N`. |
677 | /// |
678 | /// # Examples |
679 | /// |
680 | /// ``` |
681 | /// #![feature(split_array)] |
682 | /// |
683 | /// let v = [1, 2, 3, 4, 5, 6]; |
684 | /// |
685 | /// { |
686 | /// let (left, right) = v.split_array_ref::<0>(); |
687 | /// assert_eq!(left, &[]); |
688 | /// assert_eq!(right, &[1, 2, 3, 4, 5, 6]); |
689 | /// } |
690 | /// |
691 | /// { |
692 | /// let (left, right) = v.split_array_ref::<2>(); |
693 | /// assert_eq!(left, &[1, 2]); |
694 | /// assert_eq!(right, &[3, 4, 5, 6]); |
695 | /// } |
696 | /// |
697 | /// { |
698 | /// let (left, right) = v.split_array_ref::<6>(); |
699 | /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]); |
700 | /// assert_eq!(right, &[]); |
701 | /// } |
702 | /// ``` |
703 | #[unstable ( |
704 | feature = "split_array" , |
705 | reason = "return type should have array as 2nd element" , |
706 | issue = "90091" |
707 | )] |
708 | #[inline ] |
709 | pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) { |
710 | (&self[..]).split_first_chunk::<M>().unwrap() |
711 | } |
712 | |
713 | /// Divides one mutable array reference into two at an index. |
714 | /// |
715 | /// The first will contain all indices from `[0, M)` (excluding |
716 | /// the index `M` itself) and the second will contain all |
717 | /// indices from `[M, N)` (excluding the index `N` itself). |
718 | /// |
719 | /// # Panics |
720 | /// |
721 | /// Panics if `M > N`. |
722 | /// |
723 | /// # Examples |
724 | /// |
725 | /// ``` |
726 | /// #![feature(split_array)] |
727 | /// |
728 | /// let mut v = [1, 0, 3, 0, 5, 6]; |
729 | /// let (left, right) = v.split_array_mut::<2>(); |
730 | /// assert_eq!(left, &mut [1, 0][..]); |
731 | /// assert_eq!(right, &mut [3, 0, 5, 6]); |
732 | /// left[1] = 2; |
733 | /// right[1] = 4; |
734 | /// assert_eq!(v, [1, 2, 3, 4, 5, 6]); |
735 | /// ``` |
736 | #[unstable ( |
737 | feature = "split_array" , |
738 | reason = "return type should have array as 2nd element" , |
739 | issue = "90091" |
740 | )] |
741 | #[inline ] |
742 | pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) { |
743 | (&mut self[..]).split_first_chunk_mut::<M>().unwrap() |
744 | } |
745 | |
746 | /// Divides one array reference into two at an index from the end. |
747 | /// |
748 | /// The first will contain all indices from `[0, N - M)` (excluding |
749 | /// the index `N - M` itself) and the second will contain all |
750 | /// indices from `[N - M, N)` (excluding the index `N` itself). |
751 | /// |
752 | /// # Panics |
753 | /// |
754 | /// Panics if `M > N`. |
755 | /// |
756 | /// # Examples |
757 | /// |
758 | /// ``` |
759 | /// #![feature(split_array)] |
760 | /// |
761 | /// let v = [1, 2, 3, 4, 5, 6]; |
762 | /// |
763 | /// { |
764 | /// let (left, right) = v.rsplit_array_ref::<0>(); |
765 | /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]); |
766 | /// assert_eq!(right, &[]); |
767 | /// } |
768 | /// |
769 | /// { |
770 | /// let (left, right) = v.rsplit_array_ref::<2>(); |
771 | /// assert_eq!(left, &[1, 2, 3, 4]); |
772 | /// assert_eq!(right, &[5, 6]); |
773 | /// } |
774 | /// |
775 | /// { |
776 | /// let (left, right) = v.rsplit_array_ref::<6>(); |
777 | /// assert_eq!(left, &[]); |
778 | /// assert_eq!(right, &[1, 2, 3, 4, 5, 6]); |
779 | /// } |
780 | /// ``` |
781 | #[unstable ( |
782 | feature = "split_array" , |
783 | reason = "return type should have array as 2nd element" , |
784 | issue = "90091" |
785 | )] |
786 | #[inline ] |
787 | pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) { |
788 | (&self[..]).split_last_chunk::<M>().unwrap() |
789 | } |
790 | |
791 | /// Divides one mutable array reference into two at an index from the end. |
792 | /// |
793 | /// The first will contain all indices from `[0, N - M)` (excluding |
794 | /// the index `N - M` itself) and the second will contain all |
795 | /// indices from `[N - M, N)` (excluding the index `N` itself). |
796 | /// |
797 | /// # Panics |
798 | /// |
799 | /// Panics if `M > N`. |
800 | /// |
801 | /// # Examples |
802 | /// |
803 | /// ``` |
804 | /// #![feature(split_array)] |
805 | /// |
806 | /// let mut v = [1, 0, 3, 0, 5, 6]; |
807 | /// let (left, right) = v.rsplit_array_mut::<4>(); |
808 | /// assert_eq!(left, &mut [1, 0]); |
809 | /// assert_eq!(right, &mut [3, 0, 5, 6][..]); |
810 | /// left[1] = 2; |
811 | /// right[1] = 4; |
812 | /// assert_eq!(v, [1, 2, 3, 4, 5, 6]); |
813 | /// ``` |
814 | #[unstable ( |
815 | feature = "split_array" , |
816 | reason = "return type should have array as 2nd element" , |
817 | issue = "90091" |
818 | )] |
819 | #[inline ] |
820 | pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) { |
821 | (&mut self[..]).split_last_chunk_mut::<M>().unwrap() |
822 | } |
823 | } |
824 | |
825 | /// Populate an array from the first `N` elements of `iter` |
826 | /// |
827 | /// # Panics |
828 | /// |
829 | /// If the iterator doesn't actually have enough items. |
830 | /// |
831 | /// By depending on `TrustedLen`, however, we can do that check up-front (where |
832 | /// it easily optimizes away) so it doesn't impact the loop that fills the array. |
833 | #[inline ] |
834 | fn from_trusted_iterator<T, const N: usize>(iter: impl UncheckedIterator<Item = T>) -> [T; N] { |
835 | try_from_trusted_iterator(iter.map(NeverShortCircuit)).0 |
836 | } |
837 | |
838 | #[inline ] |
839 | fn try_from_trusted_iterator<T, R, const N: usize>( |
840 | iter: impl UncheckedIterator<Item = R>, |
841 | ) -> ChangeOutputType<R, [T; N]> |
842 | where |
843 | R: Try<Output = T>, |
844 | R::Residual: Residual<[T; N]>, |
845 | { |
846 | assert!(iter.size_hint().0 >= N); |
847 | fn next<T>(mut iter: impl UncheckedIterator<Item = T>) -> impl FnMut(usize) -> T { |
848 | move |_| { |
849 | // SAFETY: We know that `from_fn` will call this at most N times, |
850 | // and we checked to ensure that we have at least that many items. |
851 | unsafe { iter.next_unchecked() } |
852 | } |
853 | } |
854 | |
855 | try_from_fn(cb:next(iter)) |
856 | } |
857 | |
858 | /// Version of [`try_from_fn`] using a passed-in slice in order to avoid |
859 | /// needing to monomorphize for every array length. |
860 | /// |
861 | /// This takes a generator rather than an iterator so that *at the type level* |
862 | /// it never needs to worry about running out of items. When combined with |
863 | /// an infallible `Try` type, that means the loop canonicalizes easily, allowing |
864 | /// it to optimize well. |
865 | /// |
866 | /// It would be *possible* to unify this and [`iter_next_chunk_erased`] into one |
867 | /// function that does the union of both things, but last time it was that way |
868 | /// it resulted in poor codegen from the "are there enough source items?" checks |
869 | /// not optimizing away. So if you give it a shot, make sure to watch what |
870 | /// happens in the codegen tests. |
871 | #[inline ] |
872 | fn try_from_fn_erased<T, R>( |
873 | buffer: &mut [MaybeUninit<T>], |
874 | mut generator: impl FnMut(usize) -> R, |
875 | ) -> ControlFlow<R::Residual> |
876 | where |
877 | R: Try<Output = T>, |
878 | { |
879 | let mut guard: Guard<'_, T> = Guard { array_mut: buffer, initialized: 0 }; |
880 | |
881 | while guard.initialized < guard.array_mut.len() { |
882 | let item: T = generator(guard.initialized).branch()?; |
883 | |
884 | // SAFETY: The loop condition ensures we have space to push the item |
885 | unsafe { guard.push_unchecked(item) }; |
886 | } |
887 | |
888 | mem::forget(guard); |
889 | ControlFlow::Continue(()) |
890 | } |
891 | |
892 | /// Panic guard for incremental initialization of arrays. |
893 | /// |
894 | /// Disarm the guard with `mem::forget` once the array has been initialized. |
895 | /// |
896 | /// # Safety |
897 | /// |
898 | /// All write accesses to this structure are unsafe and must maintain a correct |
899 | /// count of `initialized` elements. |
900 | /// |
901 | /// To minimize indirection fields are still pub but callers should at least use |
902 | /// `push_unchecked` to signal that something unsafe is going on. |
903 | struct Guard<'a, T> { |
904 | /// The array to be initialized. |
905 | pub array_mut: &'a mut [MaybeUninit<T>], |
906 | /// The number of items that have been initialized so far. |
907 | pub initialized: usize, |
908 | } |
909 | |
910 | impl<T> Guard<'_, T> { |
911 | /// Adds an item to the array and updates the initialized item counter. |
912 | /// |
913 | /// # Safety |
914 | /// |
915 | /// No more than N elements must be initialized. |
916 | #[inline ] |
917 | pub(crate) unsafe fn push_unchecked(&mut self, item: T) { |
918 | // SAFETY: If `initialized` was correct before and the caller does not |
919 | // invoke this method more than N times then writes will be in-bounds |
920 | // and slots will not be initialized more than once. |
921 | unsafe { |
922 | self.array_mut.get_unchecked_mut(self.initialized).write(val:item); |
923 | self.initialized = self.initialized.unchecked_add(1); |
924 | } |
925 | } |
926 | } |
927 | |
928 | impl<T> Drop for Guard<'_, T> { |
929 | #[inline ] |
930 | fn drop(&mut self) { |
931 | debug_assert!(self.initialized <= self.array_mut.len()); |
932 | |
933 | // SAFETY: this slice will contain only initialized objects. |
934 | unsafe { |
935 | self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop(); |
936 | } |
937 | } |
938 | } |
939 | |
940 | /// Pulls `N` items from `iter` and returns them as an array. If the iterator |
941 | /// yields fewer than `N` items, `Err` is returned containing an iterator over |
942 | /// the already yielded items. |
943 | /// |
944 | /// Since the iterator is passed as a mutable reference and this function calls |
945 | /// `next` at most `N` times, the iterator can still be used afterwards to |
946 | /// retrieve the remaining items. |
947 | /// |
948 | /// If `iter.next()` panicks, all items already yielded by the iterator are |
949 | /// dropped. |
950 | /// |
951 | /// Used for [`Iterator::next_chunk`]. |
952 | #[inline ] |
953 | pub(crate) fn iter_next_chunk<T, const N: usize>( |
954 | iter: &mut impl Iterator<Item = T>, |
955 | ) -> Result<[T; N], IntoIter<T, N>> { |
956 | let mut array: [MaybeUninit; N] = [const { MaybeUninit::uninit() }; N]; |
957 | let r: Result<(), usize> = iter_next_chunk_erased(&mut array, iter); |
958 | match r { |
959 | Ok(()) => { |
960 | // SAFETY: All elements of `array` were populated. |
961 | Ok(unsafe { MaybeUninit::array_assume_init(array) }) |
962 | } |
963 | Err(initialized: usize) => { |
964 | // SAFETY: Only the first `initialized` elements were populated |
965 | Err(unsafe { IntoIter::new_unchecked(buffer:array, initialized:0..initialized) }) |
966 | } |
967 | } |
968 | } |
969 | |
970 | /// Version of [`iter_next_chunk`] using a passed-in slice in order to avoid |
971 | /// needing to monomorphize for every array length. |
972 | /// |
973 | /// Unfortunately this loop has two exit conditions, the buffer filling up |
974 | /// or the iterator running out of items, making it tend to optimize poorly. |
975 | #[inline ] |
976 | fn iter_next_chunk_erased<T>( |
977 | buffer: &mut [MaybeUninit<T>], |
978 | iter: &mut impl Iterator<Item = T>, |
979 | ) -> Result<(), usize> { |
980 | let mut guard: Guard<'_, T> = Guard { array_mut: buffer, initialized: 0 }; |
981 | while guard.initialized < guard.array_mut.len() { |
982 | let Some(item: T) = iter.next() else { |
983 | // Unlike `try_from_fn_erased`, we want to keep the partial results, |
984 | // so we need to defuse the guard instead of using `?`. |
985 | let initialized: usize = guard.initialized; |
986 | mem::forget(guard); |
987 | return Err(initialized); |
988 | }; |
989 | |
990 | // SAFETY: The loop condition ensures we have space to push the item |
991 | unsafe { guard.push_unchecked(item) }; |
992 | } |
993 | |
994 | mem::forget(guard); |
995 | Ok(()) |
996 | } |
997 | |