wasefire_wire/
reader.rs

1// Copyright 2024 Google LLC
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
15use wasefire_error::{Code, Error};
16
17pub struct Reader<'a>(&'a [u8]);
18
19impl<'a> Reader<'a> {
20    pub(crate) fn new(data: &'a [u8]) -> Self {
21        Reader(data)
22    }
23
24    pub(crate) fn get(&mut self, offset: usize) -> Result<&'a [u8], Error> {
25        Error::user(Code::InvalidLength).check(offset <= self.0.len())?;
26        let head;
27        (head, self.0) = self.0.split_at(offset);
28        Ok(head)
29    }
30
31    pub(crate) fn finalize(self) -> &'a [u8] {
32        self.0
33    }
34}