array_trait/array.rs
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
use super::*;
/// A trait for any array, with item as an associated type, and length as an assiciated constant.
///
/// # Example
///
/// ```rust
/// #![feature(const_trait_impl)]
/// #![feature(generic_const_exprs)]
///
/// use array_trait::*;
///
/// type Arr3 = [i8; 3];
///
/// const A: Arr3 = [1, 2, 3];
///
/// /// The trait can be used in a function like this:
/// const fn first<'a, T: ~const Array>(array: &'a T) -> Option<&'a <T as AsSlice>::Item>
/// where
/// [(); T::LENGTH]: // This is required for now.
/// {
/// array.as_array().first()
/// }
/// assert_eq!(first(&A), Some(&1));
///
/// // The assiciated constant `LENGTH` equals the length of the array
/// assert_eq!(Arr3::LENGTH, 3);
/// assert_eq!(Arr3::LENGTH, A.len());
/// ```
#[const_trait]
pub trait Array: private::Array + ArrayPrereq<<Self as AsSlice>::Item> + ~const AsArray + ~const IntoArray
/*where
for<'a> &'a Self: TryFrom<&'a [Self::Item]>
+ IntoIterator<Item = &'a Self::Item, IntoIter = Iter<'a, Self::Item>>,
for<'a> &'a mut Self: TryFrom<&'a mut [Self::Item]> + IntoIterator<Item = &'a mut Self::Item, IntoIter = IterMut<'a, Self::Item>>*/
{
}
impl<Item, const LENGTH: usize> const Array for [Item; LENGTH]
{
}
#[cfg(test)]
mod test
{
#[test]
fn it_works()
{
use crate::*;
type Arr3 = [i8; 3];
const A: Arr3 = [1, 2, 3];
/// The trait can be used in a function like this:
const fn first<'a, T: ~const Array>(array: &'a T) -> Option<&'a <T as AsSlice>::Item>
where
[(); T::LENGTH]: // This is required for now.
{
array.as_array().first()
}
assert_eq!(first(&A), Some(&1));
// The assiciated constant `LENGTH` equals the length of the array
assert_eq!(Arr3::LENGTH, 3);
}
}