Implement Serialize for flexbuffer::Reader (#6635)

* Implement Serialize for flexbuffer::Reader

* bump version

* Remove use of experimantal or-patterns

* Remove more use of experimantal or-patterns

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2021-05-10 23:15:46 -04:00
committed by GitHub
parent a1730fcea8
commit 8fd10606c1
9 changed files with 125 additions and 40 deletions

View File

@@ -14,7 +14,7 @@
use crate::bitwidth::BitWidth;
use crate::flexbuffer_type::FlexBufferType;
use crate::{Buffer, Blob};
use crate::{Blob, Buffer};
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::Rem;
@@ -22,6 +22,7 @@ use std::str::FromStr;
mod de;
mod iter;
mod map;
mod serialize;
mod vector;
pub use de::DeserializationError;
pub use iter::ReaderIterator;
@@ -184,13 +185,12 @@ impl<B> std::fmt::Debug for Reader<B> {
}
}
macro_rules! try_cast_fn {
($name: ident, $full_width: ident, $Ty: ident) => {
pub fn $name(&self) -> $Ty {
self.$full_width().try_into().unwrap_or_default()
}
}
};
}
fn safe_sub(a: usize, b: usize) -> Result<usize, Error> {
@@ -242,7 +242,7 @@ impl<B: Buffer> Reader<B> {
}
/// Convenience function to get the underlying buffer. By using `shallow_copy`, this preserves
/// the lifetime that the underlying buffer has.
/// the lifetime that the underlying buffer has.
pub fn buffer(&self) -> B {
self.buffer.shallow_copy()
}
@@ -263,7 +263,11 @@ impl<B: Buffer> Reader<B> {
if let Some(len) = self.fxb_type.fixed_length_vector_length() {
len
} else if self.fxb_type.has_length_slot() && self.address >= self.width.n_bytes() {
read_usize(&self.buffer, self.address - self.width.n_bytes(), self.width)
read_usize(
&self.buffer,
self.address - self.width.n_bytes(),
self.width,
)
} else {
0
}
@@ -359,7 +363,8 @@ impl<B: Buffer> Reader<B> {
/// Retrieves the string value up until the first `\0` character.
pub fn get_key(&self) -> Result<B::BufferString, Error> {
let bytes = self.buffer
let bytes = self
.buffer
.slice(self.address..self.address + self.get_key_len()?)
.ok_or(Error::IndexOutOfBounds)?;
Ok(bytes.buffer_str()?)
@@ -368,9 +373,9 @@ impl<B: Buffer> Reader<B> {
pub fn get_blob(&self) -> Result<Blob<B>, Error> {
self.expect_type(FlexBufferType::Blob)?;
Ok(Blob(
self.buffer
.slice(self.address..self.address + self.length())
.ok_or(Error::IndexOutOfBounds)?
self.buffer
.slice(self.address..self.address + self.length())
.ok_or(Error::IndexOutOfBounds)?,
))
}
@@ -382,7 +387,9 @@ impl<B: Buffer> Reader<B> {
/// is out of bounds.
pub fn get_str(&self) -> Result<B::BufferString, Error> {
self.expect_type(FlexBufferType::String)?;
let bytes = self.buffer.slice(self.address..self.address + self.length());
let bytes = self
.buffer
.slice(self.address..self.address + self.length());
Ok(bytes.ok_or(Error::ReadUsizeOverflowed)?.buffer_str()?)
}
@@ -666,4 +673,4 @@ fn unpack_type(ty: u8) -> Result<(FlexBufferType, BitWidth), Error> {
let w = BitWidth::try_from(ty & 3u8).map_err(|_| Error::InvalidPackedType)?;
let t = FlexBufferType::try_from(ty >> 2).map_err(|_| Error::InvalidPackedType)?;
Ok((t, w))
}
}

View File

@@ -0,0 +1,76 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::reader::Reader;
use crate::Buffer;
use crate::{BitWidth::*, FlexBufferType::*};
use serde::ser;
use serde::ser::{SerializeMap, SerializeSeq};
impl<B: Buffer> ser::Serialize for &Reader<B> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
#[allow(deprecated)]
match (self.flexbuffer_type(), self.bitwidth()) {
(Null, _) => serializer.serialize_unit(),
(Int, W8) | (IndirectInt, W8) => serializer.serialize_i8(self.as_i8()),
(Int, W16) | (IndirectInt, W16) => serializer.serialize_i16(self.as_i16()),
(Int, W32) | (IndirectInt, W32) => serializer.serialize_i32(self.as_i32()),
(Int, W64) | (IndirectInt, W64) => serializer.serialize_i64(self.as_i64()),
(UInt, W8) | (IndirectUInt, W8) => serializer.serialize_u8(self.as_u8()),
(UInt, W16) | (IndirectUInt, W16) => serializer.serialize_u16(self.as_u16()),
(UInt, W32) | (IndirectUInt, W32) => serializer.serialize_u32(self.as_u32()),
(UInt, W64) | (IndirectUInt, W64) => serializer.serialize_u64(self.as_u64()),
(Float, W32) | (IndirectFloat, W32) => serializer.serialize_f32(self.as_f32()),
(Float, _) | (IndirectFloat, _) => serializer.serialize_f64(self.as_f64()),
(Bool, _) => serializer.serialize_bool(self.as_bool()),
(Key, _) | (String, _) => serializer.serialize_str(&self.as_str()),
(Map, _) => {
let m = self.as_map();
let mut map_serializer = serializer.serialize_map(Some(m.len()))?;
for (k, v) in m.iter_keys().zip(m.iter_values()) {
map_serializer.serialize_key(&&k)?;
map_serializer.serialize_value(&&v)?;
}
map_serializer.end()
}
(Vector, _)
| (VectorInt, _)
| (VectorUInt, _)
| (VectorFloat, _)
| (VectorKey, _)
| (VectorString, _)
| (VectorBool, _)
| (VectorInt2, _)
| (VectorUInt2, _)
| (VectorFloat2, _)
| (VectorInt3, _)
| (VectorUInt3, _)
| (VectorFloat3, _)
| (VectorInt4, _)
| (VectorUInt4, _)
| (VectorFloat4, _) => {
let v = self.as_vector();
let mut seq_serializer = serializer.serialize_seq(Some(v.len()))?;
for x in v.iter() {
seq_serializer.serialize_element(&&x)?;
}
seq_serializer.end()
}
(Blob, _) => serializer.serialize_bytes(&self.as_blob().0),
}
}
}

View File

@@ -40,7 +40,7 @@ impl<B: Buffer> Default for VectorReader<B> {
fn default() -> Self {
VectorReader {
reader: Reader::default(),
length: usize::default()
length: usize::default(),
}
}
}