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.
Required Associated Constants§
Sourceconst SIZE: DataSize
const SIZE: DataSize
The size characteristic of the data type.
The DataSize can either be a fixed size in bytes or dynamic.
Sourceconst ALIGNMENT: PageOffset
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§
Sourcefn decode(data: Cow<'_, [u8]>) -> MemoryResult<Self>where
Self: Sized,
fn decode(data: Cow<'_, [u8]>) -> MemoryResult<Self>where
Self: Sized,
Decodes the data type from a slice of bytes.
Sourcefn size(&self) -> MSize
fn size(&self) -> MSize
Returns the size in bytes of the encoded data type.
Implementations must never panic or wrap: when the encoded size does
not fit an MSize, they must return MSize::MAX. No record of
that size fits a page together with its header, so storage rejects it
with MemoryError::DataTooLarge
before calling Self::encode. The output of Self::encode is
unspecified for such values.
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 Encode for Vec<Value>
Encodes a Vec<Value> as [count: u32 LE] + [for each value: [size: u32 LE] + value.encode()].
impl Encode for Vec<Value>
Encodes a Vec<Value> as [count: u32 LE] + [for each value: [size: u32 LE] + value.encode()].
This is used as the key type for B-tree indexes, supporting both single-column and composite indexes uniformly.
Implementors§
Source§impl Encode for ColumnSnapshot
impl Encode for ColumnSnapshot
Source§impl Encode for DataTypeSnapshot
impl Encode for DataTypeSnapshot
Source§impl Encode for ForeignKeySnapshot
impl Encode for ForeignKeySnapshot
Source§impl Encode for IndexSnapshot
impl Encode for IndexSnapshot
Source§impl Encode for TableSchemaSnapshot
impl Encode for TableSchemaSnapshot
Source§impl Encode for Value
Encodes a Value as [discriminant: u8] + [inner_type.encode()].
impl Encode for Value
Encodes a Value as [discriminant: u8] + [inner_type.encode()].
For Null, only the discriminant byte is written.
For Custom, the encoding is [discriminant] + [tag_len: u16 LE] + [tag_bytes] + [data_len: u16 LE] + [encoded_bytes].
Encode::size saturates at MSize::MAX when a value does not fit the
2-byte length prefixes; storage rejects such values before encoding.