Trait mz_persist_types::Codec

source ·
pub trait Codec: Sized + 'static {
    type Schema: Schema<Self>;
    type Storage;

    // Required methods
    fn codec_name() -> String;
    fn encode<B>(&self, buf: &mut B)
       where B: BufMut;
    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String>;

    // Provided method
    fn decode_from<'a>(
        &mut self,
        buf: &'a [u8],
        _storage: &mut Option<Self::Storage>
    ) -> Result<(), String> { ... }
}
Expand description

Encoding and decoding operations for a type usable as a persisted key or value.

Required Associated Types§

source

type Schema: Schema<Self>

The type of the associated schema for Self.

This is a separate type because Row is not self-describing. For Row, you need a RelationDesc to determine the types of any columns that are Datum::Null.

source

type Storage

A type used with Self::decode_from for allocation reuse. Set to () if unnecessary.

Required Methods§

source

fn codec_name() -> String

Name of the codec.

This name is stored for the key and value when a stream is first created and the same key and value codec must be used for that stream afterward.

source

fn encode<B>(&self, buf: &mut B)
where B: BufMut,

Encode a key or value for permanent storage.

This must perfectly round-trip Self through Codec::decode. If the encode function for this codec ever changes, decode must be able to handle bytes output by all previous versions of encode.

source

fn decode<'a>(buf: &'a [u8]) -> Result<Self, String>

Decode a key or value previous encoded with this codec’s Codec::encode.

This must perfectly round-trip Self through Codec::encode. If the encode function for this codec ever changes, decode must be able to handle bytes output by all previous versions of encode.

It should also gracefully handle data encoded by future versions of encode (likely with an error).

Provided Methods§

source

fn decode_from<'a>( &mut self, buf: &'a [u8], _storage: &mut Option<Self::Storage> ) -> Result<(), String>

An alternate form of Self::decode which enables amortizing allocs.

First, instead of returning Self, it takes &mut Self as a parameter, allowing for reuse of the internal Row/SourceData allocations.

Second, it adds a type Storage to Codec and also passes it in as &mut Option<Self::Storage>, allowing for reuse of ProtoRow/ProtoSourceData allocations. If Some, this impl may attempt to reuse allocations with it. It may also leave allocations in it for use in future calls to decode_from.

A default implementation is provided for Codec impls that can’t take advantage of this.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Codec for ()

§

type Storage = ()

§

type Schema = UnitSchema

source§

fn codec_name() -> String

source§

fn encode<B>(&self, _buf: &mut B)
where B: BufMut,

source§

fn decode<'a>(buf: &'a [u8]) -> Result<Self, String>

source§

impl Codec for String

§

type Storage = ()

§

type Schema = StringSchema

source§

fn codec_name() -> String

source§

fn encode<B>(&self, buf: &mut B)
where B: BufMut,

source§

fn decode<'a>(buf: &'a [u8]) -> Result<Self, String>

source§

impl Codec for Vec<u8>

§

type Storage = ()

§

type Schema = VecU8Schema

source§

fn codec_name() -> String

source§

fn encode<B>(&self, buf: &mut B)
where B: BufMut,

source§

fn decode<'a>(buf: &'a [u8]) -> Result<Self, String>

Implementors§