moon/fory Apache Fory · MoonBit

API · trait

Codec

Messages · defined at codec.mbt:16

pub trait Codec {
  fn ty(Self) -> TypeId
  fn write_data(Self, Writer) -> Unit raise
  fn read_data(Reader, TypeId) -> Self raise
}

A value that knows how to become bytes, and how to come back from them.

The contract is one line: write_data and read_data are everything after the type ID. The type ID belongs to whoever knows the schema — a struct field to its FieldMeta, a list to its header, a map to its chunk header — and what follows belongs to the value, which is why a struct or an enum writes its user type ID here and not there.

Everything this package ships implements this trait, and a caller's own type implements it the same way: there is no second path for the types that come with the library.

Required methods

MethodMeaning
fn ty(Self) -> TypeId

The type ID: the number that begins a value of this type.

fn write_data(Self, Writer) -> Unit raise

Everything after the type ID.

fn read_data(Reader, TypeId) -> Self raise

Everything after the type ID, read back. ty is what the wire said, which is not always what the writer would have chosen: a peer's 1 may come as VARINT64, and an integer asked for as thirty-two bits may arrive as sixty-four.

Implementors

pub impl Codec for Bool

pub impl Codec for Byte

pub impl Codec for Int32

pub impl Codec for Int64

pub impl Codec for UInt32

pub impl Codec for UInt64

pub impl Codec for Float

pub impl Codec for Double

pub impl Codec for String

pub impl Codec for Bytes

pub impl[T : Codec] Codec for Array[T]

pub impl[K : Codec + Hash + Eq, V : Codec] Codec for Map[K, V]

Generated from src/pkg.generated.mbti and the doc comments beside this declaration.