moon/fory Apache Fory · MoonBit

API · struct

Writer

Writing · defined at writer.mbt:11

pub(all) struct Writer {
  buf : @buffer.Buffer
  config : Config
  refs : Map[Int32, Int32]
  mut next_ref : Int32
  mut fresh : Int32
}

Where a value goes: a buffer, the configuration it is written under, and the table of what has already been sent.

Everything the specification fixes is a method here, so that a type this package has never heard of can be written with the same primitives its own types are written with. Nothing here is private to a codec.

Fields

FieldMeaning
buf : @buffer.Buffer

The bytes so far.

config : Config

What this message is written under.

refs : Map[Int32, Int32]

The caller's key for a shared value, and the reference id it was given.

MoonBit has no identity to infer sharing from, so the key is the caller's: two writes of the same key send the value once and point at it after.

mut next_ref : Int32

The next reference id.

mut fresh : Int32

The key handed to the next value that takes a reference id without being given one: a tracked value the caller did not name still has to occupy its place in the id order, and the key it is filed under descends from -2, below root_key.

Methods

buffer

fn Writer::buffer(Self) -> @buffer.Buffer

The buffer being written into, so that a caller who already has one does not have to copy out of it.

bytes

fn Writer::bytes(Self) -> Bytes

The bytes written so far.

length

fn Writer::length(Self) -> Int32

How many bytes have been written.

new

fn Writer::new(config? : Config, buffer? : @buffer.Buffer) -> Self

A writer with nothing in it.

write_binary

fn Writer::write_binary(Self, Bytes) -> Unit

Bytes with their length in front: the BINARY type's body.

write_bool

fn Writer::write_bool(Self, Bool) -> Unit

A boolean: one byte, 1 or 0.

write_byte

fn Writer::write_byte(Self, Int32) -> Unit

One byte.

write_bytes

fn Writer::write_bytes(Self, Bytes) -> Unit

Bytes as they are, with no length: for the caller who has already written one.

write_f32

fn Writer::write_f32(Self, Float) -> Unit

A single-precision float: IEEE 754 binary32, little endian, NaN preserved.

write_f64

fn Writer::write_f64(Self, Double) -> Unit

A double-precision float: IEEE 754 binary64, little endian, NaN preserved.

write_fresh

fn Writer::write_fresh(Self) -> Unit

The flag for a value that is tracked but was never named: it takes the next reference id, under a key of this writer's own below every caller's, so that the ids of the references that do name something still line up with a peer's.

write_head

fn Writer::write_head(Self) -> Unit

The header byte: the cross-language flag in bit 0.

The specification leaves bit 1 for out-of-band data and asks that it be 0 when there is none, which is always here: this package writes no out-of-band buffer.

write_i16

fn Writer::write_i16(Self, Int32) -> Unit

Sixteen bits, signed, little endian.

write_i32

fn Writer::write_i32(Self, Int32) -> Unit

Thirty-two bits, signed, little endian.

write_i64

fn Writer::write_i64(Self, Int64) -> Unit

Sixty-four bits, signed, little endian.

write_i8

fn Writer::write_i8(Self, Int32) -> Unit

Eight bits, signed or unsigned: the byte is the same either way.

write_null_flag

fn Writer::write_null_flag(Self, Bool) -> Unit

The null flag: 0xFD for a value that is not there, 0xFF for one that is.

write_ref

fn Writer::write_ref(Self, Int32) -> Bool

The flag for a reference to a value that has already been written, and whether it was: a key that names nothing yet writes nothing and answers false, because a reference to a value that is not on the wire cannot be written as one.

write_string

fn Writer::write_string(Self, String) -> Unit

A string: the header, then the bytes.

The encoding chosen is UTF-8, which the specification allows every language to read; LATIN1 and UTF-16 are read but not written, because a MoonBit string is Unicode and narrowing it would lose what is not in the other one.

write_tagged_i64

fn Writer::write_tagged_i64(Self, Int64) -> Unit

A sixty-four-bit signed integer that is usually small: four bytes carrying v << 1, or 1 and then eight bytes when it does not fit.

The low bit of the first four bytes is the tag, which is why the range that fits is [-2^30, 2^30-1] and not the whole of the thirty-two bits.

write_tagged_u64

fn Writer::write_tagged_u64(Self, UInt64) -> Unit

A sixty-four-bit unsigned integer that is usually small: four bytes carrying v << 1, or 1 and then eight bytes when it does not fit.

write_tracked

fn Writer::write_tracked(Self, Int32) -> Bool

The envelope of a value that may be written more than once, and whether the body still has to be written.

key is the caller's name for the value. The first time it is met, 0x00 goes out and the answer is true; after that, 0xFE and the reference id go out and the answer is false, because the value is already on the wire.

write_type_id

fn Writer::write_type_id(Self, TypeId) -> Unit

A type ID byte.

write_u16

fn Writer::write_u16(Self, Int32) -> Unit

Sixteen bits, unsigned, little endian.

write_u32

fn Writer::write_u32(Self, UInt32) -> Unit

Thirty-two bits, unsigned, little endian.

write_u64

fn Writer::write_u64(Self, UInt64) -> Unit

Sixty-four bits, unsigned, little endian.

write_u8

fn Writer::write_u8(Self, Int32) -> Unit

Eight bits, unsigned.

write_user_type

fn Writer::write_user_type(Self, TypeId, Int32) -> Unit

A type ID byte and the user type ID that follows it: how a struct, an enum or an extension names its own type in same-schema mode.

write_var_i32

fn Writer::write_var_i32(Self, Int32) -> Unit

The same for thirty-two bits.

write_var_i64

fn Writer::write_var_i64(Self, Int64) -> Unit

A signed integer folded onto an unsigned one by zigzag, then written as a varint: a small negative stays one byte.

write_var_u32

fn Writer::write_var_u32(Self, UInt32) -> Unit

A base-128 varint of thirty-two bits.

write_var_u36

fn Writer::write_var_u36(Self, UInt64) -> Unit

The string header: size << 2 | encoding, written as one byte while it fits in seven bits and as a varint when it does not.

Sizes under thirty-two bytes are most strings, so the single byte is the case worth having.

write_var_u64

fn Writer::write_var_u64(Self, UInt64) -> Unit

A base-128 varint, least significant group first, the high bit marking that more follow.

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