wlan_common/ie/wsc/
reader.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use super::{AttributeHeader, Id};
6use crate::buffer_reader::BufferReader;
7use std::mem::size_of;
8use zerocopy::SplitByteSlice;
9
10pub struct Reader<B>(BufferReader<B>);
11
12impl<B: SplitByteSlice> Reader<B> {
13    pub fn new(bytes: B) -> Self {
14        Reader(BufferReader::new(bytes))
15    }
16}
17
18impl<B: SplitByteSlice> Iterator for Reader<B> {
19    type Item = (Id, B);
20
21    fn next(&mut self) -> Option<Self::Item> {
22        let header = self.0.peek::<AttributeHeader>()?;
23        let body_len = header.body_len.to_native() as usize;
24        if self.0.bytes_remaining() < size_of::<AttributeHeader>() + body_len {
25            None
26        } else {
27            // Unwraps are OK because we checked the length above
28            let header = self.0.read::<AttributeHeader>().unwrap();
29            let body = self.0.read_bytes(body_len).unwrap();
30            Some((header.id, body))
31        }
32    }
33}