1 /* Copyright 2018 Mozilla Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 use super::{
17     BinaryReader, LinkingType, Result, SectionIteratorLimited, SectionReader,
18     SectionWithLimitedItems,
19 };
20 
21 pub struct LinkingSectionReader<'a> {
22     reader: BinaryReader<'a>,
23     count: u32,
24 }
25 
26 impl<'a> LinkingSectionReader<'a> {
new(data: &'a [u8], offset: usize) -> Result<LinkingSectionReader<'a>>27     pub fn new(data: &'a [u8], offset: usize) -> Result<LinkingSectionReader<'a>> {
28         let mut reader = BinaryReader::new_with_offset(data, offset);
29         let count = reader.read_var_u32()?;
30         Ok(LinkingSectionReader { reader, count })
31     }
32 
get_count(&self) -> u3233     pub fn get_count(&self) -> u32 {
34         self.count
35     }
36 
original_position(&self) -> usize37     pub fn original_position(&self) -> usize {
38         self.reader.original_position()
39     }
40 
read<'b>(&mut self) -> Result<LinkingType> where 'a: 'b,41     pub fn read<'b>(&mut self) -> Result<LinkingType>
42     where
43         'a: 'b,
44     {
45         Ok(self.reader.read_linking_type()?)
46     }
47 }
48 
49 impl<'a> SectionReader for LinkingSectionReader<'a> {
50     type Item = LinkingType;
read(&mut self) -> Result<Self::Item>51     fn read(&mut self) -> Result<Self::Item> {
52         LinkingSectionReader::read(self)
53     }
eof(&self) -> bool54     fn eof(&self) -> bool {
55         self.reader.eof()
56     }
original_position(&self) -> usize57     fn original_position(&self) -> usize {
58         LinkingSectionReader::original_position(self)
59     }
60 }
61 
62 impl<'a> SectionWithLimitedItems for LinkingSectionReader<'a> {
get_count(&self) -> u3263     fn get_count(&self) -> u32 {
64         LinkingSectionReader::get_count(self)
65     }
66 }
67 
68 impl<'a> IntoIterator for LinkingSectionReader<'a> {
69     type Item = Result<LinkingType>;
70     type IntoIter = SectionIteratorLimited<LinkingSectionReader<'a>>;
71 
into_iter(self) -> Self::IntoIter72     fn into_iter(self) -> Self::IntoIter {
73         SectionIteratorLimited::new(self)
74     }
75 }
76