Encode

Trait Encode 

Source
pub trait Encode: Clone {
    const SIZE: DataSize;
    const ALIGNMENT: PageOffset;

    // Required methods
    fn encode(&self) -> Cow<'_, [u8]>;
    fn decode(data: Cow<'_, [u8]>) -> MemoryResult<Self>
       where Self: Sized;
    fn size(&self) -> MSize;
}
Expand description

This trait defines the encoding and decoding behaviour for data types used in the DBMS canister.

Required Associated Constants§

Source

const SIZE: DataSize

The size characteristic of the data type.

The DataSize can either be a fixed size in bytes or dynamic.

Source

const ALIGNMENT: PageOffset

The alignment requirement in bytes for the data type.

If Self::SIZE is DataSize::Fixed, the alignment must be equal to the size, otherwise it can be any value.

This value should never be less than 8 for DataSize::Dynamic types to ensure proper memory alignment.

We should set a default value (probably 32) for dynamic types to avoid misalignment issues, but letting an expert user to override it if necessary.

Required Methods§

Source

fn encode(&self) -> Cow<'_, [u8]>

Encodes the data type into a vector of bytes.

Source

fn decode(data: Cow<'_, [u8]>) -> MemoryResult<Self>
where Self: Sized,

Decodes the data type from a slice of bytes.

Source

fn size(&self) -> MSize

Returns the size in bytes of the encoded data type.

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.

Implementors§

Source§

impl Encode for Blob

Source§

const SIZE: DataSize = DataSize::Dynamic

Source§

const ALIGNMENT: PageOffset = 32u16

Source§

impl Encode for Boolean

Source§

impl Encode for Date

Source§

impl Encode for DateTime

Source§

impl Encode for Decimal

Source§

impl Encode for Int8

Source§

impl Encode for Int16

Source§

impl Encode for Int32

Source§

impl Encode for Int64

Source§

impl Encode for Principal

Source§

const SIZE: DataSize = DataSize::Dynamic

Source§

const ALIGNMENT: PageOffset = 32u16

Source§

impl Encode for Text

Source§

const SIZE: DataSize = DataSize::Dynamic

Source§

const ALIGNMENT: PageOffset = 32u16

Source§

impl Encode for Uint8

Source§

impl Encode for Uint16

Source§

impl Encode for Uint32

Source§

impl Encode for Uint64

Source§

impl Encode for Uuid

Source§

impl<T> Encode for Nullable<T>
where T: DataType,

Source§

const SIZE: DataSize = DataSize::Dynamic

Source§

const ALIGNMENT: PageOffset = 32u16