Struct Reader

Source
pub struct Reader<B> { /* private fields */ }
Expand description

Readers allow access to data stored in a Flexbuffer.

Each reader represents a single address in the buffer so data is read lazily. Start a reader by calling get_root on your flexbuffer &[u8].

  • The get_T methods return a Result<T, Error>. They return an OK value if and only if the flexbuffer type matches T. This is analogous to the behavior of Rust’s json library, though with Result instead of Option.
  • The as_T methods will try their best to return to a value of type T (by casting or even parsing a string if necessary) but ultimately returns T::default if it fails. This behavior is analogous to that of flexbuffers C++.

Implementations§

Source§

impl<B: Buffer> Reader<B>

Source

pub fn get_root(buffer: B) -> Result<Self, Error>

Parses the flexbuffer from the given buffer. Assumes the flexbuffer root is the last byte of the buffer.

Source

pub fn buffer(&self) -> B

Convenience function to get the underlying buffer. By using shallow_copy, this preserves the lifetime that the underlying buffer has.

Source

pub fn flexbuffer_type(&self) -> FlexBufferType

Returns the FlexBufferType of this Reader.

Source

pub fn bitwidth(&self) -> BitWidth

Returns the bitwidth of this Reader.

Source

pub fn length(&self) -> usize

Returns the length of the Flexbuffer. If the type has no length, or if an error occurs, 0 is returned.

Source

pub fn is_aligned(&self) -> bool

Returns true if the flexbuffer is aligned to 8 bytes. This guarantees, for valid flexbuffers, that the data is correctly aligned in memory and slices can be read directly e.g. with get_f64s or get_i16s.

Source

pub fn as_vector(&self) -> VectorReader<B>

Source

pub fn as_map(&self) -> MapReader<B>

Source

pub fn get_slice<T: ReadLE>(&self) -> Result<&[T], Error>

👎Deprecated since 0.3.0: This function is unsafe - if this functionality is needed use Reader::buffer::align_to

Directly reads a slice of type T where T is one of u8,u16,u32,u64,i8,i16,i32,i64,f32,f64. Returns Err if the type, bitwidth, or memory alignment does not match. Since the bitwidth is dynamic, its better to use a VectorReader unless you know your data and performance is critical.

Source

pub fn get_bool(&self) -> Result<bool, Error>

Returns the value of the reader if it is a boolean. Otherwise Returns error.

Source

pub fn get_key(&self) -> Result<B::BufferString, Error>

Retrieves the string value up until the first \0 character.

Source

pub fn get_blob(&self) -> Result<Blob<B>, Error>

Source

pub fn as_blob(&self) -> Blob<B>

Source

pub fn get_str(&self) -> Result<B::BufferString, Error>

Retrieves str pointer, errors if invalid UTF-8, or the provided index is out of bounds.

Source

pub fn get_map(&self) -> Result<MapReader<B>, Error>

Source

pub fn get_u64(&self) -> Result<u64, Error>

Tries to read a FlexBufferType::UInt. Returns Err if the type is not a UInt or if the address is out of bounds.

Source

pub fn get_i64(&self) -> Result<i64, Error>

Tries to read a FlexBufferType::Int. Returns Err if the type is not a UInt or if the address is out of bounds.

Source

pub fn get_f64(&self) -> Result<f64, Error>

Tries to read a FlexBufferType::Float. Returns Err if the type is not a UInt, if the address is out of bounds, or if its a f16 or f8 (not currently supported).

Source

pub fn as_bool(&self) -> bool

Source

pub fn as_u64(&self) -> u64

Returns a u64, casting if necessary. For Maps and Vectors, their length is returned. If anything fails, 0 is returned.

Source

pub fn as_u32(&self) -> u32

Source

pub fn as_u16(&self) -> u16

Source

pub fn as_u8(&self) -> u8

Source

pub fn as_i64(&self) -> i64

Returns an i64, casting if necessary. For Maps and Vectors, their length is returned. If anything fails, 0 is returned.

Source

pub fn as_i32(&self) -> i32

Source

pub fn as_i16(&self) -> i16

Source

pub fn as_i8(&self) -> i8

Source

pub fn as_f64(&self) -> f64

Returns an f64, casting if necessary. For Maps and Vectors, their length is returned. If anything fails, 0 is returned.

Source

pub fn as_f32(&self) -> f32

Source

pub fn as_str(&self) -> B::BufferString

Returns empty string if you’re not trying to read a string.

Source

pub fn get_vector(&self) -> Result<VectorReader<B>, Error>

Trait Implementations§

Source§

impl<B: Buffer> Clone for Reader<B>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B> Debug for Reader<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B: Buffer> Default for Reader<B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserializer<'de> for Reader<&'de [u8]>

Source§

type Error = DeserializationError

The error type that can be returned if some error occurs during deserialization.
Source§

fn is_human_readable(&self) -> bool

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more
Source§

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Source§

fn deserialize_bool<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a bool value.
Source§

fn deserialize_i8<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i8 value.
Source§

fn deserialize_i16<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i16 value.
Source§

fn deserialize_i32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i32 value.
Source§

fn deserialize_i64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i64 value.
Source§

fn deserialize_u8<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u8 value.
Source§

fn deserialize_u16<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u16 value.
Source§

fn deserialize_u32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u32 value.
Source§

fn deserialize_u64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u64 value.
Source§

fn deserialize_f32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f32 value.
Source§

fn deserialize_f64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f64 value.
Source§

fn deserialize_str<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_unit<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit value.
Source§

fn deserialize_unit_struct<V>( self, name: &'static str, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit struct with a particular name.
Source§

fn deserialize_bytes<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_ignored_any<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Source§

fn deserialize_map<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a map of key-value pairs.
Source§

fn deserialize_identifier<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant.
Source§

fn deserialize_struct<V>( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a struct with a particular name and fields.
Source§

fn deserialize_tuple<V>( self, len: usize, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data.
Source§

fn deserialize_tuple_struct<V>( self, name: &'static str, len: usize, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields.
Source§

fn deserialize_seq<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values.
Source§

fn deserialize_string<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a char value.
Source§

fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an optional value. Read more
Source§

fn deserialize_newtype_struct<V>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a newtype struct with a particular name.
Source§

fn deserialize_enum<V>( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Source§

fn deserialize_i128<V>( self, visitor: V, ) -> Result<<V as Visitor<'de>>::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i128 value. Read more
Source§

fn deserialize_u128<V>( self, visitor: V, ) -> Result<<V as Visitor<'de>>::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an u128 value. Read more
Source§

impl<B: Buffer> Display for Reader<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B: Buffer> Serialize for &Reader<B>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'de> VariantAccess<'de> for Reader<&'de [u8]>

Source§

type Error = DeserializationError

The error type that can be returned if some error occurs during deserialization. Must match the error type of our EnumAccess.
Source§

fn unit_variant(self) -> Result<(), Self::Error>

Called when deserializing a variant with no values. Read more
Source§

fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed<'de>,

Called when deserializing a variant with a single value. Read more
Source§

fn tuple_variant<V>( self, _len: usize, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Called when deserializing a tuple-like variant. Read more
Source§

fn struct_variant<V>( self, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Called when deserializing a struct-like variant. Read more
Source§

fn newtype_variant<T>(self) -> Result<T, Self::Error>
where T: Deserialize<'de>,

Called when deserializing a variant with a single value. Read more

Auto Trait Implementations§

§

impl<B> Freeze for Reader<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for Reader<B>
where B: RefUnwindSafe,

§

impl<B> Send for Reader<B>
where B: Send,

§

impl<B> Sync for Reader<B>
where B: Sync,

§

impl<B> Unpin for Reader<B>
where B: Unpin,

§

impl<B> UnwindSafe for Reader<B>
where B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.