cosmogony/
mutable_slice.rs

1use crate::{Zone, ZoneIndex};
2
3// This struct is necessary to wrap the `zones` slice
4// and keep a mutable reference to a zone (and set
5// its parent) while still be able to borrow another
6// reference to another zone.
7pub struct MutableSlice<'a> {
8    pub right: &'a [Zone],
9    pub left: &'a [Zone],
10    pub idx: usize,
11}
12
13impl<'a> MutableSlice<'a> {
14    pub fn init(zones: &'a mut [Zone], index: usize) -> (Self, &'a mut Zone) {
15        let (left, temp) = zones.split_at_mut(index);
16        let (z, right) = temp.split_at_mut(1);
17        let s = Self {
18            right,
19            left,
20            idx: index,
21        };
22        (s, &mut z[0])
23    }
24
25    pub fn get(&self, zindex: &ZoneIndex) -> &Zone {
26        let idx = zindex.index;
27        match idx {
28            i if i < self.idx => &self.left[i],
29            i if i == self.idx => panic!("Cannot retrieve middle index"),
30            i => &self.right[i - self.idx - 1],
31        }
32    }
33}