moon/fory Apache Fory · MoonBit

API · struct

Reader

Reading · defined at reader.mbt:37

pub(all) struct Reader {
  view : BytesView
  config : Config
  mut at : Int32
  refs : Array[Int32]
  nodes : Map[Int32, Value]
  schemas : Map[Int32, StructMeta]
  mut replay : Int32
  replaying : Array[Int32]
  mut depth : Int32
  mut spent : Int32
}

Where a value comes from: the bytes, the place in them, and what has been read already.

The mirror of Writer. A reader keeps two things about references: where each one started, so a value can be read again, and the container being built for each one, so a cycle can point at a value that is not finished.

Fields

FieldMeaning
view : BytesView

The bytes being read.

config : Config

What this message is read under.

mut at : Int32

Where the next byte is.

refs : Array[Int32]

Where the value of each reference id starts.

nodes : Map[Int32, Value]

The container being built for each reference id, before it is filled.

schemas : Map[Int32, StructMeta]

The structs this reader knows, by the user type ID they are registered under: same-schema mode is an agreement, and this is where it is kept.

mut replay : Int32

How deep in a replay this read is: a value being read a second time for a reference does not get a reference id of its own.

replaying : Array[Int32]

The offsets of the replays above this one, so a cycle is caught rather than read for ever.

mut depth : Int32

How deep in the nesting this read is.

mut spent : Int32

How many bytes of variable-length payload have been declared so far.

Methods

end

fn Reader::end(Self) -> Unit raise

Refuse the bytes left over once a value has been read.

A message with two values in it is two messages, and reading one of them and ignoring the rest is how a truncation goes unnoticed.

enter

fn Reader::enter(Self) -> Unit raise

Go one level deeper, refusing a message nested past what was configured.

enter_ref

fn Reader::enter_ref(Self, Int32) -> Int32 raise

Go back to where the value of reference id was written and read it again, which is what a reference asks for when the value was not a container put aside. The answer is the offset to hand to leave_ref afterwards.

A reference that points into a value still being read is a cycle this package cannot rebuild — a MoonBit value is finished before it is held — so it is refused rather than read until the stack runs out.

is_done

fn Reader::is_done(Self) -> Bool

Whether every byte has been read.

leave

fn Reader::leave(Self) -> Unit

Come back one level.

leave_ref

fn Reader::leave_ref(Self, Int32) -> Unit

Come back from a replay to where the read was.

new

fn Reader::new(Bytes, config? : Config, schemas? : Array[StructMeta]) -> Self

A reader at the start of bytes, knowing the structs named in schemas.

A struct arrives as a user type ID and nothing else, so the schema it stands for has to be given to the reader before it is met.

node

fn Reader::node(Self, Int32) -> Value?

The container being built for a reference id, when there is one.

A cycle points at a value that is not finished, so the container is put here before its contents are read, and the reference finds it.

offset

fn Reader::offset(Self) -> Int32

Where the next byte is.

read_binary

fn Reader::read_binary(Self) -> Bytes raise

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

read_bool

fn Reader::read_bool(Self) -> Bool raise

A boolean: one byte, and everything but 1 is false.

read_byte

fn Reader::read_byte(Self) -> Int32 raise

One byte.

read_bytes

fn Reader::read_bytes(Self, Int32) -> Bytes raise

n bytes, as they are.

read_f32

fn Reader::read_f32(Self) -> Float raise

A single-precision float.

read_f64

fn Reader::read_f64(Self) -> Double raise

A double-precision float.

read_head

fn Reader::read_head(Self) -> Unit raise

The header byte, checked against what this reader was asked for.

A message that does not carry the cross-language flag is a message written by the native format, which is a different format.

read_i16

fn Reader::read_i16(Self) -> Int32 raise

Sixteen bits, signed, little endian.

read_i32

fn Reader::read_i32(Self) -> Int32 raise

Thirty-two bits, signed, little endian.

read_i64

fn Reader::read_i64(Self) -> Int64 raise

Sixty-four bits, signed, little endian.

read_i8

fn Reader::read_i8(Self) -> Int32 raise

Eight bits, read as signed, which is how the reference flags are written.

read_ref_flag

fn Reader::read_ref_flag(Self) -> RefFlag raise

The byte before a value, and what it says.

A reference to an id this message has not written is a refusal: there is nothing to point at.

read_string

fn Reader::read_string(Self) -> String raise

A string: the header says how many bytes and which of the three encodings they are.

read_tagged_i64

fn Reader::read_tagged_i64(Self) -> Int64 raise

A sixty-four-bit signed integer that is usually small: four bytes whose low bit says whether eight more follow.

read_tagged_u64

fn Reader::read_tagged_u64(Self) -> UInt64 raise

A sixty-four-bit unsigned integer that is usually small.

read_type_id

fn Reader::read_type_id(Self) -> TypeId raise

A type ID byte.

read_u16

fn Reader::read_u16(Self) -> Int32 raise

Sixteen bits, unsigned, little endian.

read_u32

fn Reader::read_u32(Self) -> UInt32 raise

Thirty-two bits, unsigned, little endian.

read_u64

fn Reader::read_u64(Self) -> UInt64 raise

Sixty-four bits, unsigned, little endian.

read_u8

fn Reader::read_u8(Self) -> Int32 raise

Eight bits, unsigned.

read_user_type

fn Reader::read_user_type(Self) -> (TypeId, Int32) raise

A type ID byte and the user type ID after it.

read_var_i32

fn Reader::read_var_i32(Self) -> Int32 raise

The same for thirty-two bits.

read_var_i64

fn Reader::read_var_i64(Self) -> Int64 raise

A zigzag-folded varint: the signed half of write_var_i64.

read_var_u32

fn Reader::read_var_u32(Self) -> UInt32 raise

A base-128 varint of thirty-two bits.

read_var_u36

fn Reader::read_var_u36(Self) -> UInt64 raise

The string header: one byte while it fits in seven bits, a varint when it does not.

read_var_u64

fn Reader::read_var_u64(Self) -> UInt64 raise

A base-128 varint.

ref_offset

fn Reader::ref_offset(Self, Int32) -> Int32 raise

Where the value of a reference id starts, so it can be read again.

remaining

fn Reader::remaining(Self) -> Int32

How many bytes are still unread.

schema

fn Reader::schema(Self, Int32) -> StructMeta?

The struct registered under a user type ID, when this reader knows it.

seek

fn Reader::seek(Self, Int32) -> Unit

Put the reader back at at.

set_node

fn Reader::set_node(Self, Int32, Value) -> Unit

Remember the container being built for a reference id.

spend

fn Reader::spend(Self, Int32) -> Unit raise

Count n bytes of declared payload against the budget for one message.

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