Skip to main content

TLVWrite

Trait TLVWrite 

Source
pub trait TLVWrite {
    type Position: PartialEq + Copy;

Show 31 methods // Required method fn write(&mut self, byte: u8) -> Result<(), Error>; // Provided methods fn write_ctx<T>(&mut self, ctx: u8, value: &T) -> Result<(), Error> where T: ToTLV, Self: Sized { ... } fn tlv(&mut self, tag: &TLVTag, value: &TLVValue<'_>) -> Result<(), Error> { ... } fn i8(&mut self, tag: &TLVTag, data: i8) -> Result<(), Error> { ... } fn u8(&mut self, tag: &TLVTag, data: u8) -> Result<(), Error> { ... } fn i16(&mut self, tag: &TLVTag, data: i16) -> Result<(), Error> { ... } fn u16(&mut self, tag: &TLVTag, data: u16) -> Result<(), Error> { ... } fn i32(&mut self, tag: &TLVTag, data: i32) -> Result<(), Error> { ... } fn u32(&mut self, tag: &TLVTag, data: u32) -> Result<(), Error> { ... } fn i64(&mut self, tag: &TLVTag, data: i64) -> Result<(), Error> { ... } fn u64(&mut self, tag: &TLVTag, data: u64) -> Result<(), Error> { ... } fn f32(&mut self, tag: &TLVTag, data: f32) -> Result<(), Error> { ... } fn f64(&mut self, tag: &TLVTag, data: f64) -> Result<(), Error> { ... } fn str(&mut self, tag: &TLVTag, data: &[u8]) -> Result<(), Error> { ... } fn str_cb( &mut self, _tag: &TLVTag, _cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error> { ... } fn stri<I>( &mut self, tag: &TLVTag, len: usize, data: I, ) -> Result<(), Error> where I: IntoIterator<Item = u8> { ... } fn utf8(&mut self, tag: &TLVTag, data: &str) -> Result<(), Error> { ... } fn utf8i<I>( &mut self, tag: &TLVTag, len: usize, data: I, ) -> Result<(), Error> where I: IntoIterator<Item = u8> { ... } fn utf8_cb( &mut self, _tag: &TLVTag, _cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error> { ... } fn start_struct(&mut self, tag: &TLVTag) -> Result<(), Error> { ... } fn start_array(&mut self, tag: &TLVTag) -> Result<(), Error> { ... } fn start_list(&mut self, tag: &TLVTag) -> Result<(), Error> { ... } fn start_container( &mut self, tag: &TLVTag, container_type: TLVValueType, ) -> Result<(), Error> { ... } fn end_container(&mut self) -> Result<(), Error> { ... } fn null(&mut self, tag: &TLVTag) -> Result<(), Error> { ... } fn bool(&mut self, tag: &TLVTag, val: bool) -> Result<(), Error> { ... } fn raw_value( &mut self, tag: &TLVTag, value_type: TLVValueType, value_payload: &[u8], ) -> Result<(), Error> { ... } fn write_raw_data<I>(&mut self, bytes: I) -> Result<(), Error> where I: IntoIterator<Item = u8> { ... } fn get_tail(&self) -> Self::Position { ... } fn rewind_to(&mut self, _pos: Self::Position) { ... } fn available_space(&mut self) -> &mut [u8] { ... }
}
Expand description

A trait representing a storage where data can be serialized as a TLV stream. by synchronously emitting bytes to the storage.

The one method that needs to be implemented by user code is write.

The trait operates in an append-only manner without requiring access to the serialized TLV data, so it can be implemented with an in-memory storage, or a file storage, or anything that can output a byte to somewhere (like the Write Rust traits).

With that said, the trait has two additional methods that (optionally) allow for “rewinding” the storage. Implementing these is optional, and they currently exist only for backwards compatibility with code implemented prior to the introduction of this trait.

For iterator-style TLV serialization look at the ToTLVIter trait.

Required Associated Types§

Required Methods§

Source

fn write(&mut self, byte: u8) -> Result<(), Error>

Append a single byte to the TLV stream.

Provided Methods§

Source

fn write_ctx<T>(&mut self, ctx: u8, value: &T) -> Result<(), Error>
where T: ToTLV, Self: Sized,

Write value under a context tag.

Taking the tag as a runtime argument rather than baking it into the caller is what keeps this cheap in code size: the generated per-field builder setters are otherwise structurally identical yet monomorphise separately - and, because the builders carry both the parent chain and the field index in their type, once per (field, parent, index) rather than once per type.

Source

fn tlv(&mut self, tag: &TLVTag, value: &TLVValue<'_>) -> Result<(), Error>

Write a TLV tag and value to the TLV stream.

Source

fn i8(&mut self, tag: &TLVTag, data: i8) -> Result<(), Error>

Write a tag and a TLV S8 value to the TLV stream.

Source

fn u8(&mut self, tag: &TLVTag, data: u8) -> Result<(), Error>

Write a tag and a TLV U8 value to the TLV stream.

Source

fn i16(&mut self, tag: &TLVTag, data: i16) -> Result<(), Error>

Write a tag and a TLV S16 or (if the data is small enough) S8 value to the TLV stream.

Source

fn u16(&mut self, tag: &TLVTag, data: u16) -> Result<(), Error>

Write a tag and a TLV U16 or (if the data is small enough) U8 value to the TLV stream.

Source

fn i32(&mut self, tag: &TLVTag, data: i32) -> Result<(), Error>

Write a tag and a TLV S32 or (if the data is small enough) S16 or S8 value to the TLV stream.

Source

fn u32(&mut self, tag: &TLVTag, data: u32) -> Result<(), Error>

Write a tag and a TLV U32 or (if the data is small enough) U16 or U8 value to the TLV stream.

Source

fn i64(&mut self, tag: &TLVTag, data: i64) -> Result<(), Error>

Write a tag and a TLV S64 or (if the data is small enough) S32, S16, or S8 value to the TLV stream.

Source

fn u64(&mut self, tag: &TLVTag, data: u64) -> Result<(), Error>

Write a tag and a TLV U64 or (if the data is small enough) U32, U16, or U8 value to the TLV stream.

Source

fn f32(&mut self, tag: &TLVTag, data: f32) -> Result<(), Error>

Write a tag and a TLV F32 to the TLV stream.

Source

fn f64(&mut self, tag: &TLVTag, data: f64) -> Result<(), Error>

Write a tag and a TLV F64 to the TLV stream.

Source

fn str(&mut self, tag: &TLVTag, data: &[u8]) -> Result<(), Error>

Write a tag and a TLV Octet String to the TLV stream, where the Octet String is a slice of u8 bytes.

The exact octet string type (Str8l, Str16l, Str32l, or Str64l) is chosen based on the length of the data, whereas the smallest type filling the provided data length is chosen.

Source

fn str_cb( &mut self, _tag: &TLVTag, _cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error>

Write a tag and a TLV Octet String to the TLV stream, where the Octet String is a slice of u8 bytes.

The writing is done via a user-supplied callback cb, that is expected to fill the provided buffer with the data and to return the length of the written data.

This method is useful when the data to be written needs to be computed first, and the computation needs a buffer where to operate.

Note that this method always uses a Str16l value type to write the data, which restricts the data length to no more than 65535 bytes.

Note also that this method might not be supported by all TLVWrite implementations.

Source

fn stri<I>(&mut self, tag: &TLVTag, len: usize, data: I) -> Result<(), Error>
where I: IntoIterator<Item = u8>,

Write a tag and a TLV Octet String to the TLV stream, where the Octet String is anything that can be turned into an iterator of u8 bytes.

The exact octet string type (Str8l, Str16l, Str32l, or Str64l) is chosen based on the length of the data, whereas the smallest type filling the provided data length is chosen.

NOTE: The length of the Octet String must be provided by the user and it must match the number of bytes returned by the provided iterator, or else the generated TLV stream will be invalid.

Source

fn utf8(&mut self, tag: &TLVTag, data: &str) -> Result<(), Error>

Write a tag and a TLV UTF-8 String to the TLV stream, where the UTF-8 String is a str.

The exact UTF-8 string type (Utf8l, Utf16l, Utf32l, or Utf64l) is chosen based on the length of the data, whereas the smallest type filling the provided data length is chosen.

Source

fn utf8i<I>(&mut self, tag: &TLVTag, len: usize, data: I) -> Result<(), Error>
where I: IntoIterator<Item = u8>,

Write a tag and a TLV UTF-8 String to the TLV stream, where the UTF-8 String is anything that can be turned into an iterator of u8 bytes.

The exact UTF-8 string type (Utf8l, Utf16l, Utf32l, or Utf64l) is chosen based on the length of the data, whereas the smallest type filling the provided data length is chosen.

NOTE 1: The length of the UTF-8 String must be provided by the user and it must match the number of bytes returned by the provided iterator, or else the generated TLV stream will be invalid.

NOTE 2: The provided iterator must return valid UTF-8 bytes, or else the generated TLV stream will be invalid.

Source

fn utf8_cb( &mut self, _tag: &TLVTag, _cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error>

Write a tag and a TLV UTF-8 String to the TLV stream, where the UTF-8 String is a str.

The writing is done via a user-supplied callback cb, that is expected to fill the provided buffer with the data and to return the length of the written data.

This method is useful when the data to be written needs to be computed first, and the computation needs a buffer where to operate.

Note that this method always uses a Utf16l value type to write the data, which restricts the data length to no more than 65535 bytes.

Note also that this method might not be supported by all TLVWrite implementations.

Source

fn start_struct(&mut self, tag: &TLVTag) -> Result<(), Error>

Write a tag and a value indicating the start of a Struct TLV container.

NOTE: The user must call end_container after writing all the Struct fields to close the Struct container or else the generated TLV stream will be invalid.

Source

fn start_array(&mut self, tag: &TLVTag) -> Result<(), Error>

Write a tag and a value indicating the start of an Array TLV container.

NOTE: The user must call end_container after writing all the Array elements to close the Array container or else the generated TLV stream will be invalid.

Source

fn start_list(&mut self, tag: &TLVTag) -> Result<(), Error>

Write a tag and a value indicating the start of a List TLV container.

NOTE: The user must call end_container after writing all the List elements to close the List container or else the generated TLV stream will be invalid.

Source

fn start_container( &mut self, tag: &TLVTag, container_type: TLVValueType, ) -> Result<(), Error>

Write a tag and a value indicating the start of a Struct TLV container.

NOTE: The user must call end_container after writing all the Struct fields to close the Struct container or else the generated TLV stream will be invalid.

Source

fn end_container(&mut self) -> Result<(), Error>

Write a value indicating the end of a Struct, Array, or List TLV container.

NOTE: This method must be called only when the corresponding container has been opened using start_struct, start_array, or start_list, or else the generated TLV stream will be invalid.

Source

fn null(&mut self, tag: &TLVTag) -> Result<(), Error>

Write a tag and a TLV Null value to the TLV stream.

Source

fn bool(&mut self, tag: &TLVTag, val: bool) -> Result<(), Error>

Write a tag and a TLV True or False value to the TLV stream.

Source

fn raw_value( &mut self, tag: &TLVTag, value_type: TLVValueType, value_payload: &[u8], ) -> Result<(), Error>

Write a tag and a raw, already-encoded TLV value represented as a byte slice.

Source

fn write_raw_data<I>(&mut self, bytes: I) -> Result<(), Error>
where I: IntoIterator<Item = u8>,

Append multiple raw bytes to the TLV stream.

Source

fn get_tail(&self) -> Self::Position

Get the current position in the TLV stream.

NOTE: This method might not be supported by all implementations and therefore it might panic.

Source

fn rewind_to(&mut self, _pos: Self::Position)

Rewind the TLV stream to a previous position.

NOTE: This method might not be supported by all implementations and therefore it might panic.

Source

fn available_space(&mut self) -> &mut [u8]

Get a mutable slice of the available space in the TLV stream.

NOTE: This method assumes that the TLV write implementation is writing to a buffer, which might not be the case for all implementations. Therefore, this method might panic if the implementation does not support it.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> TLVWrite for &mut T
where T: TLVWrite,

Source§

type Position = <T as TLVWrite>::Position

Source§

fn write(&mut self, byte: u8) -> Result<(), Error>

Source§

fn get_tail(&self) -> <&mut T as TLVWrite>::Position

Source§

fn rewind_to(&mut self, pos: <&mut T as TLVWrite>::Position)

Source§

fn available_space(&mut self) -> &mut [u8]

Source§

fn str_cb( &mut self, tag: &TLVTag, cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error>

Source§

fn utf8_cb( &mut self, tag: &TLVTag, cb: impl FnOnce(&mut [u8]) -> Result<usize, Error>, ) -> Result<(), Error>

Implementors§