devela/data/list/array/d2/
impl_traits.rs

1// devela::data::list::array::d2::impl_traits
2//
3//! 2-dimensional array trait impls
4//
5
6#[cfg(feature = "alloc")]
7use crate::Boxed;
8use crate::{Array, Array2d, Bare, ConstDefault, Storage};
9use core::fmt;
10
11/* Clone, Copy */
12
13#[rustfmt::skip]
14impl<T: Clone, const C: usize, const R: usize, const CR: usize, const RMAJ: bool, S: Storage>
15Clone for Array2d<T, C, R, CR, RMAJ, S> where S::Stored<[T; CR]>: Clone {
16    fn clone(&self) -> Self {
17        Self { data: self.data.clone() }
18    }
19}
20#[rustfmt::skip]
21impl<T: Copy, const C: usize, const R: usize, const CR: usize, const RMAJ: bool, S: Storage>
22Copy for Array2d<T, C, R, CR, RMAJ, S> where S::Stored<[T; CR]>: Copy {}
23
24// Debug
25#[rustfmt::skip]
26impl<T: fmt::Debug, const C: usize, const R: usize, const CR: usize, S: Storage, const RMAJ: bool>
27fmt::Debug for Array2d<T, C, R, CR, RMAJ, S> where S::Stored<[T; CR]>: fmt::Debug {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        f.debug_struct("Array2d")
30            .field("T", &core::any::type_name::<T>())
31            .field("S", &S::name())
32            .field("C", &C).field("R", &R).field("CR", &CR).field("RMAJ", &RMAJ)
33            .field("data", &self.data)
34            .finish()
35    }
36}
37
38/* PartialEq, Eq */
39
40#[rustfmt::skip]
41impl<T: PartialEq, const C: usize, const R: usize, const CR: usize, const RMAJ: bool, S: Storage>
42PartialEq for Array2d<T, C, R, CR, RMAJ, S> where S::Stored<[T; CR]>: PartialEq {
43    fn eq(&self, other: &Self) -> bool {
44        self.data == other.data && self.capacity() == other.capacity()
45    }
46}
47#[rustfmt::skip]
48impl<T: Eq, const C: usize, const R: usize, const CR: usize, const RMAJ: bool, S: Storage>
49Eq for Array2d<T, C, R, CR, RMAJ, S> where S::Stored<[T; CR]>: Eq {}
50
51/* Default, ConstDefault */
52
53// T: Default, S: Bare
54impl<T: Default, const C: usize, const R: usize, const CR: usize, const RMAJ: bool> Default
55    for Array2d<T, C, R, CR, RMAJ, Bare>
56{
57    /// Returns an array, allocated in the stack,
58    /// using the default value to fill the data.
59    /// # Panics
60    /// Panics if `C * R > usize::MAX` or if `C * R != CR`.
61    fn default() -> Self {
62        Self::panic_check_CR();
63        Self { data: Array::<T, CR, Bare>::default() }
64    }
65}
66
67// T: ConstDefault, S: Bare
68impl<T: ConstDefault, const C: usize, const R: usize, const CR: usize, const RMAJ: bool>
69    ConstDefault for Array2d<T, C, R, CR, RMAJ, Bare>
70{
71    /// Returns an array, allocated in the stack,
72    /// using the default value to fill the data.
73    /// # Panics
74    /// Panics if `C * R > usize::MAX` or if `C * R != CR`.
75    const DEFAULT: Self = {
76        Self::panic_check_CR();
77        Self { data: Array::<T, CR, Bare>::DEFAULT }
78    };
79}
80
81// T: Default, S: Boxed
82#[cfg(feature = "alloc")]
83#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
84impl<T: Default, const C: usize, const R: usize, const CR: usize, const RMAJ: bool> Default
85    for Array2d<T, C, R, CR, RMAJ, Boxed>
86{
87    /// Returns an array, allocated in the heap,
88    /// using the default value to fill the data.
89    ///
90    /// # Examples
91    /// ```
92    /// # use devela::{Boxed, Array2d};
93    /// let g = Array2d::<String, 4, 4, {4 * 4}, true, Boxed>::default();
94    /// ```
95    fn default() -> Self {
96        Self::panic_check_CR();
97        Self { data: Array::<T, CR, Boxed>::default() }
98    }
99}