API · struct
Reader
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
| Field | Meaning |
|---|---|
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.
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.
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_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_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_user_type
fn Reader::read_user_type(Self) -> (TypeId, Int32) raise
A type ID byte and the user type ID after it.
read_var_i64
fn Reader::read_var_i64(Self) -> Int64 raise
A zigzag-folded varint: the signed half of write_var_i64.
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.
ref_offset
fn Reader::ref_offset(Self, Int32) -> Int32 raise
Where the value of a reference id starts, so it can be read again.
schema
fn Reader::schema(Self, Int32) -> StructMeta?
The struct registered under a user type ID, when this reader knows it.
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.