diff options
Diffstat (limited to 'vendor/generic-array-0.12.4/src')
-rw-r--r-- | vendor/generic-array-0.12.4/src/arr.rs | 126 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/functional.rs | 94 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/hex.rs | 102 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/impl_serde.rs | 108 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/impls.rs | 182 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/iter.rs | 190 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/lib.rs | 632 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/src/sequence.rs | 320 |
8 files changed, 1754 insertions, 0 deletions
diff --git a/vendor/generic-array-0.12.4/src/arr.rs b/vendor/generic-array-0.12.4/src/arr.rs new file mode 100644 index 000000000..ebe687500 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/arr.rs @@ -0,0 +1,126 @@ +//! Implementation for `arr!` macro. + +use super::ArrayLength; +use core::ops::Add; +use typenum::U1; + +/// Helper trait for `arr!` macro +pub trait AddLength<T, N: ArrayLength<T>>: ArrayLength<T> { + /// Resulting length + type Output: ArrayLength<T>; +} + +impl<T, N1, N2> AddLength<T, N2> for N1 +where + N1: ArrayLength<T> + Add<N2>, + N2: ArrayLength<T>, + <N1 as Add<N2>>::Output: ArrayLength<T>, +{ + type Output = <N1 as Add<N2>>::Output; +} + +/// Helper type for `arr!` macro +pub type Inc<T, U> = <U as AddLength<T, U1>>::Output; + +#[doc(hidden)] +#[macro_export] +macro_rules! arr_impl { + (@replace_expr $e:expr)=>{ + 1 + }; + ($T:ty; $N:ty, [$($x:expr),*], []) => ({ + const __ARR_LENGTH:usize=0 $(+ $crate::arr_impl!(@replace_expr $x) )*; + fn __do_transmute<'a, T, N: $crate::ArrayLength<T>>(arr: [T; __ARR_LENGTH]) -> $crate::GenericArray<T, N> { + unsafe { $crate::transmute(arr) } + } + + let _:[();<$N as $crate::typenum::Unsigned>::USIZE]=[();__ARR_LENGTH]; + + __do_transmute::<$T,$N>([$($x),*]) + }); + ($T:ty; $N:ty, [], [$x1:expr]) => ( + $crate::arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1], []) + ); + ($T:ty; $N:ty, [], [$x1:expr, $($x:expr),+]) => ( + $crate::arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1], [$($x),+]) + ); + ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr]) => ( + $crate::arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1], []) + ); + ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr, $($x:expr),+]) => ( + $crate::arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1], [$($x),+]) + ); +} + +/// Macro allowing for easy generation of Generic Arrays. +/// Example: `let test = arr![u32; 1, 2, 3];` +#[macro_export] +macro_rules! arr { + ($T:ty; $(,)*) => ({ + unsafe { $crate::transmute::<[$T; 0], $crate::GenericArray<$T, $crate::typenum::U0>>([]) } + }); + ($T:ty; $($x:expr),* $(,)*) => ( + arr_impl!($T; $crate::typenum::U0, [], [$($x),*]) + ); + ($($x:expr,)+) => (arr![$($x),*]); + () => ("""Macro requires a type, e.g. `let array = arr![u32; 1, 2, 3];`") +} + + +mod doctests_only{ + /// + /// # With ellision + /// + /// Testing that lifetimes aren't transmuted when they're ellided. + /// + /// ```compile_fail + /// #[macro_use] extern crate generic_array; + /// fn main() { + /// fn unsound_lifetime_extension<'a, A>(a: &'a A) -> &'static A { + /// arr![&A; a][0] + /// } + /// } + /// ``` + /// + /// ```rust + /// #[macro_use] extern crate generic_array; + /// fn main() { + /// fn unsound_lifetime_extension<'a, A>(a: &'a A) -> &'a A { + /// arr![&A; a][0] + /// } + /// } + /// ``` + /// + /// # Without ellision + /// + /// Testing that lifetimes aren't transmuted when they're specified explicitly. + /// + /// ```compile_fail + /// #[macro_use] extern crate generic_array; + /// fn main() { + /// fn unsound_lifetime_extension<'a, A>(a: &'a A) -> &'static A { + /// arr![&'a A; a][0] + /// } + /// } + /// ``` + /// + /// ```compile_fail + /// #[macro_use] extern crate generic_array; + /// fn main() { + /// fn unsound_lifetime_extension<'a, A>(a: &'a A) -> &'static A { + /// arr![&'static A; a][0] + /// } + /// } + /// ``` + /// + /// ```rust + /// #[macro_use] extern crate generic_array; + /// fn main() { + /// fn unsound_lifetime_extension<'a, A>(a: &'a A) -> &'a A { + /// arr![&'a A; a][0] + /// } + /// } + /// ``` + #[allow(dead_code)] + pub enum DocTests{} +} diff --git a/vendor/generic-array-0.12.4/src/functional.rs b/vendor/generic-array-0.12.4/src/functional.rs new file mode 100644 index 000000000..50f4a71b8 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/functional.rs @@ -0,0 +1,94 @@ +//! Functional programming with generic sequences +//! +//! Please see `tests/generics.rs` for examples of how to best use these in your generic functions. + +use super::ArrayLength; +use core::iter::FromIterator; +use sequence::*; + +/// Defines the relationship between one generic sequence and another, +/// for operations such as `map` and `zip`. +pub unsafe trait MappedGenericSequence<T, U>: GenericSequence<T> +where + Self::Length: ArrayLength<U>, +{ + /// Mapped sequence type + type Mapped: GenericSequence<U, Length = Self::Length>; +} + +unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a S +where + &'a S: GenericSequence<T>, + S: GenericSequence<T, Length = <&'a S as GenericSequence<T>>::Length>, + <S as GenericSequence<T>>::Length: ArrayLength<U>, +{ + type Mapped = <S as MappedGenericSequence<T, U>>::Mapped; +} + +unsafe impl<'a, T, U, S: MappedGenericSequence<T, U>> MappedGenericSequence<T, U> for &'a mut S +where + &'a mut S: GenericSequence<T>, + S: GenericSequence<T, Length = <&'a mut S as GenericSequence<T>>::Length>, + <S as GenericSequence<T>>::Length: ArrayLength<U>, +{ + type Mapped = <S as MappedGenericSequence<T, U>>::Mapped; +} + +/// Accessor type for a mapped generic sequence +pub type MappedSequence<S, T, U> = + <<S as MappedGenericSequence<T, U>>::Mapped as GenericSequence<U>>::Sequence; + +/// Defines functional programming methods for generic sequences +pub unsafe trait FunctionalSequence<T>: GenericSequence<T> { + /// Maps a `GenericSequence` to another `GenericSequence`. + /// + /// If the mapping function panics, any already initialized elements in the new sequence + /// will be dropped, AND any unused elements in the source sequence will also be dropped. + fn map<U, F>(self, f: F) -> MappedSequence<Self, T, U> + where + Self: MappedGenericSequence<T, U>, + Self::Length: ArrayLength<U>, + F: FnMut(Self::Item) -> U, + { + FromIterator::from_iter(self.into_iter().map(f)) + } + + /// Combines two `GenericSequence` instances and iterates through both of them, + /// initializing a new `GenericSequence` with the result of the zipped mapping function. + /// + /// If the mapping function panics, any already initialized elements in the new sequence + /// will be dropped, AND any unused elements in the source sequences will also be dropped. + #[inline] + fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U> + where + Self: MappedGenericSequence<T, U>, + Rhs: MappedGenericSequence<B, U, Mapped = MappedSequence<Self, T, U>>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + Rhs: GenericSequence<B, Length = Self::Length>, + F: FnMut(Self::Item, Rhs::Item) -> U, + { + rhs.inverted_zip2(self, f) + } + + /// Folds (or reduces) a sequence of data into a single value. + /// + /// If the fold function panics, any unused elements will be dropped. + fn fold<U, F>(self, init: U, f: F) -> U + where + F: FnMut(U, Self::Item) -> U, + { + self.into_iter().fold(init, f) + } +} + +unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a S +where + &'a S: GenericSequence<T>, +{ +} + +unsafe impl<'a, T, S: GenericSequence<T>> FunctionalSequence<T> for &'a mut S +where + &'a mut S: GenericSequence<T>, +{ +} diff --git a/vendor/generic-array-0.12.4/src/hex.rs b/vendor/generic-array-0.12.4/src/hex.rs new file mode 100644 index 000000000..3ef92b1b9 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/hex.rs @@ -0,0 +1,102 @@ +//! Generic array are commonly used as a return value for hash digests, so +//! it's a good idea to allow to hexlify them easily. This module implements +//! `std::fmt::LowerHex` and `std::fmt::UpperHex` traits. +//! +//! Example: +//! +//! ```rust +//! # #[macro_use] +//! # extern crate generic_array; +//! # extern crate typenum; +//! # fn main() { +//! let array = arr![u8; 10, 20, 30]; +//! assert_eq!(format!("{:x}", array), "0a141e"); +//! # } +//! ``` +//! + +use {ArrayLength, GenericArray}; +use core::cmp::min; +use core::fmt; +use core::ops::Add; +use core::str; +use typenum::*; + +static LOWER_CHARS: &'static [u8] = b"0123456789abcdef"; +static UPPER_CHARS: &'static [u8] = b"0123456789ABCDEF"; + +impl<T: ArrayLength<u8>> fmt::LowerHex for GenericArray<u8, T> +where + T: Add<T>, + <T as Add<T>>::Output: ArrayLength<u8>, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let max_digits = f.precision().unwrap_or_else(|| self.len() * 2); + let max_hex = (max_digits >> 1) + (max_digits & 1); + + if T::to_usize() < 1024 { + // For small arrays use a stack allocated + // buffer of 2x number of bytes + let mut res = GenericArray::<u8, Sum<T, T>>::default(); + + for (i, c) in self.iter().take(max_hex).enumerate() { + res[i * 2] = LOWER_CHARS[(c >> 4) as usize]; + res[i * 2 + 1] = LOWER_CHARS[(c & 0xF) as usize]; + } + f.write_str(unsafe { str::from_utf8_unchecked(&res[..max_digits]) })?; + } else { + // For large array use chunks of up to 1024 bytes (2048 hex chars) + let mut buf = [0u8; 2048]; + let mut digits_left = max_digits; + + for chunk in self[..max_hex].chunks(1024) { + for (i, c) in chunk.iter().enumerate() { + buf[i * 2] = LOWER_CHARS[(c >> 4) as usize]; + buf[i * 2 + 1] = LOWER_CHARS[(c & 0xF) as usize]; + } + let n = min(chunk.len() * 2, digits_left); + f.write_str(unsafe { str::from_utf8_unchecked(&buf[..n]) })?; + digits_left -= n; + } + } + Ok(()) + } +} + +impl<T: ArrayLength<u8>> fmt::UpperHex for GenericArray<u8, T> +where + T: Add<T>, + <T as Add<T>>::Output: ArrayLength<u8>, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let max_digits = f.precision().unwrap_or_else(|| self.len() * 2); + let max_hex = (max_digits >> 1) + (max_digits & 1); + + if T::to_usize() < 1024 { + // For small arrays use a stack allocated + // buffer of 2x number of bytes + let mut res = GenericArray::<u8, Sum<T, T>>::default(); + + for (i, c) in self.iter().take(max_hex).enumerate() { + res[i * 2] = UPPER_CHARS[(c >> 4) as usize]; + res[i * 2 + 1] = UPPER_CHARS[(c & 0xF) as usize]; + } + f.write_str(unsafe { str::from_utf8_unchecked(&res[..max_digits]) })?; + } else { + // For large array use chunks of up to 1024 bytes (2048 hex chars) + let mut buf = [0u8; 2048]; + let mut digits_left = max_digits; + + for chunk in self[..max_hex].chunks(1024) { + for (i, c) in chunk.iter().enumerate() { + buf[i * 2] = UPPER_CHARS[(c >> 4) as usize]; + buf[i * 2 + 1] = UPPER_CHARS[(c & 0xF) as usize]; + } + let n = min(chunk.len() * 2, digits_left); + f.write_str(unsafe { str::from_utf8_unchecked(&buf[..n]) })?; + digits_left -= n; + } + } + Ok(()) + } +} diff --git a/vendor/generic-array-0.12.4/src/impl_serde.rs b/vendor/generic-array-0.12.4/src/impl_serde.rs new file mode 100644 index 000000000..da1df2fc0 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/impl_serde.rs @@ -0,0 +1,108 @@ +//! Serde serialization/deserialization implementation + +use core::fmt; +use core::marker::PhantomData; +use serde::de::{self, SeqAccess, Visitor}; +use serde::{ser::SerializeTuple, Deserialize, Deserializer, Serialize, Serializer}; +use {ArrayLength, GenericArray}; + +impl<T, N> Serialize for GenericArray<T, N> +where + T: Serialize, + N: ArrayLength<T>, +{ + #[inline] + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut tup = serializer.serialize_tuple(N::to_usize())?; + for el in self { + tup.serialize_element(el)?; + } + + tup.end() + } +} + +struct GAVisitor<T, N> { + _t: PhantomData<T>, + _n: PhantomData<N>, +} + +impl<'de, T, N> Visitor<'de> for GAVisitor<T, N> +where + T: Deserialize<'de> + Default, + N: ArrayLength<T>, +{ + type Value = GenericArray<T, N>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("struct GenericArray") + } + + fn visit_seq<A>(self, mut seq: A) -> Result<GenericArray<T, N>, A::Error> + where + A: SeqAccess<'de>, + { + let mut result = GenericArray::default(); + for i in 0..N::to_usize() { + result[i] = seq + .next_element()? + .ok_or_else(|| de::Error::invalid_length(i, &self))?; + } + Ok(result) + } +} + +impl<'de, T, N> Deserialize<'de> for GenericArray<T, N> +where + T: Deserialize<'de> + Default, + N: ArrayLength<T>, +{ + fn deserialize<D>(deserializer: D) -> Result<GenericArray<T, N>, D::Error> + where + D: Deserializer<'de>, + { + let visitor = GAVisitor { + _t: PhantomData, + _n: PhantomData, + }; + deserializer.deserialize_tuple(N::to_usize(), visitor) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use bincode; + use typenum; + + #[test] + fn test_serialize() { + let array = GenericArray::<u8, typenum::U2>::default(); + let serialized = bincode::serialize(&array); + assert!(serialized.is_ok()); + } + + #[test] + fn test_deserialize() { + let mut array = GenericArray::<u8, typenum::U2>::default(); + array[0] = 1; + array[1] = 2; + let serialized = bincode::serialize(&array).unwrap(); + let deserialized = bincode::deserialize::<GenericArray<u8, typenum::U2>>(&array); + assert!(deserialized.is_ok()); + let array = deserialized.unwrap(); + assert_eq!(array[0], 1); + assert_eq!(array[1], 2); + } + + #[test] + fn test_serialized_size() { + let array = GenericArray::<u8, typenum::U1>::default(); + let size = bincode::serialized_size(&array).unwrap(); + assert_eq!(size, 1); + } + +} diff --git a/vendor/generic-array-0.12.4/src/impls.rs b/vendor/generic-array-0.12.4/src/impls.rs new file mode 100644 index 000000000..ea5a3c4c9 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/impls.rs @@ -0,0 +1,182 @@ +use super::{ArrayLength, GenericArray}; +use core::borrow::{Borrow, BorrowMut}; +use core::cmp::Ordering; +use core::fmt::{self, Debug}; +use core::hash::{Hash, Hasher}; +use functional::*; +use sequence::*; + +impl<T: Default, N> Default for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline] + fn default() -> Self { + Self::generate(|_| T::default()) + } +} + +impl<T: Clone, N> Clone for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn clone(&self) -> GenericArray<T, N> { + self.map(Clone::clone) + } +} + +impl<T: Copy, N> Copy for GenericArray<T, N> +where + N: ArrayLength<T>, + N::ArrayType: Copy, +{ +} + +impl<T: PartialEq, N> PartialEq for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn eq(&self, other: &Self) -> bool { + **self == **other + } +} +impl<T: Eq, N> Eq for GenericArray<T, N> +where + N: ArrayLength<T>, +{ +} + +impl<T: PartialOrd, N> PartialOrd for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn partial_cmp(&self, other: &GenericArray<T, N>) -> Option<Ordering> { + PartialOrd::partial_cmp(self.as_slice(), other.as_slice()) + } +} + +impl<T: Ord, N> Ord for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn cmp(&self, other: &GenericArray<T, N>) -> Ordering { + Ord::cmp(self.as_slice(), other.as_slice()) + } +} + +impl<T: Debug, N> Debug for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + self[..].fmt(fmt) + } +} + +impl<T, N> Borrow<[T]> for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline(always)] + fn borrow(&self) -> &[T] { + &self[..] + } +} + +impl<T, N> BorrowMut<[T]> for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline(always)] + fn borrow_mut(&mut self) -> &mut [T] { + &mut self[..] + } +} + +impl<T, N> AsRef<[T]> for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline(always)] + fn as_ref(&self) -> &[T] { + &self[..] + } +} + +impl<T, N> AsMut<[T]> for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline(always)] + fn as_mut(&mut self) -> &mut [T] { + &mut self[..] + } +} + +impl<T: Hash, N> Hash for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn hash<H>(&self, state: &mut H) + where + H: Hasher, + { + Hash::hash(&self[..], state) + } +} + +macro_rules! impl_from { + ($($n: expr => $ty: ty),*) => { + $( + impl<T> From<[T; $n]> for GenericArray<T, $ty> { + #[inline(always)] + fn from(arr: [T; $n]) -> Self { + unsafe { $crate::transmute(arr) } + } + } + + impl<T> Into<[T; $n]> for GenericArray<T, $ty> { + #[inline(always)] + fn into(self) -> [T; $n] { + unsafe { $crate::transmute(self) } + } + } + )* + + } +} + +impl_from! { + 1 => ::typenum::U1, + 2 => ::typenum::U2, + 3 => ::typenum::U3, + 4 => ::typenum::U4, + 5 => ::typenum::U5, + 6 => ::typenum::U6, + 7 => ::typenum::U7, + 8 => ::typenum::U8, + 9 => ::typenum::U9, + 10 => ::typenum::U10, + 11 => ::typenum::U11, + 12 => ::typenum::U12, + 13 => ::typenum::U13, + 14 => ::typenum::U14, + 15 => ::typenum::U15, + 16 => ::typenum::U16, + 17 => ::typenum::U17, + 18 => ::typenum::U18, + 19 => ::typenum::U19, + 20 => ::typenum::U20, + 21 => ::typenum::U21, + 22 => ::typenum::U22, + 23 => ::typenum::U23, + 24 => ::typenum::U24, + 25 => ::typenum::U25, + 26 => ::typenum::U26, + 27 => ::typenum::U27, + 28 => ::typenum::U28, + 29 => ::typenum::U29, + 30 => ::typenum::U30, + 31 => ::typenum::U31, + 32 => ::typenum::U32 +} diff --git a/vendor/generic-array-0.12.4/src/iter.rs b/vendor/generic-array-0.12.4/src/iter.rs new file mode 100644 index 000000000..a2d67fc67 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/iter.rs @@ -0,0 +1,190 @@ +//! `GenericArray` iterator implementation. + +use super::{ArrayLength, GenericArray}; +use core::{cmp, ptr, fmt, mem}; +use core::mem::ManuallyDrop; + +/// An iterator that moves out of a `GenericArray` +pub struct GenericArrayIter<T, N: ArrayLength<T>> { + // Invariants: index <= index_back <= N + // Only values in array[index..index_back] are alive at any given time. + // Values from array[..index] and array[index_back..] are already moved/dropped. + array: ManuallyDrop<GenericArray<T, N>>, + index: usize, + index_back: usize, +} + +#[cfg(test)] +mod test { + use super::*; + + fn send<I: Send>(_iter: I) {} + + #[test] + fn test_send_iter() { + send(GenericArray::from([1, 2, 3, 4]).into_iter()); + } +} + +impl<T, N> GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + /// Returns the remaining items of this iterator as a slice + #[inline] + pub fn as_slice(&self) -> &[T] { + &self.array.as_slice()[self.index..self.index_back] + } + + /// Returns the remaining items of this iterator as a mutable slice + #[inline] + pub fn as_mut_slice(&mut self) -> &mut [T] { + &mut self.array.as_mut_slice()[self.index..self.index_back] + } +} + +impl<T, N> IntoIterator for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + type Item = T; + type IntoIter = GenericArrayIter<T, N>; + + fn into_iter(self) -> Self::IntoIter { + GenericArrayIter { + array: ManuallyDrop::new(self), + index: 0, + index_back: N::to_usize(), + } + } +} + +// Based on work in rust-lang/rust#49000 +impl<T: fmt::Debug, N> fmt::Debug for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("GenericArrayIter") + .field(&self.as_slice()) + .finish() + } +} + +impl<T, N> Drop for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + #[inline] + fn drop(&mut self) { + // Drop values that are still alive. + for p in self.as_mut_slice() { + unsafe { + ptr::drop_in_place(p); + } + } + } +} + +// Based on work in rust-lang/rust#49000 +impl<T: Clone, N> Clone for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + fn clone(&self) -> Self { + // This places all cloned elements at the start of the new array iterator, + // not at their original indices. + unsafe { + let mut iter = GenericArrayIter { + array: ManuallyDrop::new(mem::uninitialized()), + index: 0, + index_back: 0, + }; + + for (dst, src) in iter.array.iter_mut().zip(self.as_slice()) { + ptr::write(dst, src.clone()); + + iter.index_back += 1; + } + + iter + } + } +} + +impl<T, N> Iterator for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + type Item = T; + + #[inline] + fn next(&mut self) -> Option<T> { + if self.index < self.index_back { + let p = unsafe { Some(ptr::read(self.array.get_unchecked(self.index))) }; + + self.index += 1; + + p + } else { + None + } + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + let len = self.len(); + (len, Some(len)) + } + + #[inline] + fn count(self) -> usize { + self.len() + } + + fn nth(&mut self, n: usize) -> Option<T> { + // First consume values prior to the nth. + let ndrop = cmp::min(n, self.len()); + + for p in &mut self.array[self.index..self.index + ndrop] { + self.index += 1; + + unsafe { + ptr::drop_in_place(p); + } + } + + self.next() + } + + fn last(mut self) -> Option<T> { + // Note, everything else will correctly drop first as `self` leaves scope. + self.next_back() + } +} + +impl<T, N> DoubleEndedIterator for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + fn next_back(&mut self) -> Option<T> { + if self.index < self.index_back { + self.index_back -= 1; + + unsafe { Some(ptr::read(self.array.get_unchecked(self.index_back))) } + } else { + None + } + } +} + +impl<T, N> ExactSizeIterator for GenericArrayIter<T, N> +where + N: ArrayLength<T>, +{ + fn len(&self) -> usize { + self.index_back - self.index + } +} + +// TODO: Implement `FusedIterator` and `TrustedLen` when stabilized
\ No newline at end of file diff --git a/vendor/generic-array-0.12.4/src/lib.rs b/vendor/generic-array-0.12.4/src/lib.rs new file mode 100644 index 000000000..e98e8fd58 --- /dev/null +++ b/vendor/generic-array-0.12.4/src/lib.rs @@ -0,0 +1,632 @@ +//! This crate implements a structure that can be used as a generic array type.use +//! Core Rust array types `[T; N]` can't be used generically with +//! respect to `N`, so for example this: +//! +//! ```{should_fail} +//! struct Foo<T, N> { +//! data: [T; N] +//! } +//! ``` +//! +//! won't work. +//! +//! **generic-array** exports a `GenericArray<T,N>` type, which lets +//! the above be implemented as: +//! +//! ``` +//! # use generic_array::{ArrayLength, GenericArray}; +//! struct Foo<T, N: ArrayLength<T>> { +//! data: GenericArray<T,N> +//! } +//! ``` +//! +//! The `ArrayLength<T>` trait is implemented by default for +//! [unsigned integer types](../typenum/uint/index.html) from +//! [typenum](../typenum/index.html). +//! +//! For ease of use, an `arr!` macro is provided - example below: +//! +//! ``` +//! # #[macro_use] +//! # extern crate generic_array; +//! # extern crate typenum; +//! # fn main() { +//! let array = arr![u32; 1, 2, 3]; +//! assert_eq!(array[2], 3); +//! # } +//! ``` + +#![deny(missing_docs)] +#![no_std] + +#[cfg(feature = "serde")] +extern crate serde; + +#[cfg(test)] +extern crate bincode; + +pub extern crate typenum; + +mod hex; +mod impls; + +#[cfg(feature = "serde")] +pub mod impl_serde; + +use core::iter::FromIterator; +use core::marker::PhantomData; +use core::mem::ManuallyDrop; +use core::ops::{Deref, DerefMut}; +use core::{mem, ptr, slice}; +use typenum::bit::{B0, B1}; +use typenum::uint::{UInt, UTerm, Unsigned}; + +#[cfg_attr(test, macro_use)] +pub mod arr; +pub mod functional; +pub mod iter; +pub mod sequence; + +use functional::*; +pub use iter::GenericArrayIter; +use sequence::*; + +/// Trait making `GenericArray` work, marking types to be used as length of an array +pub unsafe trait ArrayLength<T>: Unsigned { + /// Associated type representing the array type for the number + type ArrayType; +} + +unsafe impl<T> ArrayLength<T> for UTerm { + #[doc(hidden)] + type ArrayType = (); +} + +/// Internal type used to generate a struct of appropriate size +#[allow(dead_code)] +#[repr(C)] +#[doc(hidden)] +pub struct GenericArrayImplEven<T, U> { + parent1: U, + parent2: U, + _marker: PhantomData<T>, +} + +impl<T: Clone, U: Clone> Clone for GenericArrayImplEven<T, U> { + fn clone(&self) -> GenericArrayImplEven<T, U> { + GenericArrayImplEven { + parent1: self.parent1.clone(), + parent2: self.parent2.clone(), + _marker: PhantomData, + } + } +} + +impl<T: Copy, U: Copy> Copy for GenericArrayImplEven<T, U> {} + +/// Internal type used to generate a struct of appropriate size +#[allow(dead_code)] +#[repr(C)] +#[doc(hidden)] +pub struct GenericArrayImplOdd<T, U> { + parent1: U, + parent2: U, + data: T, +} + +impl<T: Clone, U: Clone> Clone for GenericArrayImplOdd<T, U> { + fn clone(&self) -> GenericArrayImplOdd<T, U> { + GenericArrayImplOdd { + parent1: self.parent1.clone(), + parent2: self.parent2.clone(), + data: self.data.clone(), + } + } +} + +impl<T: Copy, U: Copy> Copy for GenericArrayImplOdd<T, U> {} + +unsafe impl<T, N: ArrayLength<T>> ArrayLength<T> for UInt<N, B0> { + #[doc(hidden)] + type ArrayType = GenericArrayImplEven<T, N::ArrayType>; +} + +unsafe impl<T, N: ArrayLength<T>> ArrayLength<T> for UInt<N, B1> { + #[doc(hidden)] + type ArrayType = GenericArrayImplOdd<T, N::ArrayType>; +} + +/// Struct representing a generic array - `GenericArray<T, N>` works like [T; N] +#[allow(dead_code)] +pub struct GenericArray<T, U: ArrayLength<T>> { + data: U::ArrayType, +} + +unsafe impl<T: Send, N: ArrayLength<T>> Send for GenericArray<T, N> {} +unsafe impl<T: Sync, N: ArrayLength<T>> Sync for GenericArray<T, N> {} + +impl<T, N> Deref for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + type Target = [T]; + + #[inline(always)] + fn deref(&self) -> &[T] { + unsafe { slice::from_raw_parts(self as *const Self as *const T, N::to_usize()) } + } +} + +impl<T, N> DerefMut for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + #[inline(always)] + fn deref_mut(&mut self) -> &mut [T] { + unsafe { slice::from_raw_parts_mut(self as *mut Self as *mut T, N::to_usize()) } + } +} + +/// Creates an array one element at a time using a mutable iterator +/// you can write to with `ptr::write`. +/// +/// Incremenent the position while iterating to mark off created elements, +/// which will be dropped if `into_inner` is not called. +#[doc(hidden)] +pub struct ArrayBuilder<T, N: ArrayLength<T>> { + array: ManuallyDrop<GenericArray<T, N>>, + position: usize, +} + +impl<T, N: ArrayLength<T>> ArrayBuilder<T, N> { + #[doc(hidden)] + #[inline] + pub unsafe fn new() -> ArrayBuilder<T, N> { + ArrayBuilder { + array: ManuallyDrop::new(mem::uninitialized()), + position: 0, + } + } + + /// Creates a mutable iterator for writing to the array using `ptr::write`. + /// + /// Increment the position value given as a mutable reference as you iterate + /// to mark how many elements have been created. + #[doc(hidden)] + #[inline] + pub unsafe fn iter_position(&mut self) -> (slice::IterMut<T>, &mut usize) { + (self.array.iter_mut(), &mut self.position) + } + + /// When done writing (assuming all elements have been written to), + /// get the inner array. + #[doc(hidden)] + #[inline] + pub unsafe fn into_inner(self) -> GenericArray<T, N> { + let array = ptr::read(&self.array); + + mem::forget(self); + + ManuallyDrop::into_inner(array) + } +} + +impl<T, N: ArrayLength<T>> Drop for ArrayBuilder<T, N> { + fn drop(&mut self) { + for value in &mut self.array[..self.position] { + unsafe { + ptr::drop_in_place(value); + } + } + } +} + +/// Consumes an array. +/// +/// Increment the position while iterating and any leftover elements +/// will be dropped if position does not go to N +#[doc(hidden)] +pub struct ArrayConsumer<T, N: ArrayLength<T>> { + array: ManuallyDrop<GenericArray<T, N>>, + position: usize, +} + +impl<T, N: ArrayLength<T>> ArrayConsumer<T, N> { + #[doc(hidden)] + #[inline] + pub unsafe fn new(array: GenericArray<T, N>) -> ArrayConsumer<T, N> { + ArrayConsumer { + array: ManuallyDrop::new(array), + position: 0, + } + } + + /// Creates an iterator and mutable reference to the internal position + /// to keep track of consumed elements. + /// + /// Increment the position as you iterate to mark off consumed elements + #[doc(hidden)] + #[inline] + pub unsafe fn iter_position(&mut self) -> (slice::Iter<T>, &mut usize) { + (self.array.iter(), &mut self.position) + } +} + +impl<T, N: ArrayLength<T>> Drop for ArrayConsumer<T, N> { + fn drop(&mut self) { + for value in &mut self.array[self.position..N::to_usize()] { + unsafe { + ptr::drop_in_place(value); + } + } + } +} + +impl<'a, T: 'a, N> IntoIterator for &'a GenericArray<T, N> +where + N: ArrayLength<T>, +{ + type IntoIter = slice::Iter<'a, T>; + type Item = &'a T; + + fn into_iter(self: &'a GenericArray<T, N>) -> Self::IntoIter { + self.as_slice().iter() + } +} + +impl<'a, T: 'a, N> IntoIterator for &'a mut GenericArray<T, N> +where + N: ArrayLength<T>, +{ + type IntoIter = slice::IterMut<'a, T>; + type Item = &'a mut T; + + fn into_iter(self: &'a mut GenericArray<T, N>) -> Self::IntoIter { + self.as_mut_slice().iter_mut() + } +} + +impl<T, N> FromIterator<T> for GenericArray<T, N> +where + N: ArrayLength<T>, +{ + fn from_iter<I>(iter: I) -> GenericArray<T, N> + where + I: IntoIterator<Item = T>, + { + unsafe { + let mut destination = ArrayBuilder::new(); + + { + let (destination_iter, position) = destination.iter_position(); + + for (src, dst) in iter.into_iter().zip(destination_iter) { + ptr::write(dst, src); + + *position += 1; + } + } + + if destination.position < N::to_usize() { + from_iter_length_fail(destination.position, N::to_usize()); + } + + destination.into_inner() + } + } +} + +#[inline(never)] +#[cold] +fn from_iter_length_fail(length: usize, expected: usize) -> ! { + panic!( + "GenericArray::from_iter received {} elements but expected {}", + length, expected + ); +} + +unsafe impl<T, N> GenericSequence<T> for GenericArray<T, N> +where + N: ArrayLength<T>, + Self: IntoIterator<Item = T>, +{ + type Length = N; + type Sequence = Self; + + fn generate<F>(mut f: F) -> GenericArray<T, N> + where + F: FnMut(usize) -> T, + { + unsafe { + let mut destination = ArrayBuilder::new(); + + { + let (destination_iter, position) = destination.iter_position(); + + for (i, dst) in destination_iter.enumerate() { + ptr::write(dst, f(i)); + + *position += 1; + } + } + + destination.into_inner() + } + } + + #[doc(hidden)] + fn inverted_zip<B, U, F>( + self, + lhs: GenericArray<B, Self::Length>, + mut f: F, + ) -> MappedSequence<GenericArray<B, Self::Length>, B, U> + where + GenericArray<B, Self::Length>: + GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>, + Self: MappedGenericSequence<T, U>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + F: FnMut(B, Self::Item) -> U, + { + unsafe { + let mut left = ArrayConsumer::new(lhs); + let mut right = ArrayConsumer::new(self); + + let (left_array_iter, left_position) = left.iter_position(); + let (right_array_iter, right_position) = right.iter_position(); + + FromIterator::from_iter(left_array_iter.zip(right_array_iter).map(|(l, r)| { + let left_value = ptr::read(l); + let right_value = ptr::read(r); + + *left_position += 1; + *right_position += 1; + + f(left_value, right_value) + })) + } + } + + #[doc(hidden)] + fn inverted_zip2<B, Lhs, U, F>(self, lhs: Lhs, mut f: F) -> MappedSequence<Lhs, B, U> + where + Lhs: GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>, + Self: MappedGenericSequence<T, U>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + F: FnMut(Lhs::Item, Self::Item) -> U, + { + unsafe { + let mut right = ArrayConsumer::new(self); + + let (right_array_iter, right_position) = right.iter_position(); + + FromIterator::from_iter( + lhs.into_iter() + .zip(right_array_iter) + .map(|(left_value, r)| { + let right_value = ptr::read(r); + + *right_position += 1; + + f(left_value, right_value) + }), + ) + } + } +} + +unsafe impl<T, U, N> MappedGenericSequence<T, U> for GenericArray<T, N> +where + N: ArrayLength<T> + ArrayLength<U>, + GenericArray<U, N>: GenericSequence<U, Length = N>, +{ + type Mapped = GenericArray<U, N>; +} + +unsafe impl<T, N> FunctionalSequence<T> for GenericArray<T, N> +where + N: ArrayLength<T>, + Self: GenericSequence<T, Item = T, Length = N>, +{ + fn map<U, F>(self, mut f: F) -> MappedSequence<Self, T, U> + where + Self::Length: ArrayLength<U>, + Self: MappedGenericSequence<T, U>, + F: FnMut(T) -> U, + { + unsafe { + let mut source = ArrayConsumer::new(self); + + let (array_iter, position) = source.iter_position(); + + FromIterator::from_iter(array_iter.map(|src| { + let value = ptr::read(src); + + *position += 1; + + f(value) + })) + } + } + + #[inline] + fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U> + where + Self: MappedGenericSequence<T, U>, + Rhs: MappedGenericSequence<B, U, Mapped = MappedSequence<Self, T, U>>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + Rhs: GenericSequence<B, Length = Self::Length>, + F: FnMut(T, Rhs::Item) -> U, + { + rhs.inverted_zip(self, f) + } + + fn fold<U, F>(self, init: U, mut f: F) -> U + where + F: FnMut(U, T) -> U, + { + unsafe { + let mut source = ArrayConsumer::new(self); + + let (array_iter, position) = source.iter_position(); + + array_iter.fold(init, |acc, src| { + let value = ptr::read(src); + + *position += 1; + + f(acc, value) + }) + } + } +} + +impl<T, N> GenericArray<T, N> +where + N: ArrayLength<T>, +{ + /// Extracts a slice containing the entire array. + #[inline] + pub fn as_slice(&self) -> &[T] { + self.deref() + } + + /// Extracts a mutable slice containing the entire array. + #[inline] + pub fn as_mut_slice(&mut self) -> &mut [T] { + self.deref_mut() + } + + /// Converts slice to a generic array reference with inferred length; + /// + /// Length of the slice must be equal to the length of the array. + #[inline] + pub fn from_slice(slice: &[T]) -> &GenericArray<T, N> { + slice.into() + } + + /// Converts mutable slice to a mutable generic array reference + /// + /// Length of the slice must be equal to the length of the array. + #[inline] + pub fn from_mut_slice(slice: &mut [T]) -> &mut GenericArray<T, N> { + slice.into() + } +} + +impl<'a, T, N: ArrayLength<T>> From<&'a [T]> for &'a GenericArray<T, N> { + /// Converts slice to a generic array reference with inferred length; + /// + /// Length of the slice must be equal to the length of the array. + #[inline] + fn from(slice: &[T]) -> &GenericArray<T, N> { + assert_eq!(slice.len(), N::to_usize()); + + unsafe { &*(slice.as_ptr() as *const GenericArray<T, N>) } + } +} + +impl<'a, T, N: ArrayLength<T>> From<&'a mut [T]> for &'a mut GenericArray<T, N> { + /// Converts mutable slice to a mutable generic array reference + /// + /// Length of the slice must be equal to the length of the array. + #[inline] + fn from(slice: &mut [T]) -> &mut GenericArray<T, N> { + assert_eq!(slice.len(), N::to_usize()); + + unsafe { &mut *(slice.as_mut_ptr() as *mut GenericArray<T, N>) } + } +} + +impl<T: Clone, N> GenericArray<T, N> +where + N: ArrayLength<T>, +{ + /// Construct a `GenericArray` from a slice by cloning its content + /// + /// Length of the slice must be equal to the length of the array + #[inline] + pub fn clone_from_slice(list: &[T]) -> GenericArray<T, N> { + Self::from_exact_iter(list.iter().cloned()) + .expect("Slice must be the same length as the array") + } +} + +impl<T, N> GenericArray<T, N> +where + N: ArrayLength<T>, +{ + /// Creates a new `GenericArray` instance from an iterator with a known exact size. + /// + /// Returns `None` if the size is not equal to the number of elements in the `GenericArray`. + pub fn from_exact_iter<I>(iter: I) -> Option<Self> + where + I: IntoIterator<Item = T>, + <I as IntoIterator>::IntoIter: ExactSizeIterator, + { + let iter = iter.into_iter(); + + if iter.len() == N::to_usize() { + unsafe { + let mut destination = ArrayBuilder::new(); + + { + let (destination_iter, position) = destination.iter_position(); + + for (dst, src) in destination_iter.zip(iter.into_iter()) { + ptr::write(dst, src); + + *position += 1; + } + } + + Some(destination.into_inner()) + } + } else { + None + } + } +} + +/// A reimplementation of the `transmute` function, avoiding problems +/// when the compiler can't prove equal sizes. +#[inline] +#[doc(hidden)] +pub unsafe fn transmute<A, B>(a: A) -> B { + let b = ::core::ptr::read(&a as *const A as *const B); + ::core::mem::forget(a); + b +} + +#[cfg(test)] +mod test { + // Compile with: + // cargo rustc --lib --profile test --release -- + // -C target-cpu=native -C opt-level=3 --emit asm + // and view the assembly to make sure test_assembly generates + // SIMD instructions instead of a niave loop. + + #[inline(never)] + pub fn black_box<T>(val: T) -> T { + use core::{mem, ptr}; + + let ret = unsafe { ptr::read_volatile(&val) }; + mem::forget(val); + ret + } + + #[test] + fn test_assembly() { + use functional::*; + + let a = black_box(arr![i32; 1, 3, 5, 7]); + let b = black_box(arr![i32; 2, 4, 6, 8]); + + let c = (&a).zip(b, |l, r| l + r); + + let d = a.fold(0, |a, x| a + x); + + assert_eq!(c, arr![i32; 3, 7, 11, 15]); + + assert_eq!(d, 16); + } +} diff --git a/vendor/generic-array-0.12.4/src/sequence.rs b/vendor/generic-array-0.12.4/src/sequence.rs new file mode 100644 index 000000000..7b928abda --- /dev/null +++ b/vendor/generic-array-0.12.4/src/sequence.rs @@ -0,0 +1,320 @@ +//! Useful traits for manipulating sequences of data stored in `GenericArray`s + +use super::*; +use core::{mem, ptr}; +use core::ops::{Add, Sub}; +use typenum::operator_aliases::*; + +/// Defines some sequence with an associated length and iteration capabilities. +/// +/// This is useful for passing N-length generic arrays as generics. +pub unsafe trait GenericSequence<T>: Sized + IntoIterator { + /// `GenericArray` associated length + type Length: ArrayLength<T>; + + /// Concrete sequence type used in conjuction with reference implementations of `GenericSequence` + type Sequence: GenericSequence<T, Length = Self::Length> + FromIterator<T>; + + /// Initializes a new sequence instance using the given function. + /// + /// If the generator function panics while initializing the sequence, + /// any already initialized elements will be dropped. + fn generate<F>(f: F) -> Self::Sequence + where + F: FnMut(usize) -> T; + + #[doc(hidden)] + fn inverted_zip<B, U, F>( + self, + lhs: GenericArray<B, Self::Length>, + mut f: F, + ) -> MappedSequence<GenericArray<B, Self::Length>, B, U> + where + GenericArray<B, Self::Length>: GenericSequence<B, Length = Self::Length> + + MappedGenericSequence<B, U>, + Self: MappedGenericSequence<T, U>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + F: FnMut(B, Self::Item) -> U, + { + unsafe { + let mut left = ArrayConsumer::new(lhs); + + let (left_array_iter, left_position) = left.iter_position(); + + FromIterator::from_iter( + left_array_iter + .zip(self.into_iter()) + .map(|(l, right_value)| { + let left_value = ptr::read(l); + + *left_position += 1; + + f(left_value, right_value) + }) + ) + } + } + + #[doc(hidden)] + fn inverted_zip2<B, Lhs, U, F>(self, lhs: Lhs, mut f: F) -> MappedSequence<Lhs, B, U> + where + Lhs: GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>, + Self: MappedGenericSequence<T, U>, + Self::Length: ArrayLength<B> + ArrayLength<U>, + F: FnMut(Lhs::Item, Self::Item) -> U, + { + FromIterator::from_iter(lhs.into_iter().zip(self.into_iter()).map(|(l, r)| f(l, r))) + } +} + +/// Accessor for `GenericSequence` item type, which is really `IntoIterator::Item` +/// +/// For deeply nested generic mapped sequence types, like shown in `tests/generics.rs`, +/// this can be useful for keeping things organized. +pub type SequenceItem<T> = <T as IntoIterator>::Item; + +unsafe impl<'a, T: 'a, S: GenericSequence<T>> GenericSequence<T> for &'a S +where + &'a S: IntoIterator, +{ + type Length = S::Length; + type Sequence = S::Sequence; + + #[inline] + fn generate<F>(f: F) -> Self::Sequence + where + F: FnMut(usize) -> T, + { + S::generate(f) + } +} + +unsafe impl<'a, T: 'a, S: GenericSequence<T>> GenericSequence<T> for &'a mut S +where + &'a mut S: IntoIterator, +{ + type Length = S::Length; + type Sequence = S::Sequence; + + #[inline] + fn generate<F>(f: F) -> Self::Sequence + where + F: FnMut(usize) -> T, + { + S::generate(f) + } +} + +/// Defines any `GenericSequence` which can be lengthened or extended by appending +/// or prepending an element to it. +/// +/// Any lengthened sequence can be shortened back to the original using `pop_front` or `pop_back` +pub unsafe trait Lengthen<T>: Sized + GenericSequence<T> { + /// `GenericSequence` that has one more element than `Self` + type Longer: Shorten<T, Shorter = Self>; + + /// Returns a new array with the given element appended to the end of it. + /// + /// Example: + /// + /// ```ignore + /// let a = arr![i32; 1, 2, 3]; + /// + /// let b = a.append(4); + /// + /// assert_eq!(b, arr![i32; 1, 2, 3, 4]); + /// ``` + fn append(self, last: T) -> Self::Longer; + + /// Returns a new array with the given element prepended to the front of it. + /// + /// Example: + /// + /// ```ignore + /// let a = arr![i32; 1, 2, 3]; + /// + /// let b = a.prepend(4); + /// + /// assert_eq!(b, arr![i32; 4, 1, 2, 3]); + /// ``` + fn prepend(self, first: T) -> Self::Longer; +} + +/// Defines a `GenericSequence` which can be shortened by removing the first or last element from it. +/// +/// Additionally, any shortened sequence can be lengthened by +/// appending or prepending an element to it. +pub unsafe trait Shorten<T>: Sized + GenericSequence<T> { + /// `GenericSequence` that has one less element than `Self` + type Shorter: Lengthen<T, Longer = Self>; + + /// Returns a new array without the last element, and the last element. + /// + /// Example: + /// + /// ```ignore + /// let a = arr![i32; 1, 2, 3, 4]; + /// + /// let (init, last) = a.pop_back(); + /// + /// assert_eq!(init, arr![i32; 1, 2, 3]); + /// assert_eq!(last, 4); + /// ``` + fn pop_back(self) -> (Self::Shorter, T); + + /// Returns a new array without the first element, and the first element. + /// Example: + /// + /// ```ignore + /// let a = arr![i32; 1, 2, 3, 4]; + /// + /// let (head, tail) = a.pop_front(); + /// + /// assert_eq!(head, 1); + /// assert_eq!(tail, arr![i32; 2, 3, 4]); + /// ``` + fn pop_front(self) -> (T, Self::Shorter); +} + +unsafe impl<T, N: ArrayLength<T>> Lengthen<T> for GenericArray<T, N> +where + N: Add<B1>, + Add1<N>: ArrayLength<T>, + Add1<N>: Sub<B1, Output = N>, + Sub1<Add1<N>>: ArrayLength<T>, +{ + type Longer = GenericArray<T, Add1<N>>; + + fn append(self, last: T) -> Self::Longer { + let mut longer: Self::Longer = unsafe { mem::uninitialized() }; + + unsafe { + ptr::write(longer.as_mut_ptr() as *mut _, self); + ptr::write(&mut longer[N::to_usize()], last); + } + + longer + } + + fn prepend(self, first: T) -> Self::Longer { + let mut longer: Self::Longer = unsafe { mem::uninitialized() }; + + let longer_ptr = longer.as_mut_ptr(); + + unsafe { + ptr::write(longer_ptr as *mut _, first); + ptr::write(longer_ptr.offset(1) as *mut _, self); + } + + longer + } +} + +unsafe impl<T, N: ArrayLength<T>> Shorten<T> for GenericArray<T, N> +where + N: Sub<B1>, + Sub1<N>: ArrayLength<T>, + Sub1<N>: Add<B1, Output = N>, + Add1<Sub1<N>>: ArrayLength<T>, +{ + type Shorter = GenericArray<T, Sub1<N>>; + + fn pop_back(self) -> (Self::Shorter, T) { + let init_ptr = self.as_ptr(); + let last_ptr = unsafe { init_ptr.offset(Sub1::<N>::to_usize() as isize) }; + + let init = unsafe { ptr::read(init_ptr as _) }; + let last = unsafe { ptr::read(last_ptr as _) }; + + mem::forget(self); + + (init, last) + } + + fn pop_front(self) -> (T, Self::Shorter) { + let head_ptr = self.as_ptr(); + let tail_ptr = unsafe { head_ptr.offset(1) }; + + let head = unsafe { ptr::read(head_ptr as _) }; + let tail = unsafe { ptr::read(tail_ptr as _) }; + + mem::forget(self); + + (head, tail) + } +} + +/// Defines a `GenericSequence` that can be split into two parts at a given pivot index. +pub unsafe trait Split<T, K>: GenericSequence<T> +where + K: ArrayLength<T>, +{ + /// First part of the resulting split array + type First: GenericSequence<T>; + /// Second part of the resulting split array + type Second: GenericSequence<T>; + + /// Splits an array at the given index, returning the separate parts of the array. + fn split(self) -> (Self::First, Self::Second); +} + +unsafe impl<T, N, K> Split<T, K> for GenericArray<T, N> +where + N: ArrayLength<T>, + K: ArrayLength<T>, + N: Sub<K>, + Diff<N, K>: ArrayLength<T>, +{ + type First = GenericArray<T, K>; + type Second = GenericArray<T, Diff<N, K>>; + + fn split(self) -> (Self::First, Self::Second) { + let head_ptr = self.as_ptr(); + let tail_ptr = unsafe { head_ptr.offset(K::to_usize() as isize) }; + + let head = unsafe { ptr::read(head_ptr as _) }; + let tail = unsafe { ptr::read(tail_ptr as _) }; + + mem::forget(self); + + (head, tail) + } +} + +/// Defines `GenericSequence`s which can be joined together, forming a larger array. +pub unsafe trait Concat<T, M>: GenericSequence<T> +where + M: ArrayLength<T>, +{ + /// Sequence to be concatenated with `self` + type Rest: GenericSequence<T, Length = M>; + + /// Resulting sequence formed by the concatenation. + type Output: GenericSequence<T>; + + /// Concatenate, or join, two sequences. + fn concat(self, rest: Self::Rest) -> Self::Output; +} + +unsafe impl<T, N, M> Concat<T, M> for GenericArray<T, N> +where + N: ArrayLength<T> + Add<M>, + M: ArrayLength<T>, + Sum<N, M>: ArrayLength<T>, +{ + type Rest = GenericArray<T, M>; + type Output = GenericArray<T, Sum<N, M>>; + + fn concat(self, rest: Self::Rest) -> Self::Output { + let mut output: Self::Output = unsafe { mem::uninitialized() }; + + let output_ptr = output.as_mut_ptr(); + + unsafe { + ptr::write(output_ptr as *mut _, self); + ptr::write(output_ptr.offset(N::to_usize() as isize) as *mut _, rest); + } + + output + } +} |