Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 1 | // Copyright 2019 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 | // https://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 | use super::{unpack_type, Error, Reader, ReaderIterator}; |
| 16 | use crate::{BitWidth, FlexBufferType}; |
| 17 | |
| 18 | #[derive(Default, Clone)] |
| 19 | /// Allows indexing on any flexbuffer vector type, (heterogenous vector, typed vector, or fixed |
| 20 | /// length typed vector). |
| 21 | /// |
| 22 | /// VectorReaders may be indexed with usize, `index` returns a result type |
| 23 | /// which may indicate failure due to indexing out of bounds or bad data. `idx` returns a |
| 24 | /// Null Reader in the event of any failure. |
| 25 | pub struct VectorReader<'de> { |
| 26 | pub(super) reader: Reader<'de>, |
| 27 | // Cache the length because read_usize can be slow. |
| 28 | pub(super) length: usize, |
| 29 | } |
| 30 | |
| 31 | impl<'de> VectorReader<'de> { |
| 32 | /// Returns the number of elements in the vector. |
| 33 | pub fn len(&self) -> usize { |
| 34 | self.length |
| 35 | } |
| 36 | /// Returns true if there are 0 elements in the vector. |
| 37 | pub fn is_empty(&self) -> bool { |
| 38 | self.length == 0 |
| 39 | } |
| 40 | fn get_elem_type(&self, i: usize) -> Result<(FlexBufferType, BitWidth), Error> { |
| 41 | if let Some(ty) = self.reader.fxb_type.typed_vector_type() { |
| 42 | Ok((ty, self.reader.width)) |
| 43 | } else { |
| 44 | let types_addr = self.reader.address + self.length * self.reader.width.n_bytes(); |
| 45 | self.reader |
| 46 | .buffer |
| 47 | .get(types_addr + i) |
| 48 | .ok_or(Error::FlexbufferOutOfBounds) |
| 49 | .and_then(|&t| unpack_type(t)) |
| 50 | } |
| 51 | } |
| 52 | /// Index into a flexbuffer vector. Any errors are defaulted to Null Readers. |
| 53 | pub fn idx(&self, i: usize) -> Reader<'de> { |
| 54 | self.index(i).unwrap_or_default() |
| 55 | } |
| 56 | /// Index into a flexbuffer. |
| 57 | pub fn index(&self, i: usize) -> Result<Reader<'de>, Error> { |
| 58 | if i >= self.length { |
| 59 | return Err(Error::IndexOutOfBounds); |
| 60 | } |
| 61 | let (fxb_type, bw) = self.get_elem_type(i)?; |
| 62 | let data_address = self.reader.address + self.reader.width.n_bytes() * i; |
| 63 | Reader::new( |
| 64 | self.reader.buffer, |
| 65 | data_address, |
| 66 | fxb_type, |
| 67 | bw, |
| 68 | self.reader.width, |
| 69 | ) |
| 70 | } |
| 71 | pub fn iter(&self) -> ReaderIterator<'de> { |
| 72 | ReaderIterator::new(self.clone()) |
| 73 | } |
| 74 | } |