hv1_structs/
vtl_array.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Container data structures indexable by [`Vtl`].
5
6use bitvec::array::BitArray;
7use core::ops::Deref;
8use core::ops::DerefMut;
9use core::ops::Index;
10use core::ops::IndexMut;
11use hvdef::Vtl;
12use inspect::Inspect;
13use inspect::InspectMut;
14
15// TODO: Enforce N <= 3 on the type when stable
16/// An array indexable by [`Vtl`].
17#[derive(Debug, Clone)]
18pub struct VtlArray<T, const N: usize> {
19    data: [T; N],
20}
21
22impl<T, const N: usize> VtlArray<T, N> {
23    /// Creates an array of type T, where each element is `value`.
24    pub const fn new(value: T) -> Self
25    where
26        T: Copy,
27    {
28        assert!(N > 0 && N <= 3);
29        Self { data: [value; N] }
30    }
31
32    /// Creates an array of type T, where each element is
33    /// the returned value from `f` using that element’s index.
34    pub fn from_fn<F>(mut f: F) -> Self
35    where
36        F: FnMut(Vtl) -> T,
37    {
38        assert!(N > 0 && N <= 3);
39        Self {
40            data: core::array::from_fn(|i| f(Vtl::try_from(i as u8).unwrap())),
41        }
42    }
43
44    /// Maps over the vtl array using the raw underlying array.
45    pub fn map<U, F>(self, f: F) -> VtlArray<U, N>
46    where
47        F: FnMut(T) -> U,
48    {
49        VtlArray {
50            data: self.data.map(f),
51        }
52    }
53
54    /// Borrows each element and returns an array of references with the same
55    /// size as self.
56    pub fn each_ref(&self) -> VtlArray<&T, N> {
57        VtlArray {
58            data: self.data.each_ref(),
59        }
60    }
61
62    /// Borrows each element mutably and returns an array of mutable references
63    /// with the same size as self.
64    pub fn each_mut(&mut self) -> VtlArray<&mut T, N> {
65        VtlArray {
66            data: self.data.each_mut(),
67        }
68    }
69
70    /// Returns the raw underlying array.
71    pub fn into_inner(self) -> [T; N] {
72        self.data
73    }
74}
75
76impl<T> From<[T; 1]> for VtlArray<T, 1> {
77    fn from(a: [T; 1]) -> Self {
78        Self { data: a }
79    }
80}
81
82impl<T> From<[T; 2]> for VtlArray<T, 2> {
83    fn from(a: [T; 2]) -> Self {
84        Self { data: a }
85    }
86}
87
88impl<T> From<[T; 3]> for VtlArray<T, 3> {
89    fn from(a: [T; 3]) -> Self {
90        Self { data: a }
91    }
92}
93
94// TODO: Remove this when deriving Default for arrays is stable
95impl<T, const N: usize> Default for VtlArray<T, N>
96where
97    T: Default,
98{
99    fn default() -> Self {
100        Self::from_fn(|_| T::default())
101    }
102}
103
104impl<T, const N: usize> Inspect for VtlArray<T, N>
105where
106    T: Inspect,
107{
108    fn inspect(&self, req: inspect::Request<'_>) {
109        inspect::iter_by_index(&self.data).inspect(req)
110    }
111}
112
113impl<T, const N: usize> InspectMut for VtlArray<T, N>
114where
115    T: InspectMut,
116{
117    fn inspect_mut(&mut self, req: inspect::Request<'_>) {
118        let mut resp = req.respond();
119        for (i, data) in self.data.iter_mut().enumerate() {
120            resp.field_mut(&i.to_string(), data);
121        }
122    }
123}
124
125impl<T, V: Into<Vtl>, const N: usize> Index<V> for VtlArray<T, N> {
126    type Output = T;
127
128    fn index(&self, index: V) -> &Self::Output {
129        &self.data[index.into() as usize]
130    }
131}
132
133impl<T, V: Into<Vtl>, const N: usize> IndexMut<V> for VtlArray<T, N> {
134    fn index_mut(&mut self, index: V) -> &mut Self::Output {
135        &mut self.data[index.into() as usize]
136    }
137}
138
139impl<T, const N: usize> Deref for VtlArray<T, N> {
140    type Target = [T; N];
141
142    fn deref(&self) -> &Self::Target {
143        &self.data
144    }
145}
146
147impl<T, const N: usize> DerefMut for VtlArray<T, N> {
148    fn deref_mut(&mut self) -> &mut Self::Target {
149        &mut self.data
150    }
151}
152
153/// A set of [`Vtl`]s.
154#[derive(Copy, Clone)]
155pub struct VtlSet {
156    bits: BitArray<u16>,
157}
158
159impl VtlSet {
160    /// Creates a new empty set.
161    pub fn new() -> Self {
162        Self {
163            bits: BitArray::new(0),
164        }
165    }
166
167    /// Adds a [`Vtl`] to the set.
168    pub fn set(&mut self, vtl: Vtl) {
169        self.bits.set(vtl as usize, true);
170    }
171
172    /// Removes a [`Vtl`] from the set.
173    pub fn clear(&mut self, vtl: Vtl) {
174        self.bits.set(vtl as usize, false);
175    }
176
177    /// Returns true if any [`Vtl`] in the set is higher than `vtl`.
178    pub fn is_higher_vtl_set_than(&self, vtl: Vtl) -> bool {
179        self.highest_set() > Some(vtl)
180    }
181
182    /// Returns the highest set [`Vtl`] in the set, if any.
183    pub fn highest_set(&self) -> Option<Vtl> {
184        Some(Vtl::try_from(self.bits.last_one()? as u8).unwrap())
185    }
186
187    /// Returns true if the given [`Vtl`] is set.
188    pub fn is_set<V: Into<Vtl>>(&self, vtl: V) -> bool {
189        self.bits[vtl.into() as usize]
190    }
191
192    /// Returns true if the given [`Vtl`] is not set.
193    pub fn is_clear<V: Into<Vtl>>(&self, vtl: V) -> bool {
194        !self.is_set(vtl)
195    }
196
197    /// Returns an iterator over the set [`Vtl`]s, in order from highest to lowest.
198    pub fn iter_highest_first(&self) -> impl Iterator<Item = Vtl> + '_ {
199        self.bits
200            .iter_ones()
201            .rev()
202            .map(|i| Vtl::try_from(i as u8).unwrap())
203    }
204}
205
206impl Inspect for VtlSet {
207    fn inspect(&self, req: inspect::Request<'_>) {
208        inspect::iter_by_index(self.bits.iter().map(|v| *v)).inspect(req)
209    }
210}
211
212impl From<u16> for VtlSet {
213    fn from(bits: u16) -> Self {
214        VtlSet {
215            bits: BitArray::new(bits),
216        }
217    }
218}
219
220#[cfg(test)]
221mod tests {
222    use super::VtlSet;
223    use hvdef::Vtl;
224
225    #[test]
226    fn test_vtlset() {
227        let mut set = VtlSet::new();
228        assert_eq!(set.highest_set(), None);
229        set.set(Vtl::Vtl0);
230        assert_eq!(set.highest_set(), Some(Vtl::Vtl0));
231        set.set(Vtl::Vtl2);
232        assert_eq!(set.highest_set(), Some(Vtl::Vtl2));
233
234        {
235            let mut iter = set.iter_highest_first();
236            assert_eq!(iter.next(), Some(Vtl::Vtl2));
237            assert_eq!(iter.next(), Some(Vtl::Vtl0));
238            assert_eq!(iter.next(), None);
239        }
240
241        assert!(!set.is_higher_vtl_set_than(Vtl::Vtl2));
242        assert!(set.is_higher_vtl_set_than(Vtl::Vtl1));
243        assert!(set.is_higher_vtl_set_than(Vtl::Vtl0));
244
245        set.clear(Vtl::Vtl2);
246        assert!(!set.is_higher_vtl_set_than(Vtl::Vtl0));
247    }
248}