x11rb_protocol/
wrapper.rs

1//! Helpers for the generated code
2
3use super::x11_utils::TryParse;
4use core::marker::PhantomData;
5
6/// Iterator implementation used by [GetPropertyReply].
7///
8/// This is the actual type returned by [GetPropertyReply::value8], [GetPropertyReply::value16],
9/// and [GetPropertyReply::value32]. This type needs to be public due to Rust's visibility rules.
10///
11/// [GetPropertyReply]: crate::protocol::xproto::GetPropertyReply
12/// [GetPropertyReply::value8]: crate::protocol::xproto::GetPropertyReply::value8
13/// [GetPropertyReply::value16]: crate::protocol::xproto::GetPropertyReply::value16
14/// [GetPropertyReply::value32]: crate::protocol::xproto::GetPropertyReply::value32
15#[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        // In this case, the data is not an exact multiple of the element size
88        let hint = PropertyIterator::<u32>::new(&[0; 30]).size_hint();
89        assert_eq!(hint, (7, Some(7)));
90    }
91}