mod.rs.html -- source
Description: Source to the Rust file `src/libcore/array/mod.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432

    //! Implementations of things like `Eq` for fixed-length arrays
    //! up to a certain length. Eventually we should able to generalize
    //! to all lengths.
    //!
    //! *[See also the array primitive type](../../std/primitive.array.html).*

    #![stable(feature = "core_array", since = "1.36.0")]

    use crate::borrow::{Borrow, BorrowMut};
    use crate::cmp::Ordering;
    use crate::convert::{Infallible, TryFrom};
    use crate::fmt;
    use crate::hash::{Hash, self};
    use crate::marker::Unsize;
    use crate::slice::{Iter, IterMut};

    #[cfg(not(bootstrap))]
    mod iter;

    #[cfg(not(bootstrap))]
    #[unstable(feature = "array_value_iter", issue = "65798")]
    pub use iter::IntoIter;

    /// Utility trait implemented only on arrays of fixed size
    ///
    /// This trait can be used to implement other traits on fixed-size arrays
    /// without causing much metadata bloat.
    ///
    /// The trait is marked unsafe in order to restrict implementors to fixed-size
    /// arrays. User of this trait can assume that implementors have the exact
    /// layout in memory of a fixed size array (for example, for unsafe
    /// initialization).
    ///
    /// Note that the traits [`AsRef`] and [`AsMut`] provide similar methods for types that
    /// may not be fixed-size arrays. Implementors should prefer those traits
    /// instead.
    ///
    /// [`AsRef`]: ../convert/trait.AsRef.html
    /// [`AsMut`]: ../convert/trait.AsMut.html
    #[unstable(feature = "fixed_size_array", issue = "27778")]
    pub unsafe trait FixedSizeArray<T> {
        /// Converts the array to immutable slice
        #[unstable(feature = "fixed_size_array", issue = "27778")]
        fn as_slice(&self) -> &[T];
        /// Converts the array to mutable slice
        #[unstable(feature = "fixed_size_array", issue = "27778")]
        fn as_mut_slice(&mut self) -> &mut [T];
    }

    #[unstable(feature = "fixed_size_array", issue = "27778")]
    unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
        #[inline]
        fn as_slice(&self) -> &[T] {
            self
        }
        #[inline]
        fn as_mut_slice(&mut self) -> &mut [T] {
            self
        }
    }

    /// The error type returned when a conversion from a slice to an array fails.
    #[stable(feature = "try_from", since = "1.34.0")]
    #[derive(Debug, Copy, Clone)]
    pub struct TryFromSliceError(());

    #[stable(feature = "core_array", since = "1.36.0")]
    impl fmt::Display for TryFromSliceError {
        #[inline]
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            fmt::Display::fmt(self.__description(), f)
        }
    }

    impl TryFromSliceError {
        #[unstable(feature = "array_error_internals",
               reason = "available through Error trait and this method should not \
                         be exposed publicly",
               issue = "0")]
        #[inline]
        #[doc(hidden)]
        pub fn __description(&self) -> &str {
            "could not convert slice to array"
        }
    }

    #[stable(feature = "try_from_slice_error", since = "1.36.0")]
    impl From<Infallible> for TryFromSliceError {
        fn from(x: Infallible) -> TryFromSliceError {
            match x {}
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T, const N: usize> AsRef<[T]> for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        #[inline]
        fn as_ref(&self) -> &[T] {
            &self[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T, const N: usize> AsMut<[T]> for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        #[inline]
        fn as_mut(&mut self) -> &mut [T] {
            &mut self[..]
        }
    }

    #[stable(feature = "array_borrow", since = "1.4.0")]
    impl<T, const N: usize> Borrow<[T]> for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        fn borrow(&self) -> &[T] {
            self
        }
    }

    #[stable(feature = "array_borrow", since = "1.4.0")]
    impl<T, const N: usize> BorrowMut<[T]> for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        fn borrow_mut(&mut self) -> &mut [T] {
            self
        }
    }

    #[stable(feature = "try_from", since = "1.34.0")]
    impl<T, const N: usize> TryFrom<&[T]> for [T; N]
    where
        T: Copy,
        [T; N]: LengthAtMost32,
    {
        type Error = TryFromSliceError;

        fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
            <&Self>::try_from(slice).map(|r| *r)
        }
    }

    #[stable(feature = "try_from", since = "1.34.0")]
    impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        type Error = TryFromSliceError;

        fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
            if slice.len() == N {
                let ptr = slice.as_ptr() as *const [T; N];
                unsafe { Ok(&*ptr) }
            } else {
                Err(TryFromSliceError(()))
            }
        }
    }

    #[stable(feature = "try_from", since = "1.34.0")]
    impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        type Error = TryFromSliceError;

        fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
            if slice.len() == N {
                let ptr = slice.as_mut_ptr() as *mut [T; N];
                unsafe { Ok(&mut *ptr) }
            } else {
                Err(TryFromSliceError(()))
            }
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T: Hash, const N: usize> Hash for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        fn hash<H: hash::Hasher>(&self, state: &mut H) {
            Hash::hash(&self[..], state)
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            fmt::Debug::fmt(&&self[..], f)
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        type Item = &'a T;
        type IntoIter = Iter<'a, T>;

        fn into_iter(self) -> Iter<'a, T> {
            self.iter()
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        type Item = &'a mut T;
        type IntoIter = IterMut<'a, T>;

        fn into_iter(self) -> IterMut<'a, T> {
            self.iter_mut()
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
    where
        A: PartialEq<B>,
        [A; N]: LengthAtMost32,
        [B; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &[B; N]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &[B; N]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
    where
        A: PartialEq<B>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &[B]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &[B]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
    where
        B: PartialEq<A>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &[A; N]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &[A; N]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N]
    where
        A: PartialEq<B>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &&'b [B]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &&'b [B]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b [B]
    where
        B: PartialEq<A>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &[A; N]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &[A; N]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N]
    where
        A: PartialEq<B>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &&'b mut [B]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &&'b mut [B]) -> bool {
            self[..] != other[..]
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b mut [B]
    where
        B: PartialEq<A>,
        [A; N]: LengthAtMost32,
    {
        #[inline]
        fn eq(&self, other: &[A; N]) -> bool {
            self[..] == other[..]
        }
        #[inline]
        fn ne(&self, other: &[A; N]) -> bool {
            self[..] != other[..]
        }
    }

    // NOTE: some less important impls are omitted to reduce code bloat
    // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
    // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T: PartialOrd, const N: usize> PartialOrd for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        #[inline]
        fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
            PartialOrd::partial_cmp(&&self[..], &&other[..])
        }
        #[inline]
        fn lt(&self, other: &[T; N]) -> bool {
            PartialOrd::lt(&&self[..], &&other[..])
        }
        #[inline]
        fn le(&self, other: &[T; N]) -> bool {
            PartialOrd::le(&&self[..], &&other[..])
        }
        #[inline]
        fn ge(&self, other: &[T; N]) -> bool {
            PartialOrd::ge(&&self[..], &&other[..])
        }
        #[inline]
        fn gt(&self, other: &[T; N]) -> bool {
            PartialOrd::gt(&&self[..], &&other[..])
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T: Ord, const N: usize> Ord for [T; N]
    where
        [T; N]: LengthAtMost32,
    {
        #[inline]
        fn cmp(&self, other: &[T; N]) -> Ordering {
            Ord::cmp(&&self[..], &&other[..])
        }
    }

    /// Implemented for lengths where trait impls are allowed on arrays in core/std
    #[rustc_on_unimplemented(
        message="arrays only have std trait implementations for lengths 0..=32",
    )]
    #[unstable(feature = "const_generic_impls_guard", issue = "0",
        reason = "will never be stable, just a temporary step until const generics are stable")]
    pub trait LengthAtMost32 {}

    macro_rules! array_impls {
        ($($N:literal)+) => {
            $(
                #[unstable(feature = "const_generic_impls_guard", issue = "0")]
                impl<T> LengthAtMost32 for [T; $N] {}
            )+
        }
    }

    array_impls! {
         0  1  2  3  4  5  6  7  8  9
        10 11 12 13 14 15 16 17 18 19
        20 21 22 23 24 25 26 27 28 29
        30 31 32
    }

    // The Default impls cannot be generated using the array_impls! macro because
    // they require array literals.

    macro_rules! array_impl_default {
        {$n:expr, $t:ident $($ts:ident)*} => {
            #[stable(since = "1.4.0", feature = "array_default")]
            impl<T> Default for [T; $n] where T: Default {
                fn default() -> [T; $n] {
                    [$t::default(), $($ts::default()),*]
                }
            }
            array_impl_default!{($n - 1), $($ts)*}
        };
        {$n:expr,} => {
            #[stable(since = "1.4.0", feature = "array_default")]
            impl<T> Default for [T; $n] {
                fn default() -> [T; $n] { [] }
            }
        };
    }

    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}