array_trait/
lib.rs

1#![cfg_attr(not(test), no_std)]
2#![feature(const_trait_impl)]
3#![feature(trait_alias)]
4#![feature(associated_const_equality)]
5#![feature(const_convert)]
6#![feature(const_index)]
7#![feature(generic_const_exprs)]
8#![feature(ptr_metadata)]
9
10//! A trait for any array, with item as an associated type, and length as an assiciated constant.
11//!
12//! This crate is a subset of the crate [`array_ops`](https://crates.io/crates/array_ops).
13//!
14//! # Examples
15//!
16//! ```rust
17//! use array_trait::*;
18//!
19//! type Arr3 = [i8; 3];
20//!
21//! const A: Arr3 = [1, 2, 3];
22//!
23//! // The assiciated constant `LENGTH` equals the length of the array
24//! assert_eq!(Arr3::LENGTH, 3);
25//! assert_eq!(Arr3::LENGTH, A.len());
26//! ```
27//!
28//! ```rust
29//! #![feature(const_trait_impl)]
30//! #![feature(generic_const_exprs)]
31//!
32//! use array_trait::*;
33//!
34//! type Arr3 = [i8; 3];
35//!
36//! const A: Arr3 = [1, 2, 3];
37//!
38//! /// The trait can be used in a function like this:
39//! const fn first<'a, T: ~const Array>(array: &'a T) -> Option<&'a <T as AsSlice>::Elem>
40//! where
41//!     [(); T::LENGTH]: // This is required for now.
42//! {
43//!     array.as_array().first()
44//! }
45//! assert_eq!(first(&A), Some(&1));
46//! ```
47//!
48//! # N-dimensional arrays
49//!
50//! There is also a trait for N-dimensional arrays, which contain information on its inner structure, and supports a depth up to 64 dimensions.
51//!
52//! The associated constants [DIMENSIONS](ArrayNd::DIMENSIONS) and [FLAT_LENGTH](ArrayNd::FLAT_LENGTH) vary depending on the chosen depth.
53//!
54//!  The assiciated type [ItemNd](ArrayNd::ItemNd) represents the innermost type given a chosen depth.
55//!
56//! # Examples
57//!
58//! ```rust
59//! #![feature(generic_const_exprs)]
60//!
61//! use array_trait::*;
62//!
63//! type Mat2x3 = [[i8; 3]; 2];
64//!
65//! /// The number of dimensions
66//! const DEPTH: usize = 2;
67//!
68//! // `FLAT_LENGTH` is the combined length if the N-dimensional array was flattened,
69//! // i.e. the product of the lengths of each dimension.
70//! assert_eq!(<Mat2x3 as ArrayNd<DEPTH>>::FLAT_LENGTH, 6);
71//!
72//! // `DIMENSIONS` contains the lengths of each dimension ordered outermost to innermost.
73//! assert_eq!(<Mat2x3 as ArrayNd<DEPTH>>::DIMENSIONS, [2, 3]);
74//! ```
75
76moddef::moddef!(
77    flat(pub) mod {
78        array,
79        array_nd,
80        into_array,
81        as_array
82    }
83);
84
85#[cfg(feature = "alloc")]
86extern crate alloc;
87
88pub use slice_trait::*;
89
90mod private
91{
92    use crate::AsArray;
93
94    pub trait Array: AsArray {}
95    impl<Item, const LENGTH: usize> Array for [Item; LENGTH] {}
96}
97
98#[cfg(test)]
99mod test
100{
101    #[test]
102    fn test() {}
103}