AsDynSizeBytes

Trait AsDynSizeBytes 

Source
pub trait AsDynSizeBytes {
    // Required methods
    fn as_dyn_size_bytes(&self) -> Vec<u8> ;
    fn from_dyn_size_bytes(buf: &[u8]) -> Self;
}
Expand description

Trait allowing encoding and decoding of unsized data.

See also SBox.

By default is implemented for:

  1. Every AsFixedSizeBytes type
  2. Vec<u8>
  3. String

This trait can be easily implemented using derive macros:

  1. [derive::CandidAsDynSizeBytes] implements this trait for types which already implement candid::CandidType and candid::Deserialize.
  2. [derive::FixedSizeAsDynSizeBytes] implements this trait for types which already implement AsFixedSizeBytes.

Required Methods§

Source

fn as_dyn_size_bytes(&self) -> Vec<u8>

Encodes self into vector of bytes

§Panics

Should panic if data encoding failed.

Source

fn from_dyn_size_bytes(buf: &[u8]) -> Self

Decodes self from a slice of bytes.

§Important

The slice can have trailing bytes with unmeaningful. It means, that if your data encoded value is [1, 0, 1, 0], then it should also be able to decode itself from a slice like [1, 0, 1, 0, 0, 0, 0, 0, 0] or [1, 0, 1, 0, 1, 1, 0, 1].

§Panics

Should panic if data decoding failed.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AsDynSizeBytes for String

Available on non-crate feature custom_dyn_encoding only.
Source§

impl AsDynSizeBytes for Vec<u8>

Available on non-crate feature custom_dyn_encoding only.

Implementors§

Source§

impl<T: AsFixedSizeBytes> AsDynSizeBytes for T

Available on non-crate feature custom_dyn_encoding only.