mirror_mirror/
array.rs

1use core::fmt;
2use core::iter::FusedIterator;
3
4use crate::iter::ValueIterMut;
5use crate::Reflect;
6
7/// A reflected array type.
8pub trait Array: Reflect {
9    fn get(&self, index: usize) -> Option<&dyn Reflect>;
10
11    fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>;
12
13    fn len(&self) -> usize;
14
15    fn is_empty(&self) -> bool;
16
17    fn iter(&self) -> Iter<'_>;
18
19    fn iter_mut(&mut self) -> ValueIterMut<'_>;
20}
21
22impl fmt::Debug for dyn Array {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        self.as_reflect().debug(f)
25    }
26}
27
28#[derive(Debug)]
29pub struct Iter<'a> {
30    index: usize,
31    array: &'a dyn Array,
32}
33
34impl<'a> Iter<'a> {
35    pub fn new(array: &'a dyn Array) -> Self {
36        Self { index: 0, array }
37    }
38}
39
40impl<'a> Iterator for Iter<'a> {
41    type Item = &'a dyn Reflect;
42
43    fn next(&mut self) -> Option<Self::Item> {
44        let value = self.array.get(self.index)?;
45        self.index += 1;
46        Some(value)
47    }
48}
49
50impl<'a> ExactSizeIterator for Iter<'a> {
51    fn len(&self) -> usize {
52        self.array.len()
53    }
54}
55
56impl<'a> FusedIterator for Iter<'a> {}