hanki

encode

stdlib/core/encode.hk: the Encode trait and its builtin impls.

Encode is the write half of the format-generic serialization framework (HANKI.md §16). A type is Encode when it can write itself to any Serializer (§ core serializer): encode! names the structural events scalars, an Option's presence, the framing of a sequence, map, struct or variant, and the serializer turns them into one wire format. A single @derive(Encode) serves every format; pick the bytes by passing a different serializer.

encode! declares its own bounded generic <S: Serializer> (a method-own trait generic, §10): the caller chooses the serializer, and one threads through the whole value tree. It is an action (!) with an empty effect row: the mutation is in the serializer's sink, committing no real-world capability.

Encode

trait Encode

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Writes self to s as a sequence of structural events.

s = BinarySerializer(out=BytesBuilder.new!())
65u8.encode!(s)
s.out.finish!() => "A".to_bytes()

impl Encode<bool>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the bool through the serializer.

@no-doctest: see the trait method

impl Encode<u8>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the u8 through the serializer.

@no-doctest: see the trait method

impl Encode<u16>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the u16 through the serializer.

@no-doctest: see the trait method

impl Encode<u32>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the u32 through the serializer.

@no-doctest: see the trait method

impl Encode<u64>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the u64 through the serializer.

@no-doctest: see the trait method

impl Encode<i32>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the i32 through the serializer.

@no-doctest: see the trait method

impl Encode<i64>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the i64 through the serializer.

@no-doctest: see the trait method

impl Encode<f64>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the f64 through the serializer.

@no-doctest: see the trait method

impl Encode<int>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the int through the serializer.

@no-doctest: see the trait method

impl Encode<decimal>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the decimal through the serializer.

@no-doctest: see the trait method

impl Encode<rational>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the rational through the serializer.

@no-doctest: see the trait method

impl Encode<f32>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the f32 through the serializer.

@no-doctest: see the trait method

impl Encode<string>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the string through the serializer.

@no-doctest: see the trait method

impl Encode<bytes>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the bytes through the serializer.

@no-doctest: see the trait method

listencode!

def _list_encode!<T: Encode, S: Serializer>(xs: List<T>, s: S) -> ()

A List writes its element count, then each element in order. A counting loop. Per-element recursion would consume call stack proportional to the list on the AOT tier.

impl<T: Encode> Encode<List<T>>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes the list as a sequence of its elements.

@no-doctest: see the trait method

optionencode_some!

def _option_encode_some!<T: Encode, S: Serializer>(v: T, s: S) -> ()

A Some payload follows its marker in a second statement, and it therefore sits in a helper (a match arm is a single expression; HANKI.md §8).

impl<T: Encode> Encode<Option<T>>

encode!

def encode!<S: Serializer>(self, s: S) -> ()

Encodes None as absent, Some as the wrapped value.

@no-doctest: see the trait method