The streaming contract of byte-stream codecs: step through input chunks into bounded output slices, with bounded memory.
moon add gaato/codec_bytes///|
/// Decodes what `read` hands over, `None` once it has no more, with any
/// decoder, passing each piece of output to `write`.
fn[D : @codec_bytes.Decode] decode_all(
decoder : D,
read : () -> Bytes?,
write : (Bytes) -> Unit,
) -> Unit raise {
let out = FixedArray::make(64 * 1024, b'\x00')
for ;; {
let piece = read()
let end = piece is None
let mut input = piece.unwrap_or(b"")[:]
for ;; {
let status = decoder.step(input, out.mut_view(), end~)
input = input[decoder.last_consumed():]
write(Bytes::from_array(out[:decoder.last_produced()]))
match status {
Done => return
NeedMoreInput => break
NeedMoreOutput => ()
}
}
}
}
///|
/// A source that hands over `pieces` one by one, then `None`.
fn pieces_of(pieces : Array[Bytes]) -> () -> Bytes? {
let mut next = 0
() => {
if next < pieces.length() {
next += 1
Some(pieces[next - 1])
} else {
None
}
}
}///|
/// A decoder of the identity format: output is input.
struct Identity {
mut taken : Int32
mut written : Int32
}
///|
impl @codec_bytes.Decode for Identity with fn step(self, input, output, end~) {
let n = input.length().min(output.length())
for i in 0..<n {
output[i] = input[i]
}
self.taken = n
self.written = n
if n < input.length() {
NeedMoreOutput
} else if end {
Done
} else {
NeedMoreInput
}
}
///|
impl @codec_bytes.Decode for Identity with fn last_consumed(self) {
self.taken
}
///|
impl @codec_bytes.Decode for Identity with fn last_produced(self) {
self.written
}
///|
impl @codec_bytes.Decode for Identity with fn reset(self) {
self.taken = 0
self.written = 0
}
///|
impl @codec_bytes.Decode for Identity with fn memory_usage(_self) {
0
}
///|
test "drive a decoder through the contract" {
let decoder : Identity = { taken: 0, written: 0, }
let decoded = Buffer()
decode_all(decoder, pieces_of([b"hel", b"lo"]), bytes => {
decoded.write_bytes(bytes)
})
assert_eq(decoded.to_bytes(), b"hello")
}| Action | Use it for |
|---|---|
| Continue | Ordinary input. The encoder writes output when it has enough to write. |
| Flush | Making everything so far decodable while the stream stays open, for example to send one part of an HTTP response now. LZW and .Z cannot flush and raise Unsupported. |
| Finish | The last input. Keep calling until the step returns Done, so that the trailer is written. |
///|
/// Compresses what `read` hands over, `None` once it has no more, with any
/// encoder, passing each piece of output to `write`. An empty source still
/// finishes the stream, so its trailer is written.
pub fn[E : @codec_bytes.Encode] encode_all(
encoder : E,
read : () -> Bytes?,
write : (Bytes) -> Unit,
) -> Unit raise {
let out = FixedArray::make(64 * 1024, b'\x00')
for ;; {
let piece = read()
let action : @codec_bytes.EncodeAction = if piece is None {
Finish
} else {
Continue
}
let mut input = piece.unwrap_or(b"")[:]
for ;; {
let status = encoder.step(input, out.mut_view(), action~)
input = input[encoder.last_consumed():]
write(Bytes::from_array(out[:encoder.last_produced()]))
match status {
Done => return
NeedMoreInput => break
NeedMoreOutput => ()
}
}
}
}pub(open) trait Decode {
fn step(Self, BytesView, MutArrayView[Byte], end~ : Bool) -> Status raise CodecError
fn last_consumed(Self) -> Int
fn last_produced(Self) -> Int
fn reset(Self) -> Unit
fn memory_usage(Self) -> Int
}pub(open) trait Encode {
fn step(Self, BytesView, MutArrayView[Byte], action~ : EncodeAction) -> Status raise CodecError
fn last_consumed(Self) -> Int
fn last_produced(Self) -> Int
fn reset(Self) -> Unit
fn memory_usage(Self) -> Int
}Install
Download zipThe streaming contract of byte-stream codecs: step through input chunks into bounded output slices, with bounded memory.