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 | //! Flexbuffers is a high performance schemaless binary data format designed at Google. |
| 16 | //! It is complementary to the schema-ed format [Flatbuffers](http://docs.rs/flatbuffers/). |
| 17 | //! See [Flexbuffer Internals](https://google.github.io/flatbuffers/flatbuffers_internals.html) |
| 18 | //! for details on the binary format. |
| 19 | //! |
| 20 | //! See the examples for usage: |
| 21 | //! * [Example](https://github.com/google/flatbuffers/blob/master/samples/sample_flexbuffers.rs) |
| 22 | //! * [Serde Example](https://github.com/google/flatbuffers/blob/master/samples/sample_flexbuffers_serde.rs) |
| 23 | //! |
| 24 | //! This rust implementation is in progress and, until the 1.0 release, breaking API changes may |
| 25 | //! happen between minor versions. |
| 26 | // TODO(cneo): serde stuff are behind a default-on feature flag |
| 27 | // Reader to Json is behind a default-off feature flag |
| 28 | // Serializable structs are Pushable |
| 29 | // Serde with maps - field names and type names. |
| 30 | |
| 31 | #[macro_use] |
| 32 | extern crate bitflags; |
| 33 | extern crate byteorder; |
| 34 | #[macro_use] |
| 35 | extern crate serde_derive; |
| 36 | extern crate num_enum; |
| 37 | extern crate serde; |
| 38 | |
| 39 | mod bitwidth; |
| 40 | mod builder; |
| 41 | mod flexbuffer_type; |
| 42 | mod reader; |
| 43 | pub use bitwidth::BitWidth; |
| 44 | pub use builder::Error as SerializationError; |
| 45 | pub use builder::{ |
| 46 | singleton, Builder, BuilderOptions, FlexbufferSerializer, MapBuilder, Pushable, VectorBuilder, |
| 47 | }; |
| 48 | pub use flexbuffer_type::FlexBufferType; |
| 49 | pub use reader::Error as ReaderError; |
| 50 | pub use reader::{DeserializationError, MapReader, Reader, ReaderIterator, VectorReader}; |
| 51 | use serde::{Deserialize, Serialize}; |
| 52 | |
| 53 | mod private { |
| 54 | pub trait Sealed {} |
| 55 | } |
| 56 | |
| 57 | /// Serialize as a flexbuffer into a vector. |
| 58 | pub fn to_vec<T: Serialize>(x: T) -> Result<Vec<u8>, SerializationError> { |
| 59 | let mut s = FlexbufferSerializer::new(); |
| 60 | x.serialize(&mut s)?; |
| 61 | Ok(s.take_buffer()) |
| 62 | } |
| 63 | /// Deserialize a type from a flexbuffer. |
| 64 | pub fn from_slice<'de, T: Deserialize<'de>>(buf: &'de [u8]) -> Result<T, DeserializationError> { |
| 65 | let r = Reader::get_root(buf)?; |
| 66 | T::deserialize(r) |
| 67 | } |
| 68 | |
| 69 | /// This struct, when pushed will be serialized as a `FlexBufferType::Blob`. |
| 70 | /// |
| 71 | /// A `Blob` is a variable width `length` followed by that many bytes of data. |
| 72 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 73 | pub struct Blob<'a>(pub &'a [u8]); |
| 74 | |
| 75 | /// This struct, when pushed, will be serialized as a `FlexBufferType::IndirectUInt`. |
| 76 | /// |
| 77 | /// It is an unsigned integer stored by reference in the flexbuffer. This can reduce the |
| 78 | /// size of vectors and maps containing the `IndirectUInt`. |
| 79 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 80 | pub struct IndirectUInt(pub u64); |
| 81 | |
| 82 | /// This struct, when pushed, will be serialized as a `FlexBufferType::IndirectInt`. |
| 83 | /// |
| 84 | /// It is a signed integer stored by reference in the flexbuffer. This can reduce the |
| 85 | /// size of vectors and maps containing the `IndirectInt`. |
| 86 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 87 | pub struct IndirectInt(pub i64); |
| 88 | |
| 89 | /// This struct, when pushed, will be serialized as a `FlexBufferType::IndirectFloat`. |
| 90 | /// |
| 91 | /// It is a floating point stored by reference in the flexbuffer. This can reduce the |
| 92 | /// size of vectors and maps containing the `IndirectFloat`. |
| 93 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 94 | pub struct IndirectFloat(pub f64); |