Writer¶
A buffer for building messages.
Writer
provides an way to create byte sequences using common
data encodings. The Writer
manages the underlying arrays and
sizes. It is useful for encoding data to send over a network or
store in a file. Once a message has been built you can call done()
to get the message's ByteSeq
s, and you can then reuse the
Writer
for creating a new message.
For example, suppose we have a TCP-based network data protocol where messages consist of the following:
message_length
- the number of bytes in the message as a big-endian 32-bit integerlist_size
- the number of items in the following list of items as a big-endian 32-bit integer- zero or more items of the following data:
- a big-endian 64-bit floating point number
- a string that starts with a big-endian 32-bit integer that specifies the length of the string, followed by a number of bytes that represent the string
A message would be something like this:
The following program uses a write buffer to encode an array of tuples as a message of this type:
use "buffered"
actor Main
new create(env: Env) =>
let wb = Writer
let messages = [[(F32(3597.82), "Anderson"); (F32(-7979.3), "Graham")]
[(F32(3.14159), "Hopper"); (F32(-83.83), "Jones")]]
for items in messages.values() do
wb.i32_be((items.size() / 2).i32())
for (f, s) in items.values() do
wb.f32_be(f)
wb.i32_be(s.size().i32())
wb.write(s.array())
end
let wb_msg = Writer
wb_msg.i32_be(wb.size().i32())
wb_msg.writev(wb.done())
env.out.writev(wb_msg.done())
end
Constructors¶
create¶
Returns¶
- Writer iso^
Public Functions¶
reserve_chunks¶
Reserve space for size' chunks.
This needs to be recalled after every call to done
as done
resets the chunks.
Parameters¶
- size': USize val
Returns¶
- None val
reserve_current¶
Reserve space for size bytes in _current
.
Parameters¶
- size': USize val
Returns¶
- None val
size¶
Returns¶
- USize val
u8¶
Write a byte to the buffer.
Parameters¶
- data: U8 val
Returns¶
- None val
u16_le¶
Write a U16 to the buffer in little-endian byte order.
Parameters¶
- data: U16 val
Returns¶
- None val
u16_be¶
Write a U16 to the buffer in big-endian byte order.
Parameters¶
- data: U16 val
Returns¶
- None val
i16_le¶
Write an I16 to the buffer in little-endian byte order.
Parameters¶
- data: I16 val
Returns¶
- None val
i16_be¶
Write an I16 to the buffer in big-endian byte order.
Parameters¶
- data: I16 val
Returns¶
- None val
u32_le¶
Write a U32 to the buffer in little-endian byte order.
Parameters¶
- data: U32 val
Returns¶
- None val
u32_be¶
Write a U32 to the buffer in big-endian byte order.
Parameters¶
- data: U32 val
Returns¶
- None val
i32_le¶
Write an I32 to the buffer in little-endian byte order.
Parameters¶
- data: I32 val
Returns¶
- None val
i32_be¶
Write an I32 to the buffer in big-endian byte order.
Parameters¶
- data: I32 val
Returns¶
- None val
f32_le¶
Write an F32 to the buffer in little-endian byte order.
Parameters¶
- data: F32 val
Returns¶
- None val
f32_be¶
Write an F32 to the buffer in big-endian byte order.
Parameters¶
- data: F32 val
Returns¶
- None val
u64_le¶
Write a U64 to the buffer in little-endian byte order.
Parameters¶
- data: U64 val
Returns¶
- None val
u64_be¶
Write a U64 to the buffer in big-endian byte order.
Parameters¶
- data: U64 val
Returns¶
- None val
i64_le¶
Write an I64 to the buffer in little-endian byte order.
Parameters¶
- data: I64 val
Returns¶
- None val
i64_be¶
Write an I64 to the buffer in big-endian byte order.
Parameters¶
- data: I64 val
Returns¶
- None val
f64_le¶
Write an F64 to the buffer in little-endian byte order.
Parameters¶
- data: F64 val
Returns¶
- None val
f64_be¶
Write an F64 to the buffer in big-endian byte order.
Parameters¶
- data: F64 val
Returns¶
- None val
u128_le¶
Write a U128 to the buffer in little-endian byte order.
Parameters¶
- data: U128 val
Returns¶
- None val
u128_be¶
Write a U128 to the buffer in big-endian byte order.
Parameters¶
- data: U128 val
Returns¶
- None val
i128_le¶
Write an I128 to the buffer in little-endian byte order.
Parameters¶
- data: I128 val
Returns¶
- None val
i128_be¶
Write an I128 to the buffer in big-endian byte order.
Parameters¶
- data: I128 val
Returns¶
- None val
write¶
Write a ByteSeq to the buffer.
Parameters¶
Returns¶
- None val
writev¶
Write ByteSeqs to the buffer.
Parameters¶
- data: ByteSeqIter val
Returns¶
- None val
done¶
Return an array of buffered ByteSeqs and reset the Writer's buffer.