devela/data/collections/array/d1/
impl_traits.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// devela::data::collections:array::d1::impl_traits
//
//! 1-dimensional array trait impls
//
// TOC
// - utility traits
// - iterator related
// - bitwise ops

use crate::{
    array_init, Array, AsMut, AsRef, Bare, BareBox, BitAnd, BitAndAssign, BitOr, BitOrAssign,
    BitXor, BitXorAssign, Borrow, BorrowMut, ConstDefault, Debug, Deref, DerefMut, FmtResult,
    Formatter, Hash, Hasher, Not, Ordering, Storage,
};

#[cfg(feature = "alloc")]
use crate::{Box, Boxed};

/* utility traits */

// Deref
impl<T, const CAP: usize, S: Storage> Deref for Array<T, CAP, S> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.data.deref()
    }
}
// DerefMut
impl<T, const CAP: usize, S: Storage> DerefMut for Array<T, CAP, S> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.data.deref_mut()
    }
}
// AsRef
impl<T, const CAP: usize, S: Storage> AsRef<[T; CAP]> for Array<T, CAP, S> {
    fn as_ref(&self) -> &[T; CAP] {
        &self.data
    }
}
// AsMut
impl<T, const CAP: usize, S: Storage> AsMut<[T; CAP]> for Array<T, CAP, S> {
    fn as_mut(&mut self) -> &mut [T; CAP] {
        &mut self.data
    }
}
// Borrow
impl<T, const CAP: usize, S: Storage> Borrow<[T; CAP]> for Array<T, CAP, S> {
    fn borrow(&self) -> &[T; CAP] {
        &self.data
    }
}
// BorrowMut
impl<T, const CAP: usize, S: Storage> BorrowMut<[T; CAP]> for Array<T, CAP, S> {
    fn borrow_mut(&mut self) -> &mut [T; CAP] {
        &mut self.data
    }
}

// T: Clone
impl<T: Clone, const CAP: usize, S: Storage> Clone for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Clone,
{
    fn clone(&self) -> Self {
        Self { data: self.data.clone() }
    }
}

// T: Copy
impl<T: Copy, const CAP: usize, S: Storage> Copy for Array<T, CAP, S> where S::Stored<[T; CAP]>: Copy
{}

// T: Debug
impl<T: Debug, const CAP: usize, S: Storage> Debug for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
        let mut debug = f.debug_struct("Array");
        debug.field("T", &core::any::type_name::<T>()).field("S", &S::name()).field("CAP", &CAP);

        const MAX: usize = 16;
        if CAP <= MAX {
            debug.field("data", &self.data);
        } else {
            let first = &self.data[..MAX / 2];
            let last = &self.data[CAP - MAX / 2..];
            debug.field("array", &(&first, "...", &last));
        }
        debug.finish()
    }
}
// T: PartialEq
impl<T: PartialEq, const CAP: usize, S: Storage> PartialEq for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data && self.capacity() == other.capacity()
    }
}
// T: Eq
impl<T: Eq, const CAP: usize, S: Storage> Eq for Array<T, CAP, S> where S::Stored<[T; CAP]>: Eq {}
// T: PartialOrd
impl<T: PartialOrd, const CAP: usize, S: Storage> PartialOrd for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.data.partial_cmp(&other.data)
    }
}
// T: Ord
impl<T: Ord, const CAP: usize, S: Storage> Ord for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.data.cmp(&other.data)
    }
}
// T: Hash
impl<T: Hash, const CAP: usize, S: Storage> Hash for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.data.hash(state);
    }
}

// T: Default, S: Bare
impl<T: Default, const CAP: usize> Default for Array<T, CAP, Bare> {
    /// Returns an array, allocated in the stack,
    /// using the default value to fill the data.
    fn default() -> Self {
        let data = BareBox::new(array_init!(default [T; CAP], "safe_data", "unsafe_array"));
        Array { data }
    }
}
// T: ConstDefault, S: Bare
impl<T: ConstDefault, const CAP: usize> ConstDefault for Array<T, CAP, Bare> {
    /// Returns an array, allocated in the stack,
    /// using the default value to fill the data.
    const DEFAULT: Self = { Array { data: BareBox::new([T::DEFAULT; CAP]) } };
}

// T: Default, S: Boxed
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
impl<T: Default, const CAP: usize> Default for Array<T, CAP, Boxed> {
    /// Returns an array, allocated in the heap,
    /// using the default value to fill the data.
    ///
    /// # Examples
    /// ```
    /// # use devela::{Array, Boxed};
    /// let mut s = Array::<i32, 100, Boxed>::default();
    /// ```
    fn default() -> Self {
        let data = array_init!(default_heap [T; CAP], "safe_data", "unsafe_array");
        Array { data }
    }
}

impl<T, const CAP: usize> From<Array<T, CAP, Bare>> for [T; CAP] {
    fn from(array: Array<T, CAP, Bare>) -> [T; CAP] {
        array.data.0
    }
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
impl<T, const CAP: usize> From<Array<T, CAP, Boxed>> for Box<[T; CAP]> {
    fn from(array: Array<T, CAP, Boxed>) -> Box<[T; CAP]> {
        array.data
    }
}

/* iterator related */

impl<T: Default, I, const CAP: usize> From<I> for Array<T, CAP, Bare>
where
    I: IntoIterator<Item = T>,
{
    /// Returns a array filled with an iterator, in the stack.
    ///
    /// If the `iterator` length is less than the array capacity `CAP`,
    /// the missing elements will be the default value of `T`.
    ///
    /// # Examples
    /// ```
    /// # use devela::data::Array;
    /// let s: Array<_, 4> = [1, 2, 3].into();
    /// assert_eq![s.as_slice(), &[1, 2, 3, 0]];
    /// ```
    fn from(iterator: I) -> Array<T, CAP, Bare> {
        let data = BareBox::new(array_init!(iter [T; CAP], "safe_data", "unsafe_array", iterator));
        Array { data }
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
impl<T: Default, I, const CAP: usize> From<I> for Array<T, CAP, Boxed>
where
    I: IntoIterator<Item = T>,
{
    /// Returns a array filled with an iterator, in the heap.
    ///
    /// # Examples
    /// ```
    /// # use devela::{Array, Boxed};
    /// let s: Array<_, 4, Boxed> = [1, 2, 3].into();
    ///
    /// assert_eq![s.as_slice(), &[1, 2, 3, 0]];
    /// ```
    fn from(iterator: I) -> Array<T, CAP, Boxed> {
        let data = array_init!(iter_heap [T; CAP], "safe_data", "unsafe_array", iterator);
        Array { data }
    }
}

/* impl bitwise ops */

impl<T, const CAP: usize, S: Storage> BitAnd for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitAnd<Output = T> + Copy,
{
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        let mut result = self;
        for i in 0..CAP {
            result.data[i] = result.data[i] & rhs.data[i];
        }
        result
    }
}
impl<T, const CAP: usize, S: Storage> BitOr for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitOr<Output = T> + Copy,
{
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        let mut result = self;
        for i in 0..CAP {
            result.data[i] = result.data[i] | rhs.data[i];
        }
        result
    }
}
impl<T, const CAP: usize, S: Storage> BitXor for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitXor<Output = T> + Copy,
{
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        let mut result = self;
        for i in 0..CAP {
            result.data[i] = result.data[i] ^ rhs.data[i];
        }
        result
    }
}

impl<T, const CAP: usize, S: Storage> BitAndAssign for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitAndAssign + Copy,
{
    fn bitand_assign(&mut self, rhs: Self) {
        for i in 0..CAP {
            self.data[i] &= rhs.data[i];
        }
    }
}

impl<T, const CAP: usize, S: Storage> BitOrAssign for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitOrAssign + Copy,
{
    fn bitor_assign(&mut self, rhs: Self) {
        for i in 0..CAP {
            self.data[i] |= rhs.data[i];
        }
    }
}

impl<T, const CAP: usize, S: Storage> BitXorAssign for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Copy,
    T: BitXorAssign + Copy,
{
    fn bitxor_assign(&mut self, rhs: Self) {
        for i in 0..CAP {
            self.data[i] ^= rhs.data[i];
        }
    }
}

impl<T, const CAP: usize, S: Storage> Not for Array<T, CAP, S>
where
    S::Stored<[T; CAP]>: Clone,
    T: Not<Output = T> + Copy,
{
    type Output = Self;

    fn not(self) -> Self::Output {
        let mut result = self;
        for i in 0..CAP {
            result.data[i] = !result.data[i];
        }
        result
    }
}