devela/data/list/array/
adt.rs

1// devela::data::list::array::adt
2//
3//! [`DataArray`] abstract data type
4//
5// TOC
6// - define trait DataArray
7// - impl for:
8//   - Array
9
10use crate::{DataCollection, IndexOutOfBounds, Storage};
11
12#[doc = crate::TAG_DATA_STRUCTURE!()]
13/// An abstract *array* data type.
14///
15/// - <https://en.wikipedia.org/wiki/Array_(data_type)#Abstract_arrays>
16pub trait DataArray: DataCollection {
17    /// Returns an immutable reference to the element at the specified `index`.
18    /// # Errors
19    /// Returns [`IndexOutOfBounds`] if the given `index` is out of bounds.
20    fn array_ref_get(
21        &self,
22        index: usize,
23    ) -> Result<&<Self as DataCollection>::Element, IndexOutOfBounds>;
24
25    /// Returns a mutable reference to the element at the given `index`.
26    /// # Errors
27    /// Returns [`IndexOutOfBounds`] if the given `index` is out of bounds.
28    fn array_mut_get(
29        &mut self,
30        index: usize,
31    ) -> Result<&mut <Self as DataCollection>::Element, IndexOutOfBounds>;
32
33    /// Sets the element at the specified `index` to the given `value`.
34    /// # Errors
35    /// Returns [`IndexOutOfBounds`] if the given `index` is out of bounds.
36    fn array_set(
37        &mut self,
38        index: usize,
39        value: <Self as DataCollection>::Element,
40    ) -> Result<(), IndexOutOfBounds>;
41
42    /// Sets the element at the specified `index` to the given clonable `value`.
43    /// # Errors
44    /// Returns [`IndexOutOfBounds`] if the given `index` is out of bounds.
45    fn array_set_ref(
46        &mut self,
47        index: usize,
48        value: &<Self as DataCollection>::Element,
49    ) -> Result<(), IndexOutOfBounds>
50    where
51        Self::Element: Clone;
52}
53
54/* impl for Array */
55
56impl<T, const LEN: usize, S: Storage> DataArray for crate::data::Array<T, LEN, S> {
57    fn array_ref_get(
58        &self,
59        idx: usize,
60    ) -> Result<&<Self as DataCollection>::Element, IndexOutOfBounds> {
61        if let Some(e) = self.get(idx) {
62            Ok(e)
63        } else {
64            Err(IndexOutOfBounds(Some(idx)))
65        }
66    }
67    fn array_mut_get(
68        &mut self,
69        idx: usize,
70    ) -> Result<&mut <Self as DataCollection>::Element, IndexOutOfBounds> {
71        if let Some(e) = self.get_mut(idx) {
72            Ok(e)
73        } else {
74            Err(IndexOutOfBounds(Some(idx)))
75        }
76    }
77    fn array_set(
78        &mut self,
79        idx: usize,
80        value: <Self as DataCollection>::Element,
81    ) -> Result<(), IndexOutOfBounds> {
82        if let Some(e) = self.get_mut(idx) {
83            *e = value;
84            Ok(())
85        } else {
86            Err(IndexOutOfBounds(Some(idx)))
87        }
88    }
89    fn array_set_ref(
90        &mut self,
91        idx: usize,
92        value: &<Self as DataCollection>::Element,
93    ) -> Result<(), IndexOutOfBounds>
94    where
95        T: Clone,
96    {
97        if let Some(e) = self.get_mut(idx) {
98            *e = value.clone();
99            Ok(())
100        } else {
101            Err(IndexOutOfBounds(Some(idx)))
102        }
103    }
104}
105
106/* impl for core array [T; LEN] */
107
108impl<T, const LEN: usize> DataArray for [T; LEN] {
109    fn array_ref_get(
110        &self,
111        idx: usize,
112    ) -> Result<&<Self as DataCollection>::Element, IndexOutOfBounds> {
113        if let Some(e) = self.get(idx) {
114            Ok(e)
115        } else {
116            Err(IndexOutOfBounds(Some(idx)))
117        }
118    }
119    fn array_mut_get(
120        &mut self,
121        idx: usize,
122    ) -> Result<&mut <Self as DataCollection>::Element, IndexOutOfBounds> {
123        if let Some(e) = self.get_mut(idx) {
124            Ok(e)
125        } else {
126            Err(IndexOutOfBounds(Some(idx)))
127        }
128    }
129    fn array_set(
130        &mut self,
131        idx: usize,
132        value: <Self as DataCollection>::Element,
133    ) -> Result<(), IndexOutOfBounds> {
134        if let Some(e) = self.get_mut(idx) {
135            *e = value;
136            Ok(())
137        } else {
138            Err(IndexOutOfBounds(Some(idx)))
139        }
140    }
141    fn array_set_ref(
142        &mut self,
143        idx: usize,
144        value: &<Self as DataCollection>::Element,
145    ) -> Result<(), IndexOutOfBounds>
146    where
147        T: Clone,
148    {
149        if let Some(e) = self.get_mut(idx) {
150            *e = value.clone();
151            Ok(())
152        } else {
153            Err(IndexOutOfBounds(Some(idx)))
154        }
155    }
156}
157
158/* impl for Vec */
159
160#[cfg(feature = "alloc")]
161impl<T> DataArray for crate::Vec<T> {
162    fn array_ref_get(
163        &self,
164        idx: usize,
165    ) -> Result<&<Self as DataCollection>::Element, IndexOutOfBounds> {
166        if let Some(e) = self.get(idx) {
167            Ok(e)
168        } else {
169            Err(IndexOutOfBounds(Some(idx)))
170        }
171    }
172    fn array_mut_get(
173        &mut self,
174        idx: usize,
175    ) -> Result<&mut <Self as DataCollection>::Element, IndexOutOfBounds> {
176        if let Some(e) = self.get_mut(idx) {
177            Ok(e)
178        } else {
179            Err(IndexOutOfBounds(Some(idx)))
180        }
181    }
182    fn array_set(
183        &mut self,
184        idx: usize,
185        value: <Self as DataCollection>::Element,
186    ) -> Result<(), IndexOutOfBounds> {
187        if let Some(e) = self.get_mut(idx) {
188            *e = value;
189            Ok(())
190        } else {
191            Err(IndexOutOfBounds(Some(idx)))
192        }
193    }
194    fn array_set_ref(
195        &mut self,
196        idx: usize,
197        value: &<Self as DataCollection>::Element,
198    ) -> Result<(), IndexOutOfBounds>
199    where
200        T: Clone,
201    {
202        if let Some(e) = self.get_mut(idx) {
203            *e = value.clone();
204            Ok(())
205        } else {
206            Err(IndexOutOfBounds(Some(idx)))
207        }
208    }
209}