ordes/
lib.rs

1#![cfg_attr(feature = "const_generics", allow(incomplete_features))]
2#![cfg_attr(feature = "const_generics", feature(generic_const_exprs))]
3#![cfg_attr(
4    feature = "const_generics",
5    doc(test(attr(allow(incomplete_features))))
6)]
7#![cfg_attr(
8    feature = "const_generics",
9    doc(test(attr(feature(generic_const_exprs))))
10)]
11
12#[allow(dead_code)]
13#[cfg(test)]
14mod tests;
15
16#[cfg(not(feature = "const_generics"))]
17mod stable_arr;
18
19#[cfg(feature = "const_generics")]
20mod nightly_arr;
21
22#[cfg(feature = "const_generics")]
23pub use nightly_arr::*;
24
25/// Trait for defining the removal of the last element from an array- or tuple-like data structure.
26pub trait OrdesPop {
27    /// Type of the container after `.pop()` has been called.
28    type Newlen;
29    /// Type of the removed value.
30    type Output;
31
32    /// Remove the last element from `self` and return it.
33    /// # Examples
34    /// Using an array:
35    /// ```
36    /// # use ordes::OrdesPop;
37    /// let foo: [u8; 5] = [0, 1, 2, 3, 4];
38    /// let (foo, pop) = foo.pop();
39    /// assert_eq!(foo, [0, 1, 2, 3]);
40    /// assert_eq!(pop, 4);
41    /// ```
42    /// Using a tuple:
43    /// ```
44    /// # use ordes::OrdesPop;
45    /// let foo = ('a', false, b'c', "d");
46    /// let (foo, pop) = foo.pop();
47    /// assert_eq!(foo, ('a', false, b'c'));
48    /// assert_eq!(pop, "d");
49    /// ```
50    fn pop(self) -> (Self::Newlen, Self::Output);
51}
52
53// Consider adding remove?
54
55/// Trait for defining the removal of the first element from an array- or tuple-like data structure.
56pub trait OrdesRest {
57    /// Type of the container after `.rest()` has been called.
58    type Newlen;
59    /// Type of the removed value.
60    type Output;
61
62    /// Removes the first element from `self` and returns it.
63    /// # Examples
64    /// Using an array:
65    /// ```
66    /// # use ordes::OrdesRest;
67    /// let foo: [u8; 5] = [0, 1, 2, 3, 4];
68    /// let (foo, pop) = foo.rest();
69    /// assert_eq!(foo, [1, 2, 3, 4]);
70    /// assert_eq!(pop, 0);
71    /// ```
72    /// Using a tuple:
73    /// ```
74    /// # use ordes::OrdesRest;
75    /// let foo = ('a', false, b'c', "d");
76    /// let (foo, pop) = foo.rest();
77    /// assert_eq!(foo, (false, b'c', "d"));
78    /// assert_eq!(pop, 'a');
79    /// ```
80    fn rest(self) -> (Self::Newlen, Self::Output);
81}
82
83/// Trait for defining pushing to an array- or tuple-like data structure.
84pub trait OrdesPush<Input> {
85    type Output;
86
87    /// Add an element to the end of `self` and return the sum.
88    /// # Examples
89    /// Using an array:
90    /// ```
91    /// # use ordes::OrdesPush;
92    /// let foo: [u8; 4] = [0, 1, 2, 3];
93    /// let foo = foo.push(4);
94    /// assert_eq!(foo, [0, 1, 2, 3, 4]);
95    /// ```
96    /// Using a tuple:
97    /// ```
98    /// # use ordes::OrdesPush;
99    /// let foo = ('a', false, b'c');
100    /// let foo = foo.push("d");
101    /// assert_eq!(foo, ('a', false, b'c', "d"));
102    /// ```
103    fn push(self, value: Input) -> Self::Output;
104
105    // Consider adding insert?
106}
107
108pub trait OrdesCons<Input> {
109    type Output;
110
111    /// Add an element to the start of `self` and return the sum.
112    /// # Examples
113    /// Using an array:
114    /// ```
115    /// # use ordes::OrdesCons;
116    /// let foo: [u8; 4] = [1, 2, 3, 4];
117    /// let foo = foo.cons(0);
118    /// assert_eq!(foo, [0, 1, 2, 3, 4]);
119    /// ```
120    /// Using a tuple:
121    /// ```
122    /// # use ordes::OrdesCons;
123    /// let foo = (false, b'c', "d");
124    /// let foo = foo.cons('a');
125    /// assert_eq!(foo, ('a', false, b'c', "d"));
126    /// ```
127    fn cons(self, value: Input) -> Self::Output;
128}
129
130use ordes_macros::impl_ops_tuple;
131use seq_macro::seq;
132
133seq! {N in 1..=32 {
134    impl_ops_tuple!(N);
135}}
136
137#[cfg(feature = "len_32_64")]
138seq! {N in 33..=64 {
139    impl_ops_tuple!(N);
140}}
141
142#[cfg(feature = "len_64_128")]
143seq! {N in 65..=128 {
144    impl_ops_tuple!(N);
145}}
146
147#[cfg(feature = "len_128_256")]
148seq! {N in 129..=256 {
149    impl_ops_tuple!(N);
150}}
151
152#[cfg(feature = "len_256_512")]
153seq! {N in 257..=512 {
154    impl_ops_tuple!(N);
155}}
156
157#[cfg(feature = "len_512_1024")]
158seq! {N in 513..=1024 {
159    impl_ops_tuple!(N);
160}}