1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use super::x11_utils::TryParse;
use core::marker::PhantomData;
#[derive(Debug, Clone)]
pub struct PropertyIterator<'a, T>(&'a [u8], PhantomData<T>);
impl<'a, T> PropertyIterator<'a, T> {
pub(crate) fn new(value: &'a [u8]) -> Self {
PropertyIterator(value, PhantomData)
}
}
impl<T> Iterator for PropertyIterator<'_, T>
where
T: TryParse,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match T::try_parse(self.0) {
Ok((value, remaining)) => {
self.0 = remaining;
Some(value)
}
Err(_) => {
self.0 = &[];
None
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.0.len() / core::mem::size_of::<T>();
(size, Some(size))
}
}
impl<T: TryParse> core::iter::FusedIterator for PropertyIterator<'_, T> {}