Protocol Buffers wire format in MoonBit: a zero-copy, allocation-free reader and a writer
moon add t-ujiie-g/pbf/// The `name` (field 1, a string) and the sum of `sizes` (field 2, packed varints) of a message.
fn read(bytes : Bytes) -> (String, Int)? {
let r = @pbf.Reader::new(bytes[:])
let mut name = ""
let mut sum = 0
while !r.at_end() {
let tag = r.read_tag()
if tag == @pbf.tag(1, @pbf.WIRE_LEN) {
name = r.read_string()
} else if tag == @pbf.tag(2, @pbf.WIRE_LEN) {
let token = r.enter()
while !r.at_end() {
sum += r.read_varint32()
}
r.exit(token)
} else {
r.skip(tag)
}
}
guard r.error() is None else { return None }
Some((name, sum))
}pub struct Reader {
// private fields
}pub struct Writer {
// private fields
}fn tag(field : Int, wire : Int) -> IntInstall
Download zipProtocol Buffers wire format in MoonBit: a zero-copy, allocation-free reader and a writer