2014-11-14 11:37:59 +00:00
|
|
|
//! Implementations of things like `Eq` for fixed-length arrays
|
|
|
|
//! up to a certain length. Eventually we should able to generalize
|
|
|
|
//! to all lengths.
|
2015-07-20 18:21:02 +00:00
|
|
|
//!
|
2016-03-01 12:44:48 +00:00
|
|
|
//! *[See also the array primitive type](../../std/primitive.array.html).*
|
2014-10-31 09:41:25 +00:00
|
|
|
|
2019-05-09 02:58:39 +00:00
|
|
|
#![stable(feature = "core_array", since = "1.36.0")]
|
2015-03-23 21:01:28 +00:00
|
|
|
|
2019-04-15 02:23:21 +00:00
|
|
|
use crate::borrow::{Borrow, BorrowMut};
|
|
|
|
use crate::cmp::Ordering;
|
2019-04-26 19:45:26 +00:00
|
|
|
use crate::convert::{Infallible, TryFrom};
|
2019-04-15 02:23:21 +00:00
|
|
|
use crate::fmt;
|
|
|
|
use crate::hash::{Hash, self};
|
|
|
|
use crate::marker::Unsize;
|
|
|
|
use crate::slice::{Iter, IterMut};
|
2014-10-31 09:41:25 +00:00
|
|
|
|
2015-02-27 19:48:51 +00:00
|
|
|
/// 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.
|
2015-09-23 15:38:01 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2019-05-09 02:58:39 +00:00
|
|
|
#[unstable(feature = "fixed_size_array", issue = "27778")]
|
2015-09-19 19:33:34 +00:00
|
|
|
pub unsafe trait FixedSizeArray<T> {
|
2015-02-27 19:48:51 +00:00
|
|
|
/// Converts the array to immutable slice
|
2019-05-09 02:58:39 +00:00
|
|
|
#[unstable(feature = "fixed_size_array", issue = "27778")]
|
2015-02-27 19:48:51 +00:00
|
|
|
fn as_slice(&self) -> &[T];
|
|
|
|
/// Converts the array to mutable slice
|
2019-05-09 02:58:39 +00:00
|
|
|
#[unstable(feature = "fixed_size_array", issue = "27778")]
|
2015-02-27 19:48:51 +00:00
|
|
|
fn as_mut_slice(&mut self) -> &mut [T];
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:58:39 +00:00
|
|
|
#[unstable(feature = "fixed_size_array", issue = "27778")]
|
2015-09-19 19:33:34 +00:00
|
|
|
unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
|
2015-08-29 16:30:05 +00:00
|
|
|
#[inline]
|
|
|
|
fn as_slice(&self) -> &[T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 20:35:23 +00:00
|
|
|
/// The error type returned when a conversion from a slice to an array fails.
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-09-21 20:35:23 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct TryFromSliceError(());
|
|
|
|
|
2019-05-09 02:58:39 +00:00
|
|
|
#[stable(feature = "core_array", since = "1.36.0")]
|
2017-09-29 15:15:05 +00:00
|
|
|
impl fmt::Display for TryFromSliceError {
|
|
|
|
#[inline]
|
2019-04-18 23:37:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-09-29 15:26:19 +00:00
|
|
|
fmt::Display::fmt(self.__description(), f)
|
2017-09-29 15:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFromSliceError {
|
2017-09-29 15:20:21 +00:00
|
|
|
#[unstable(feature = "array_error_internals",
|
|
|
|
reason = "available through Error trait and this method should not \
|
|
|
|
be exposed publicly",
|
|
|
|
issue = "0")]
|
2017-09-29 15:15:05 +00:00
|
|
|
#[inline]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn __description(&self) -> &str {
|
|
|
|
"could not convert slice to array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 19:45:26 +00:00
|
|
|
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
|
|
|
|
impl From<Infallible> for TryFromSliceError {
|
|
|
|
fn from(x: Infallible) -> TryFromSliceError {
|
|
|
|
match x {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 06:59:59 +00:00
|
|
|
#[cfg(bootstrap)]
|
2015-12-03 01:31:49 +00:00
|
|
|
macro_rules! __impl_slice_eq1 {
|
|
|
|
($Lhs: ty, $Rhs: ty) => {
|
|
|
|
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
|
|
|
|
};
|
|
|
|
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 06:59:59 +00:00
|
|
|
#[cfg(bootstrap)]
|
2015-12-03 01:31:49 +00:00
|
|
|
macro_rules! __impl_slice_eq2 {
|
|
|
|
($Lhs: ty, $Rhs: ty) => {
|
|
|
|
__impl_slice_eq2! { $Lhs, $Rhs, Sized }
|
|
|
|
};
|
|
|
|
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
|
|
|
|
__impl_slice_eq1!($Lhs, $Rhs, $Bound);
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq<A> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &$Lhs) -> bool { self[..] == other[..] }
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &$Lhs) -> bool { self[..] != other[..] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:04:02 +00:00
|
|
|
// macro for implementing n-element array functions and operations
|
2019-07-06 06:59:59 +00:00
|
|
|
#[cfg(bootstrap)]
|
2014-10-31 09:41:25 +00:00
|
|
|
macro_rules! array_impls {
|
|
|
|
($($N:expr)+) => {
|
|
|
|
$(
|
2016-09-28 10:28:42 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-24 23:37:08 +00:00
|
|
|
impl<T> AsRef<[T]> for [T; $N] {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
&self[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 10:28:42 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-24 23:37:08 +00:00
|
|
|
impl<T> AsMut<[T]> for [T; $N] {
|
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut [T] {
|
|
|
|
&mut self[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 16:35:26 +00:00
|
|
|
#[stable(feature = "array_borrow", since = "1.4.0")]
|
|
|
|
impl<T> Borrow<[T]> for [T; $N] {
|
|
|
|
fn borrow(&self) -> &[T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "array_borrow", since = "1.4.0")]
|
|
|
|
impl<T> BorrowMut<[T]> for [T; $N] {
|
|
|
|
fn borrow_mut(&mut self) -> &mut [T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2019-03-10 03:10:28 +00:00
|
|
|
impl<T> TryFrom<&[T]> for [T; $N] where T: Copy {
|
2018-11-25 16:33:16 +00:00
|
|
|
type Error = TryFromSliceError;
|
|
|
|
|
|
|
|
fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {
|
|
|
|
<&Self>::try_from(slice).map(|r| *r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-09-21 20:35:23 +00:00
|
|
|
impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
|
|
|
|
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(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-09-21 20:35:23 +00:00
|
|
|
impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
|
|
|
|
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(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:48:07 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T: Hash> Hash for [T; $N] {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
2015-02-18 23:58:07 +00:00
|
|
|
Hash::hash(&self[..], state)
|
2015-02-18 04:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 20:16:48 +00:00
|
|
|
|
2015-01-24 17:15:42 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 23:45:07 +00:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for [T; $N] {
|
2019-04-18 23:37:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-02-18 19:48:57 +00:00
|
|
|
fmt::Debug::fmt(&&self[..], f)
|
2014-11-14 11:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:06:24 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-13 22:55:10 +00:00
|
|
|
impl<'a, T> IntoIterator for &'a [T; $N] {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = Iter<'a, T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:06:24 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-08 03:01:05 +00:00
|
|
|
impl<'a, T> IntoIterator for &'a mut [T; $N] {
|
2015-02-13 22:55:10 +00:00
|
|
|
type Item = &'a mut T;
|
2015-02-06 22:47:55 +00:00
|
|
|
type IntoIter = IterMut<'a, T>;
|
2015-01-08 03:01:05 +00:00
|
|
|
|
|
|
|
fn into_iter(self) -> IterMut<'a, T> {
|
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:32:55 +00:00
|
|
|
// NOTE: some less important impls are omitted to reduce code bloat
|
|
|
|
__impl_slice_eq1! { [A; $N], [B; $N] }
|
|
|
|
__impl_slice_eq2! { [A; $N], [B] }
|
|
|
|
__impl_slice_eq2! { [A; $N], &'b [B] }
|
|
|
|
__impl_slice_eq2! { [A; $N], &'b mut [B] }
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
|
2014-11-21 05:14:05 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 04:40:24 +00:00
|
|
|
impl<T:Eq> Eq for [T; $N] { }
|
2014-10-31 09:41:25 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 04:40:24 +00:00
|
|
|
impl<T:PartialOrd> PartialOrd for [T; $N] {
|
2014-10-31 09:41:25 +00:00
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
|
2015-02-18 19:48:57 +00:00
|
|
|
PartialOrd::partial_cmp(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn lt(&self, other: &[T; $N]) -> bool {
|
2015-02-18 19:48:57 +00:00
|
|
|
PartialOrd::lt(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn le(&self, other: &[T; $N]) -> bool {
|
2015-02-18 19:48:57 +00:00
|
|
|
PartialOrd::le(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn ge(&self, other: &[T; $N]) -> bool {
|
2015-02-18 19:48:57 +00:00
|
|
|
PartialOrd::ge(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn gt(&self, other: &[T; $N]) -> bool {
|
2015-02-18 19:48:57 +00:00
|
|
|
PartialOrd::gt(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 04:40:24 +00:00
|
|
|
impl<T:Ord> Ord for [T; $N] {
|
2014-10-31 09:41:25 +00:00
|
|
|
#[inline]
|
2015-01-01 04:40:24 +00:00
|
|
|
fn cmp(&self, other: &[T; $N]) -> Ordering {
|
2015-02-18 19:48:57 +00:00
|
|
|
Ord::cmp(&&self[..], &&other[..])
|
2014-10-31 09:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 06:59:59 +00:00
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
mod impls_using_const_generics {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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, 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, 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, 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<'a, '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<'a, '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<'a, '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<'a, '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")]
|
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
pub trait LengthAtMost32 {}
|
|
|
|
|
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
macro_rules! array_impls {
|
|
|
|
($($N:literal)+) => {
|
|
|
|
$(
|
|
|
|
#[unstable(feature = "const_generic_impls_guard", issue = "0")]
|
|
|
|
impl<T> LengthAtMost32 for [T; $N] {}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 09:41:25 +00:00
|
|
|
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
|
|
|
|
}
|
2015-08-14 17:11:19 +00:00
|
|
|
|
|
|
|
// 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}
|