]>
Commit | Line | Data |
---|---|---|
f2b60f7d | 1 | //! Utilities for the array primitive type. |
c1a9b12d | 2 | //! |
6a06907d | 3 | //! *[See also the array primitive type](array).* |
1a4d82fc | 4 | |
532ac7d7 | 5 | #![stable(feature = "core_array", since = "1.36.0")] |
c34b1796 | 6 | |
48663c56 XL |
7 | use crate::borrow::{Borrow, BorrowMut}; |
8 | use crate::cmp::Ordering; | |
9 | use crate::convert::{Infallible, TryFrom}; | |
f2b60f7d FG |
10 | #[cfg(not(bootstrap))] |
11 | use crate::error::Error; | |
48663c56 | 12 | use crate::fmt; |
dfeec247 | 13 | use crate::hash::{self, Hash}; |
6a06907d | 14 | use crate::iter::TrustedLen; |
6a06907d | 15 | use crate::mem::{self, MaybeUninit}; |
a2a8927a XL |
16 | use crate::ops::{ |
17 | ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try, | |
18 | }; | |
48663c56 | 19 | use crate::slice::{Iter, IterMut}; |
1a4d82fc | 20 | |
136023e0 | 21 | mod equality; |
e74abb32 XL |
22 | mod iter; |
23 | ||
5869c6ff | 24 | #[stable(feature = "array_value_iter", since = "1.51.0")] |
e74abb32 XL |
25 | pub use iter::IntoIter; |
26 | ||
c295e0f8 XL |
27 | /// Creates an array `[T; N]` where each array element `T` is returned by the `cb` call. |
28 | /// | |
29 | /// # Arguments | |
30 | /// | |
31 | /// * `cb`: Callback where the passed argument is the current array index. | |
32 | /// | |
33 | /// # Example | |
34 | /// | |
35 | /// ```rust | |
c295e0f8 XL |
36 | /// let array = core::array::from_fn(|i| i); |
37 | /// assert_eq!(array, [0, 1, 2, 3, 4]); | |
38 | /// ``` | |
39 | #[inline] | |
923072b8 FG |
40 | #[stable(feature = "array_from_fn", since = "1.63.0")] |
41 | pub fn from_fn<T, const N: usize, F>(mut cb: F) -> [T; N] | |
c295e0f8 XL |
42 | where |
43 | F: FnMut(usize) -> T, | |
44 | { | |
45 | let mut idx = 0; | |
46 | [(); N].map(|_| { | |
47 | let res = cb(idx); | |
48 | idx += 1; | |
49 | res | |
50 | }) | |
51 | } | |
52 | ||
53 | /// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call. | |
a2a8927a | 54 | /// Unlike [`from_fn`], where the element creation can't fail, this version will return an error |
c295e0f8 XL |
55 | /// if any element creation was unsuccessful. |
56 | /// | |
a2a8927a XL |
57 | /// The return type of this function depends on the return type of the closure. |
58 | /// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N]; E>`. | |
59 | /// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`. | |
60 | /// | |
c295e0f8 XL |
61 | /// # Arguments |
62 | /// | |
63 | /// * `cb`: Callback where the passed argument is the current array index. | |
64 | /// | |
65 | /// # Example | |
66 | /// | |
67 | /// ```rust | |
923072b8 | 68 | /// #![feature(array_try_from_fn)] |
c295e0f8 | 69 | /// |
a2a8927a | 70 | /// let array: Result<[u8; 5], _> = std::array::try_from_fn(|i| i.try_into()); |
c295e0f8 XL |
71 | /// assert_eq!(array, Ok([0, 1, 2, 3, 4])); |
72 | /// | |
a2a8927a XL |
73 | /// let array: Result<[i8; 200], _> = std::array::try_from_fn(|i| i.try_into()); |
74 | /// assert!(array.is_err()); | |
75 | /// | |
76 | /// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_add(100)); | |
77 | /// assert_eq!(array, Some([100, 101, 102, 103])); | |
78 | /// | |
79 | /// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_sub(100)); | |
80 | /// assert_eq!(array, None); | |
c295e0f8 XL |
81 | /// ``` |
82 | #[inline] | |
923072b8 FG |
83 | #[unstable(feature = "array_try_from_fn", issue = "89379")] |
84 | pub fn try_from_fn<R, const N: usize, F>(cb: F) -> ChangeOutputType<R, [R::Output; N]> | |
c295e0f8 | 85 | where |
a2a8927a XL |
86 | F: FnMut(usize) -> R, |
87 | R: Try, | |
88 | R::Residual: Residual<[R::Output; N]>, | |
c295e0f8 XL |
89 | { |
90 | // SAFETY: we know for certain that this iterator will yield exactly `N` | |
91 | // items. | |
a2a8927a | 92 | unsafe { try_collect_into_array_unchecked(&mut (0..N).map(cb)) } |
c295e0f8 XL |
93 | } |
94 | ||
1b1a35ee | 95 | /// Converts a reference to `T` into a reference to an array of length 1 (without copying). |
cdc7bbd5 | 96 | #[stable(feature = "array_from_ref", since = "1.53.0")] |
923072b8 | 97 | #[rustc_const_stable(feature = "const_array_from_ref_shared", since = "1.63.0")] |
3c0e092e | 98 | pub const fn from_ref<T>(s: &T) -> &[T; 1] { |
1b1a35ee XL |
99 | // SAFETY: Converting `&T` to `&[T; 1]` is sound. |
100 | unsafe { &*(s as *const T).cast::<[T; 1]>() } | |
101 | } | |
102 | ||
103 | /// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying). | |
cdc7bbd5 | 104 | #[stable(feature = "array_from_ref", since = "1.53.0")] |
3c0e092e XL |
105 | #[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")] |
106 | pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] { | |
1b1a35ee XL |
107 | // SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound. |
108 | unsafe { &mut *(s as *mut T).cast::<[T; 1]>() } | |
109 | } | |
110 | ||
abe05a73 | 111 | /// The error type returned when a conversion from a slice to an array fails. |
9fa01778 | 112 | #[stable(feature = "try_from", since = "1.34.0")] |
abe05a73 XL |
113 | #[derive(Debug, Copy, Clone)] |
114 | pub struct TryFromSliceError(()); | |
115 | ||
532ac7d7 | 116 | #[stable(feature = "core_array", since = "1.36.0")] |
abe05a73 XL |
117 | impl fmt::Display for TryFromSliceError { |
118 | #[inline] | |
48663c56 | 119 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
abe05a73 XL |
120 | fmt::Display::fmt(self.__description(), f) |
121 | } | |
122 | } | |
123 | ||
f2b60f7d FG |
124 | #[cfg(not(bootstrap))] |
125 | #[stable(feature = "try_from", since = "1.34.0")] | |
126 | impl Error for TryFromSliceError { | |
127 | #[allow(deprecated)] | |
128 | fn description(&self) -> &str { | |
129 | self.__description() | |
130 | } | |
131 | } | |
132 | ||
abe05a73 | 133 | impl TryFromSliceError { |
dfeec247 XL |
134 | #[unstable( |
135 | feature = "array_error_internals", | |
136 | reason = "available through Error trait and this method should not \ | |
abe05a73 | 137 | be exposed publicly", |
dfeec247 XL |
138 | issue = "none" |
139 | )] | |
abe05a73 XL |
140 | #[inline] |
141 | #[doc(hidden)] | |
142 | pub fn __description(&self) -> &str { | |
143 | "could not convert slice to array" | |
144 | } | |
145 | } | |
146 | ||
48663c56 | 147 | #[stable(feature = "try_from_slice_error", since = "1.36.0")] |
3c0e092e XL |
148 | #[rustc_const_unstable(feature = "const_convert", issue = "88674")] |
149 | impl const From<Infallible> for TryFromSliceError { | |
48663c56 XL |
150 | fn from(x: Infallible) -> TryFromSliceError { |
151 | match x {} | |
152 | } | |
153 | } | |
154 | ||
416331ca | 155 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 156 | impl<T, const N: usize> AsRef<[T]> for [T; N] { |
416331ca XL |
157 | #[inline] |
158 | fn as_ref(&self) -> &[T] { | |
159 | &self[..] | |
160 | } | |
161 | } | |
162 | ||
163 | #[stable(feature = "rust1", since = "1.0.0")] | |
3dfed10e | 164 | impl<T, const N: usize> AsMut<[T]> for [T; N] { |
416331ca XL |
165 | #[inline] |
166 | fn as_mut(&mut self) -> &mut [T] { | |
167 | &mut self[..] | |
168 | } | |
169 | } | |
170 | ||
171 | #[stable(feature = "array_borrow", since = "1.4.0")] | |
a2a8927a XL |
172 | #[rustc_const_unstable(feature = "const_borrow", issue = "91522")] |
173 | impl<T, const N: usize> const Borrow<[T]> for [T; N] { | |
416331ca XL |
174 | fn borrow(&self) -> &[T] { |
175 | self | |
176 | } | |
177 | } | |
178 | ||
179 | #[stable(feature = "array_borrow", since = "1.4.0")] | |
a2a8927a XL |
180 | #[rustc_const_unstable(feature = "const_borrow", issue = "91522")] |
181 | impl<T, const N: usize> const BorrowMut<[T]> for [T; N] { | |
416331ca XL |
182 | fn borrow_mut(&mut self) -> &mut [T] { |
183 | self | |
184 | } | |
185 | } | |
186 | ||
187 | #[stable(feature = "try_from", since = "1.34.0")] | |
188 | impl<T, const N: usize> TryFrom<&[T]> for [T; N] | |
189 | where | |
190 | T: Copy, | |
416331ca XL |
191 | { |
192 | type Error = TryFromSliceError; | |
193 | ||
194 | fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> { | |
195 | <&Self>::try_from(slice).map(|r| *r) | |
196 | } | |
197 | } | |
198 | ||
a2a8927a XL |
199 | #[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")] |
200 | impl<T, const N: usize> TryFrom<&mut [T]> for [T; N] | |
201 | where | |
202 | T: Copy, | |
203 | { | |
204 | type Error = TryFromSliceError; | |
205 | ||
206 | fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> { | |
207 | <Self>::try_from(&*slice) | |
208 | } | |
209 | } | |
210 | ||
416331ca | 211 | #[stable(feature = "try_from", since = "1.34.0")] |
3dfed10e | 212 | impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] { |
416331ca XL |
213 | type Error = TryFromSliceError; |
214 | ||
215 | fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> { | |
216 | if slice.len() == N { | |
217 | let ptr = slice.as_ptr() as *const [T; N]; | |
60c5eb7d | 218 | // SAFETY: ok because we just checked that the length fits |
416331ca XL |
219 | unsafe { Ok(&*ptr) } |
220 | } else { | |
221 | Err(TryFromSliceError(())) | |
92a42be0 SL |
222 | } |
223 | } | |
224 | } | |
225 | ||
416331ca | 226 | #[stable(feature = "try_from", since = "1.34.0")] |
3dfed10e | 227 | impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] { |
416331ca XL |
228 | type Error = TryFromSliceError; |
229 | ||
230 | fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> { | |
231 | if slice.len() == N { | |
232 | let ptr = slice.as_mut_ptr() as *mut [T; N]; | |
60c5eb7d | 233 | // SAFETY: ok because we just checked that the length fits |
416331ca XL |
234 | unsafe { Ok(&mut *ptr) } |
235 | } else { | |
236 | Err(TryFromSliceError(())) | |
92a42be0 SL |
237 | } |
238 | } | |
239 | } | |
240 | ||
136023e0 XL |
241 | /// The hash of an array is the same as that of the corresponding slice, |
242 | /// as required by the `Borrow` implementation. | |
243 | /// | |
244 | /// ``` | |
245 | /// #![feature(build_hasher_simple_hash_one)] | |
246 | /// use std::hash::BuildHasher; | |
247 | /// | |
248 | /// let b = std::collections::hash_map::RandomState::new(); | |
249 | /// let a: [u8; 3] = [0xa8, 0x3c, 0x09]; | |
250 | /// let s: &[u8] = &[0xa8, 0x3c, 0x09]; | |
251 | /// assert_eq!(b.hash_one(a), b.hash_one(s)); | |
252 | /// ``` | |
416331ca | 253 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 254 | impl<T: Hash, const N: usize> Hash for [T; N] { |
416331ca XL |
255 | fn hash<H: hash::Hasher>(&self, state: &mut H) { |
256 | Hash::hash(&self[..], state) | |
257 | } | |
258 | } | |
c34b1796 | 259 | |
416331ca | 260 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 261 | impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] { |
416331ca XL |
262 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
263 | fmt::Debug::fmt(&&self[..], f) | |
264 | } | |
265 | } | |
c34b1796 | 266 | |
416331ca | 267 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 268 | impl<'a, T, const N: usize> IntoIterator for &'a [T; N] { |
416331ca XL |
269 | type Item = &'a T; |
270 | type IntoIter = Iter<'a, T>; | |
e9174d1e | 271 | |
416331ca XL |
272 | fn into_iter(self) -> Iter<'a, T> { |
273 | self.iter() | |
274 | } | |
275 | } | |
e9174d1e | 276 | |
416331ca | 277 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 278 | impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] { |
416331ca XL |
279 | type Item = &'a mut T; |
280 | type IntoIter = IterMut<'a, T>; | |
a1dfa0c6 | 281 | |
416331ca XL |
282 | fn into_iter(self) -> IterMut<'a, T> { |
283 | self.iter_mut() | |
284 | } | |
285 | } | |
a1dfa0c6 | 286 | |
fc512014 | 287 | #[stable(feature = "index_trait_on_arrays", since = "1.50.0")] |
5e7ed085 FG |
288 | #[rustc_const_unstable(feature = "const_slice_index", issue = "none")] |
289 | impl<T, I, const N: usize> const Index<I> for [T; N] | |
fc512014 | 290 | where |
5e7ed085 | 291 | [T]: ~const Index<I>, |
fc512014 XL |
292 | { |
293 | type Output = <[T] as Index<I>>::Output; | |
294 | ||
295 | #[inline] | |
296 | fn index(&self, index: I) -> &Self::Output { | |
297 | Index::index(self as &[T], index) | |
298 | } | |
299 | } | |
300 | ||
301 | #[stable(feature = "index_trait_on_arrays", since = "1.50.0")] | |
5e7ed085 FG |
302 | #[rustc_const_unstable(feature = "const_slice_index", issue = "none")] |
303 | impl<T, I, const N: usize> const IndexMut<I> for [T; N] | |
fc512014 | 304 | where |
5e7ed085 | 305 | [T]: ~const IndexMut<I>, |
fc512014 XL |
306 | { |
307 | #[inline] | |
308 | fn index_mut(&mut self, index: I) -> &mut Self::Output { | |
309 | IndexMut::index_mut(self as &mut [T], index) | |
310 | } | |
311 | } | |
312 | ||
416331ca | 313 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 314 | impl<T: PartialOrd, const N: usize> PartialOrd for [T; N] { |
416331ca XL |
315 | #[inline] |
316 | fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> { | |
317 | PartialOrd::partial_cmp(&&self[..], &&other[..]) | |
318 | } | |
319 | #[inline] | |
320 | fn lt(&self, other: &[T; N]) -> bool { | |
321 | PartialOrd::lt(&&self[..], &&other[..]) | |
322 | } | |
323 | #[inline] | |
324 | fn le(&self, other: &[T; N]) -> bool { | |
325 | PartialOrd::le(&&self[..], &&other[..]) | |
326 | } | |
327 | #[inline] | |
328 | fn ge(&self, other: &[T; N]) -> bool { | |
329 | PartialOrd::ge(&&self[..], &&other[..]) | |
330 | } | |
331 | #[inline] | |
332 | fn gt(&self, other: &[T; N]) -> bool { | |
333 | PartialOrd::gt(&&self[..], &&other[..]) | |
334 | } | |
335 | } | |
336 | ||
29967ef6 | 337 | /// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison). |
416331ca | 338 | #[stable(feature = "rust1", since = "1.0.0")] |
3dfed10e | 339 | impl<T: Ord, const N: usize> Ord for [T; N] { |
416331ca XL |
340 | #[inline] |
341 | fn cmp(&self, other: &[T; N]) -> Ordering { | |
342 | Ord::cmp(&&self[..], &&other[..]) | |
343 | } | |
344 | } | |
345 | ||
3c0e092e XL |
346 | #[stable(feature = "copy_clone_array_lib", since = "1.58.0")] |
347 | impl<T: Copy, const N: usize> Copy for [T; N] {} | |
348 | ||
3c0e092e XL |
349 | #[stable(feature = "copy_clone_array_lib", since = "1.58.0")] |
350 | impl<T: Clone, const N: usize> Clone for [T; N] { | |
351 | #[inline] | |
352 | fn clone(&self) -> Self { | |
353 | SpecArrayClone::clone(self) | |
354 | } | |
355 | ||
356 | #[inline] | |
357 | fn clone_from(&mut self, other: &Self) { | |
358 | self.clone_from_slice(other); | |
359 | } | |
360 | } | |
361 | ||
3c0e092e XL |
362 | trait SpecArrayClone: Clone { |
363 | fn clone<const N: usize>(array: &[Self; N]) -> [Self; N]; | |
364 | } | |
365 | ||
3c0e092e XL |
366 | impl<T: Clone> SpecArrayClone for T { |
367 | #[inline] | |
368 | default fn clone<const N: usize>(array: &[T; N]) -> [T; N] { | |
369 | // SAFETY: we know for certain that this iterator will yield exactly `N` | |
370 | // items. | |
371 | unsafe { collect_into_array_unchecked(&mut array.iter().cloned()) } | |
372 | } | |
373 | } | |
374 | ||
3c0e092e XL |
375 | impl<T: Copy> SpecArrayClone for T { |
376 | #[inline] | |
377 | fn clone<const N: usize>(array: &[T; N]) -> [T; N] { | |
378 | *array | |
379 | } | |
380 | } | |
381 | ||
29967ef6 XL |
382 | // The Default impls cannot be done with const generics because `[T; 0]` doesn't |
383 | // require Default to be implemented, and having different impl blocks for | |
384 | // different numbers isn't supported yet. | |
e9174d1e SL |
385 | |
386 | macro_rules! array_impl_default { | |
387 | {$n:expr, $t:ident $($ts:ident)*} => { | |
388 | #[stable(since = "1.4.0", feature = "array_default")] | |
389 | impl<T> Default for [T; $n] where T: Default { | |
390 | fn default() -> [T; $n] { | |
391 | [$t::default(), $($ts::default()),*] | |
392 | } | |
393 | } | |
394 | array_impl_default!{($n - 1), $($ts)*} | |
395 | }; | |
396 | {$n:expr,} => { | |
397 | #[stable(since = "1.4.0", feature = "array_default")] | |
94222f64 XL |
398 | #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] |
399 | impl<T> const Default for [T; $n] { | |
e9174d1e SL |
400 | fn default() -> [T; $n] { [] } |
401 | } | |
402 | }; | |
403 | } | |
404 | ||
dfeec247 | 405 | 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} |
3dfed10e | 406 | |
3dfed10e XL |
407 | impl<T, const N: usize> [T; N] { |
408 | /// Returns an array of the same size as `self`, with function `f` applied to each element | |
409 | /// in order. | |
410 | /// | |
94222f64 XL |
411 | /// If you don't necessarily need a new fixed-size array, consider using |
412 | /// [`Iterator::map`] instead. | |
413 | /// | |
414 | /// | |
415 | /// # Note on performance and stack usage | |
416 | /// | |
417 | /// Unfortunately, usages of this method are currently not always optimized | |
418 | /// as well as they could be. This mainly concerns large arrays, as mapping | |
419 | /// over small arrays seem to be optimized just fine. Also note that in | |
420 | /// debug mode (i.e. without any optimizations), this method can use a lot | |
421 | /// of stack space (a few times the size of the array or more). | |
422 | /// | |
423 | /// Therefore, in performance-critical code, try to avoid using this method | |
424 | /// on large arrays or check the emitted code. Also try to avoid chained | |
425 | /// maps (e.g. `arr.map(...).map(...)`). | |
426 | /// | |
427 | /// In many cases, you can instead use [`Iterator::map`] by calling `.iter()` | |
428 | /// or `.into_iter()` on your array. `[T; N]::map` is only necessary if you | |
429 | /// really need a new array of the same size as the result. Rust's lazy | |
430 | /// iterators tend to get optimized very well. | |
431 | /// | |
432 | /// | |
3dfed10e XL |
433 | /// # Examples |
434 | /// | |
435 | /// ``` | |
3dfed10e XL |
436 | /// let x = [1, 2, 3]; |
437 | /// let y = x.map(|v| v + 1); | |
438 | /// assert_eq!(y, [2, 3, 4]); | |
439 | /// | |
440 | /// let x = [1, 2, 3]; | |
441 | /// let mut temp = 0; | |
442 | /// let y = x.map(|v| { temp += 1; v * temp }); | |
443 | /// assert_eq!(y, [1, 4, 9]); | |
444 | /// | |
445 | /// let x = ["Ferris", "Bueller's", "Day", "Off"]; | |
446 | /// let y = x.map(|v| v.len()); | |
447 | /// assert_eq!(y, [6, 9, 3, 3]); | |
448 | /// ``` | |
136023e0 | 449 | #[stable(feature = "array_map", since = "1.55.0")] |
6a06907d | 450 | pub fn map<F, U>(self, f: F) -> [U; N] |
3dfed10e XL |
451 | where |
452 | F: FnMut(T) -> U, | |
453 | { | |
6a06907d XL |
454 | // SAFETY: we know for certain that this iterator will yield exactly `N` |
455 | // items. | |
17df50a5 | 456 | unsafe { collect_into_array_unchecked(&mut IntoIterator::into_iter(self).map(f)) } |
3dfed10e | 457 | } |
1b1a35ee | 458 | |
a2a8927a XL |
459 | /// A fallible function `f` applied to each element on array `self` in order to |
460 | /// return an array the same size as `self` or the first error encountered. | |
461 | /// | |
462 | /// The return type of this function depends on the return type of the closure. | |
463 | /// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N]; E>`. | |
464 | /// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`. | |
465 | /// | |
466 | /// # Examples | |
467 | /// | |
468 | /// ``` | |
469 | /// #![feature(array_try_map)] | |
470 | /// let a = ["1", "2", "3"]; | |
471 | /// let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1); | |
472 | /// assert_eq!(b, [2, 3, 4]); | |
473 | /// | |
474 | /// let a = ["1", "2a", "3"]; | |
475 | /// let b = a.try_map(|v| v.parse::<u32>()); | |
476 | /// assert!(b.is_err()); | |
477 | /// | |
478 | /// use std::num::NonZeroU32; | |
479 | /// let z = [1, 2, 0, 3, 4]; | |
480 | /// assert_eq!(z.try_map(NonZeroU32::new), None); | |
481 | /// let a = [1, 2, 3]; | |
482 | /// let b = a.try_map(NonZeroU32::new); | |
483 | /// let c = b.map(|x| x.map(NonZeroU32::get)); | |
484 | /// assert_eq!(c, Some(a)); | |
485 | /// ``` | |
486 | #[unstable(feature = "array_try_map", issue = "79711")] | |
487 | pub fn try_map<F, R>(self, f: F) -> ChangeOutputType<R, [R::Output; N]> | |
488 | where | |
489 | F: FnMut(T) -> R, | |
490 | R: Try, | |
491 | R::Residual: Residual<[R::Output; N]>, | |
492 | { | |
493 | // SAFETY: we know for certain that this iterator will yield exactly `N` | |
494 | // items. | |
495 | unsafe { try_collect_into_array_unchecked(&mut IntoIterator::into_iter(self).map(f)) } | |
496 | } | |
497 | ||
fc512014 XL |
498 | /// 'Zips up' two arrays into a single array of pairs. |
499 | /// | |
500 | /// `zip()` returns a new array where every element is a tuple where the | |
501 | /// first element comes from the first array, and the second element comes | |
502 | /// from the second array. In other words, it zips two arrays together, | |
503 | /// into a single one. | |
504 | /// | |
505 | /// # Examples | |
506 | /// | |
507 | /// ``` | |
508 | /// #![feature(array_zip)] | |
509 | /// let x = [1, 2, 3]; | |
510 | /// let y = [4, 5, 6]; | |
511 | /// let z = x.zip(y); | |
512 | /// assert_eq!(z, [(1, 4), (2, 5), (3, 6)]); | |
513 | /// ``` | |
514 | #[unstable(feature = "array_zip", issue = "80094")] | |
515 | pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N] { | |
17df50a5 | 516 | let mut iter = IntoIterator::into_iter(self).zip(rhs); |
6a06907d XL |
517 | |
518 | // SAFETY: we know for certain that this iterator will yield exactly `N` | |
519 | // items. | |
520 | unsafe { collect_into_array_unchecked(&mut iter) } | |
fc512014 XL |
521 | } |
522 | ||
1b1a35ee | 523 | /// Returns a slice containing the entire array. Equivalent to `&s[..]`. |
c295e0f8 | 524 | #[stable(feature = "array_as_slice", since = "1.57.0")] |
5099ac24 | 525 | #[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")] |
c295e0f8 | 526 | pub const fn as_slice(&self) -> &[T] { |
1b1a35ee XL |
527 | self |
528 | } | |
529 | ||
530 | /// Returns a mutable slice containing the entire array. Equivalent to | |
531 | /// `&mut s[..]`. | |
c295e0f8 | 532 | #[stable(feature = "array_as_slice", since = "1.57.0")] |
1b1a35ee XL |
533 | pub fn as_mut_slice(&mut self) -> &mut [T] { |
534 | self | |
535 | } | |
5869c6ff XL |
536 | |
537 | /// Borrows each element and returns an array of references with the same | |
538 | /// size as `self`. | |
539 | /// | |
540 | /// | |
541 | /// # Example | |
542 | /// | |
543 | /// ``` | |
544 | /// #![feature(array_methods)] | |
545 | /// | |
546 | /// let floats = [3.1, 2.7, -1.0]; | |
547 | /// let float_refs: [&f64; 3] = floats.each_ref(); | |
548 | /// assert_eq!(float_refs, [&3.1, &2.7, &-1.0]); | |
549 | /// ``` | |
550 | /// | |
551 | /// This method is particularly useful if combined with other methods, like | |
6a06907d | 552 | /// [`map`](#method.map). This way, you can avoid moving the original |
c295e0f8 | 553 | /// array if its elements are not [`Copy`]. |
5869c6ff XL |
554 | /// |
555 | /// ``` | |
136023e0 | 556 | /// #![feature(array_methods)] |
5869c6ff XL |
557 | /// |
558 | /// let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()]; | |
559 | /// let is_ascii = strings.each_ref().map(|s| s.is_ascii()); | |
560 | /// assert_eq!(is_ascii, [true, false, true]); | |
561 | /// | |
562 | /// // We can still access the original array: it has not been moved. | |
563 | /// assert_eq!(strings.len(), 3); | |
564 | /// ``` | |
565 | #[unstable(feature = "array_methods", issue = "76118")] | |
566 | pub fn each_ref(&self) -> [&T; N] { | |
6a06907d XL |
567 | // SAFETY: we know for certain that this iterator will yield exactly `N` |
568 | // items. | |
569 | unsafe { collect_into_array_unchecked(&mut self.iter()) } | |
5869c6ff XL |
570 | } |
571 | ||
572 | /// Borrows each element mutably and returns an array of mutable references | |
573 | /// with the same size as `self`. | |
574 | /// | |
575 | /// | |
576 | /// # Example | |
577 | /// | |
578 | /// ``` | |
579 | /// #![feature(array_methods)] | |
580 | /// | |
581 | /// let mut floats = [3.1, 2.7, -1.0]; | |
582 | /// let float_refs: [&mut f64; 3] = floats.each_mut(); | |
583 | /// *float_refs[0] = 0.0; | |
584 | /// assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]); | |
585 | /// assert_eq!(floats, [0.0, 2.7, -1.0]); | |
586 | /// ``` | |
587 | #[unstable(feature = "array_methods", issue = "76118")] | |
588 | pub fn each_mut(&mut self) -> [&mut T; N] { | |
6a06907d XL |
589 | // SAFETY: we know for certain that this iterator will yield exactly `N` |
590 | // items. | |
591 | unsafe { collect_into_array_unchecked(&mut self.iter_mut()) } | |
592 | } | |
3c0e092e XL |
593 | |
594 | /// Divides one array reference into two at an index. | |
595 | /// | |
596 | /// The first will contain all indices from `[0, M)` (excluding | |
597 | /// the index `M` itself) and the second will contain all | |
598 | /// indices from `[M, N)` (excluding the index `N` itself). | |
599 | /// | |
600 | /// # Panics | |
601 | /// | |
602 | /// Panics if `M > N`. | |
603 | /// | |
604 | /// # Examples | |
605 | /// | |
606 | /// ``` | |
607 | /// #![feature(split_array)] | |
608 | /// | |
609 | /// let v = [1, 2, 3, 4, 5, 6]; | |
610 | /// | |
611 | /// { | |
612 | /// let (left, right) = v.split_array_ref::<0>(); | |
613 | /// assert_eq!(left, &[]); | |
614 | /// assert_eq!(right, &[1, 2, 3, 4, 5, 6]); | |
615 | /// } | |
616 | /// | |
617 | /// { | |
618 | /// let (left, right) = v.split_array_ref::<2>(); | |
619 | /// assert_eq!(left, &[1, 2]); | |
620 | /// assert_eq!(right, &[3, 4, 5, 6]); | |
621 | /// } | |
622 | /// | |
623 | /// { | |
624 | /// let (left, right) = v.split_array_ref::<6>(); | |
625 | /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]); | |
626 | /// assert_eq!(right, &[]); | |
627 | /// } | |
628 | /// ``` | |
629 | #[unstable( | |
630 | feature = "split_array", | |
631 | reason = "return type should have array as 2nd element", | |
632 | issue = "90091" | |
633 | )] | |
634 | #[inline] | |
635 | pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) { | |
636 | (&self[..]).split_array_ref::<M>() | |
637 | } | |
638 | ||
639 | /// Divides one mutable array reference into two at an index. | |
640 | /// | |
641 | /// The first will contain all indices from `[0, M)` (excluding | |
642 | /// the index `M` itself) and the second will contain all | |
643 | /// indices from `[M, N)` (excluding the index `N` itself). | |
644 | /// | |
645 | /// # Panics | |
646 | /// | |
647 | /// Panics if `M > N`. | |
648 | /// | |
649 | /// # Examples | |
650 | /// | |
651 | /// ``` | |
652 | /// #![feature(split_array)] | |
653 | /// | |
654 | /// let mut v = [1, 0, 3, 0, 5, 6]; | |
655 | /// let (left, right) = v.split_array_mut::<2>(); | |
656 | /// assert_eq!(left, &mut [1, 0][..]); | |
657 | /// assert_eq!(right, &mut [3, 0, 5, 6]); | |
658 | /// left[1] = 2; | |
659 | /// right[1] = 4; | |
660 | /// assert_eq!(v, [1, 2, 3, 4, 5, 6]); | |
661 | /// ``` | |
662 | #[unstable( | |
663 | feature = "split_array", | |
664 | reason = "return type should have array as 2nd element", | |
665 | issue = "90091" | |
666 | )] | |
667 | #[inline] | |
668 | pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) { | |
669 | (&mut self[..]).split_array_mut::<M>() | |
670 | } | |
a2a8927a XL |
671 | |
672 | /// Divides one array reference into two at an index from the end. | |
673 | /// | |
674 | /// The first will contain all indices from `[0, N - M)` (excluding | |
675 | /// the index `N - M` itself) and the second will contain all | |
676 | /// indices from `[N - M, N)` (excluding the index `N` itself). | |
677 | /// | |
678 | /// # Panics | |
679 | /// | |
680 | /// Panics if `M > N`. | |
681 | /// | |
682 | /// # Examples | |
683 | /// | |
684 | /// ``` | |
685 | /// #![feature(split_array)] | |
686 | /// | |
687 | /// let v = [1, 2, 3, 4, 5, 6]; | |
688 | /// | |
689 | /// { | |
690 | /// let (left, right) = v.rsplit_array_ref::<0>(); | |
691 | /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]); | |
692 | /// assert_eq!(right, &[]); | |
693 | /// } | |
694 | /// | |
695 | /// { | |
696 | /// let (left, right) = v.rsplit_array_ref::<2>(); | |
697 | /// assert_eq!(left, &[1, 2, 3, 4]); | |
698 | /// assert_eq!(right, &[5, 6]); | |
699 | /// } | |
700 | /// | |
701 | /// { | |
702 | /// let (left, right) = v.rsplit_array_ref::<6>(); | |
703 | /// assert_eq!(left, &[]); | |
704 | /// assert_eq!(right, &[1, 2, 3, 4, 5, 6]); | |
705 | /// } | |
706 | /// ``` | |
707 | #[unstable( | |
708 | feature = "split_array", | |
709 | reason = "return type should have array as 2nd element", | |
710 | issue = "90091" | |
711 | )] | |
712 | #[inline] | |
713 | pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) { | |
714 | (&self[..]).rsplit_array_ref::<M>() | |
715 | } | |
716 | ||
717 | /// Divides one mutable array reference into two at an index from the end. | |
718 | /// | |
719 | /// The first will contain all indices from `[0, N - M)` (excluding | |
720 | /// the index `N - M` itself) and the second will contain all | |
721 | /// indices from `[N - M, N)` (excluding the index `N` itself). | |
722 | /// | |
723 | /// # Panics | |
724 | /// | |
725 | /// Panics if `M > N`. | |
726 | /// | |
727 | /// # Examples | |
728 | /// | |
729 | /// ``` | |
730 | /// #![feature(split_array)] | |
731 | /// | |
732 | /// let mut v = [1, 0, 3, 0, 5, 6]; | |
733 | /// let (left, right) = v.rsplit_array_mut::<4>(); | |
734 | /// assert_eq!(left, &mut [1, 0]); | |
735 | /// assert_eq!(right, &mut [3, 0, 5, 6][..]); | |
736 | /// left[1] = 2; | |
737 | /// right[1] = 4; | |
738 | /// assert_eq!(v, [1, 2, 3, 4, 5, 6]); | |
739 | /// ``` | |
740 | #[unstable( | |
741 | feature = "split_array", | |
742 | reason = "return type should have array as 2nd element", | |
743 | issue = "90091" | |
744 | )] | |
745 | #[inline] | |
746 | pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) { | |
747 | (&mut self[..]).rsplit_array_mut::<M>() | |
748 | } | |
6a06907d XL |
749 | } |
750 | ||
751 | /// Pulls `N` items from `iter` and returns them as an array. If the iterator | |
752 | /// yields fewer than `N` items, this function exhibits undefined behavior. | |
753 | /// | |
a2a8927a | 754 | /// See [`try_collect_into_array`] for more information. |
6a06907d XL |
755 | /// |
756 | /// | |
757 | /// # Safety | |
758 | /// | |
759 | /// It is up to the caller to guarantee that `iter` yields at least `N` items. | |
760 | /// Violating this condition causes undefined behavior. | |
a2a8927a | 761 | unsafe fn try_collect_into_array_unchecked<I, T, R, const N: usize>(iter: &mut I) -> R::TryType |
6a06907d XL |
762 | where |
763 | // Note: `TrustedLen` here is somewhat of an experiment. This is just an | |
764 | // internal function, so feel free to remove if this bound turns out to be a | |
765 | // bad idea. In that case, remember to also remove the lower bound | |
766 | // `debug_assert!` below! | |
a2a8927a XL |
767 | I: Iterator + TrustedLen, |
768 | I::Item: Try<Output = T, Residual = R>, | |
769 | R: Residual<[T; N]>, | |
6a06907d XL |
770 | { |
771 | debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX)); | |
772 | debug_assert!(N <= iter.size_hint().0); | |
773 | ||
94222f64 | 774 | // SAFETY: covered by the function contract. |
a2a8927a | 775 | unsafe { try_collect_into_array(iter).unwrap_unchecked() } |
6a06907d XL |
776 | } |
777 | ||
a2a8927a | 778 | // Infallible version of `try_collect_into_array_unchecked`. |
c295e0f8 XL |
779 | unsafe fn collect_into_array_unchecked<I, const N: usize>(iter: &mut I) -> [I::Item; N] |
780 | where | |
781 | I: Iterator + TrustedLen, | |
782 | { | |
a2a8927a | 783 | let mut map = iter.map(NeverShortCircuit); |
c295e0f8 XL |
784 | |
785 | // SAFETY: The same safety considerations w.r.t. the iterator length | |
a2a8927a | 786 | // apply for `try_collect_into_array_unchecked` as for |
c295e0f8 | 787 | // `collect_into_array_unchecked` |
a2a8927a XL |
788 | match unsafe { try_collect_into_array_unchecked(&mut map) } { |
789 | NeverShortCircuit(array) => array, | |
c295e0f8 XL |
790 | } |
791 | } | |
792 | ||
6a06907d | 793 | /// Pulls `N` items from `iter` and returns them as an array. If the iterator |
064997fb FG |
794 | /// yields fewer than `N` items, `Err` is returned containing an iterator over |
795 | /// the already yielded items. | |
6a06907d XL |
796 | /// |
797 | /// Since the iterator is passed as a mutable reference and this function calls | |
798 | /// `next` at most `N` times, the iterator can still be used afterwards to | |
799 | /// retrieve the remaining items. | |
800 | /// | |
801 | /// If `iter.next()` panicks, all items already yielded by the iterator are | |
802 | /// dropped. | |
064997fb FG |
803 | #[inline] |
804 | fn try_collect_into_array<I, T, R, const N: usize>( | |
805 | iter: &mut I, | |
806 | ) -> Result<R::TryType, IntoIter<T, N>> | |
6a06907d | 807 | where |
a2a8927a XL |
808 | I: Iterator, |
809 | I::Item: Try<Output = T, Residual = R>, | |
810 | R: Residual<[T; N]>, | |
6a06907d XL |
811 | { |
812 | if N == 0 { | |
813 | // SAFETY: An empty array is always inhabited and has no validity invariants. | |
064997fb | 814 | return Ok(Try::from_output(unsafe { mem::zeroed() })); |
6a06907d XL |
815 | } |
816 | ||
c295e0f8 XL |
817 | struct Guard<'a, T, const N: usize> { |
818 | array_mut: &'a mut [MaybeUninit<T>; N], | |
6a06907d XL |
819 | initialized: usize, |
820 | } | |
821 | ||
c295e0f8 | 822 | impl<T, const N: usize> Drop for Guard<'_, T, N> { |
6a06907d XL |
823 | fn drop(&mut self) { |
824 | debug_assert!(self.initialized <= N); | |
825 | ||
c295e0f8 | 826 | // SAFETY: this slice will contain only initialized objects. |
6a06907d | 827 | unsafe { |
c295e0f8 XL |
828 | crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut( |
829 | &mut self.array_mut.get_unchecked_mut(..self.initialized), | |
830 | )); | |
6a06907d XL |
831 | } |
832 | } | |
833 | } | |
834 | ||
835 | let mut array = MaybeUninit::uninit_array::<N>(); | |
c295e0f8 XL |
836 | let mut guard = Guard { array_mut: &mut array, initialized: 0 }; |
837 | ||
064997fb FG |
838 | for _ in 0..N { |
839 | match iter.next() { | |
840 | Some(item_rslt) => { | |
841 | let item = match item_rslt.branch() { | |
842 | ControlFlow::Break(r) => { | |
843 | return Ok(FromResidual::from_residual(r)); | |
844 | } | |
845 | ControlFlow::Continue(elem) => elem, | |
846 | }; | |
847 | ||
848 | // SAFETY: `guard.initialized` starts at 0, is increased by one in the | |
849 | // loop and the loop is aborted once it reaches N (which is | |
850 | // `array.len()`). | |
851 | unsafe { | |
852 | guard.array_mut.get_unchecked_mut(guard.initialized).write(item); | |
853 | } | |
854 | guard.initialized += 1; | |
855 | } | |
856 | None => { | |
857 | let alive = 0..guard.initialized; | |
858 | mem::forget(guard); | |
859 | // SAFETY: `array` was initialized with exactly `initialized` | |
860 | // number of elements. | |
861 | return Err(unsafe { IntoIter::new_unchecked(array, alive) }); | |
c295e0f8 | 862 | } |
6a06907d | 863 | } |
5869c6ff | 864 | } |
6a06907d | 865 | |
064997fb FG |
866 | mem::forget(guard); |
867 | // SAFETY: All elements of the array were populated in the loop above. | |
868 | let output = unsafe { MaybeUninit::array_assume_init(array) }; | |
869 | Ok(Try::from_output(output)) | |
870 | } | |
871 | ||
872 | /// Returns the next chunk of `N` items from the iterator or errors with an | |
873 | /// iterator over the remainder. Used for `Iterator::next_chunk`. | |
874 | #[inline] | |
875 | pub(crate) fn iter_next_chunk<I, const N: usize>( | |
876 | iter: &mut I, | |
877 | ) -> Result<[I::Item; N], IntoIter<I::Item, N>> | |
878 | where | |
879 | I: Iterator, | |
880 | { | |
881 | let mut map = iter.map(NeverShortCircuit); | |
882 | try_collect_into_array(&mut map).map(|NeverShortCircuit(arr)| arr) | |
3dfed10e | 883 | } |