copa/
params.rs

1//! Fixed size parameters list with optional subparameters.
2
3use core::fmt::{self, Debug, Formatter};
4
5pub(crate) const MAX_PARAMS: usize = 32;
6
7#[derive(Default)]
8pub struct Params {
9    /// Number of subparameters for each parameter.
10    ///
11    /// For each entry in the `params` slice, this stores the length of the param as number of
12    /// subparams at the same index as the param in the `params` slice.
13    ///
14    /// At the subparam positions the length will always be `0`.
15    subparams: [u8; MAX_PARAMS],
16
17    /// All parameters and subparameters.
18    params: [u16; MAX_PARAMS],
19
20    /// Number of suparameters in the current parameter.
21    current_subparams: u8,
22
23    /// Total number of parameters and subparameters.
24    len: usize,
25}
26
27impl Params {
28    /// Returns the number of parameters.
29    #[inline]
30    pub fn len(&self) -> usize {
31        self.len
32    }
33
34    /// Returns `true` if there are no parameters present.
35    #[inline]
36    pub fn is_empty(&self) -> bool {
37        self.len == 0
38    }
39
40    /// Returns an iterator over all parameters and subparameters.
41    #[inline]
42    pub fn iter(&self) -> ParamsIter<'_> {
43        ParamsIter::new(self)
44    }
45
46    /// Returns `true` if there is no more space for additional parameters.
47    #[inline]
48    pub(crate) fn is_full(&self) -> bool {
49        self.len == MAX_PARAMS
50    }
51
52    /// Clear all parameters.
53    #[inline]
54    pub(crate) fn clear(&mut self) {
55        self.current_subparams = 0;
56        self.len = 0;
57    }
58
59    /// Add an additional parameter.
60    #[inline]
61    pub(crate) fn push(&mut self, item: u16) {
62        self.subparams[self.len - self.current_subparams as usize] =
63            self.current_subparams + 1;
64        self.params[self.len] = item;
65        self.current_subparams = 0;
66        self.len += 1;
67    }
68
69    /// Add an additional subparameter to the current parameter.
70    #[inline]
71    pub(crate) fn extend(&mut self, item: u16) {
72        self.subparams[self.len - self.current_subparams as usize] =
73            self.current_subparams + 1;
74        self.params[self.len] = item;
75        self.current_subparams += 1;
76        self.len += 1;
77    }
78}
79
80impl<'a> IntoIterator for &'a Params {
81    type IntoIter = ParamsIter<'a>;
82    type Item = &'a [u16];
83
84    fn into_iter(self) -> Self::IntoIter {
85        self.iter()
86    }
87}
88
89/// Immutable subparameter iterator.
90pub struct ParamsIter<'a> {
91    params: &'a Params,
92    index: usize,
93}
94
95impl<'a> ParamsIter<'a> {
96    fn new(params: &'a Params) -> Self {
97        Self { params, index: 0 }
98    }
99}
100
101impl<'a> Iterator for ParamsIter<'a> {
102    type Item = &'a [u16];
103
104    fn next(&mut self) -> Option<Self::Item> {
105        if self.index >= self.params.len() {
106            return None;
107        }
108
109        // Get all subparameters for the current parameter.
110        let num_subparams = self.params.subparams[self.index];
111        let param = &self.params.params[self.index..self.index + num_subparams as usize];
112
113        // Jump to the next parameter.
114        self.index += num_subparams as usize;
115
116        Some(param)
117    }
118
119    fn size_hint(&self) -> (usize, Option<usize>) {
120        let remaining = self.params.len() - self.index;
121        (remaining, Some(remaining))
122    }
123}
124
125impl Debug for Params {
126    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
127        write!(f, "[")?;
128
129        for (i, param) in self.iter().enumerate() {
130            if i != 0 {
131                write!(f, ";")?;
132            }
133
134            for (i, subparam) in param.iter().enumerate() {
135                if i != 0 {
136                    write!(f, ":")?;
137                }
138
139                subparam.fmt(f)?;
140            }
141        }
142
143        write!(f, "]")
144    }
145}