x11rb_protocol/
wrapper.rs1use super::x11_utils::TryParse;
4use core::marker::PhantomData;
5
6#[derive(Debug, Clone)]
16pub struct PropertyIterator<'a, T>(&'a [u8], PhantomData<T>);
17
18impl<'a, T> PropertyIterator<'a, T> {
19 pub(crate) fn new(value: &'a [u8]) -> Self {
20 PropertyIterator(value, PhantomData)
21 }
22}
23
24impl<T> Iterator for PropertyIterator<'_, T>
25where
26 T: TryParse,
27{
28 type Item = T;
29
30 fn next(&mut self) -> Option<Self::Item> {
31 match T::try_parse(self.0) {
32 Ok((value, remaining)) => {
33 self.0 = remaining;
34 Some(value)
35 }
36 Err(_) => {
37 self.0 = &[];
38 None
39 }
40 }
41 }
42
43 fn size_hint(&self) -> (usize, Option<usize>) {
44 use core::mem::size_of;
45 let size = self.0.len() / size_of::<T>();
46 (size, Some(size))
47 }
48}
49
50impl<T: TryParse> core::iter::FusedIterator for PropertyIterator<'_, T> {}
51
52#[cfg(test)]
53mod tests {
54 use super::PropertyIterator;
55 use alloc::vec::Vec;
56
57 #[test]
58 fn test_parse_u8() {
59 let input = [0u8, 1, 2, 3, 4, 5];
60 let output = PropertyIterator::new(&input).collect::<Vec<u8>>();
61 assert_eq!(&input[..], output);
62 }
63
64 #[test]
65 fn test_parse_u32() {
66 let expected = [0u32, 1, 2, 3, 4, 5];
67 let input = {
68 let mut input = Vec::new();
69 for value in &expected {
70 input.extend_from_slice(&value.to_ne_bytes());
71 }
72 input
73 };
74
75 let output = PropertyIterator::new(&input).collect::<Vec<u32>>();
76 assert_eq!(&expected[..], output);
77 }
78
79 #[test]
80 fn test_size_hint() {
81 let hint = PropertyIterator::<u32>::new(&[0; 0]).size_hint();
82 assert_eq!(hint, (0, Some(0)));
83
84 let hint = PropertyIterator::<u32>::new(&[0; 8]).size_hint();
85 assert_eq!(hint, (2, Some(2)));
86
87 let hint = PropertyIterator::<u32>::new(&[0; 30]).size_hint();
89 assert_eq!(hint, (7, Some(7)));
90 }
91}