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