APIWriter

Writer

Buffered byte writers.

A Writer e accepts bytes through operations that perform exactly e. over buffers writes to a Sink, and collecting gathers them in memory. Both are scoped runners: the writer cannot leave their callback.

Writer

type Writer e =    { emit : Bytes ->{e} ()    , flush : () ->{e} ()    }Source

A byte writer whose operations perform e. Its fields are operations, called by projection, as writer.flush(); they are not module functions.

Fields

emit

emit : Bytes ->{e} ()Source

Accepts bytes for eventual writing.

flush

flush : () ->{e} ()Source

Pushes everything accepted so far.

collecting

collecting : (Writer {#s} ->{#s} a) ->{e} (a, Bytes)Source

Runs action with a writer that keeps everything written in memory, and answers the result with those bytes, once action completes. Its flush does nothing. With no other effects in the callback, the call is pure.

import Bytes
import Writer

written = Writer.collecting { writer -> Writer.writeString writer "hi" }

written == ((), Bytes.fromString "hi")

copy

copy : Reader e -> Writer e ->{e} ()Source

Writes everything left in reader, chunk by chunk.

import Bytes
import Reader
import Tuple
import Writer

copied = Reader.withBytes (Bytes.fromString "abc") { reader -> Writer.collecting { writer -> Writer.copy reader writer } }

Tuple.second copied == Bytes.fromString "abc"

over

over : Sink e -> Int -> (Writer {#s} ->{#s} a) ->{e} aSource

Runs action with a writer that buffers into windows of size bytes, writing to sink whenever the buffer reaches the window and once more when action returns. An emit is never split, so a chunk larger than the window is one write, and a window below one byte writes every emit straight through. A failed action writes nothing further, so no reader sees a truncated message it would have to guess at.

write

write : Writer e -> Bytes ->{e} ()Source

Emits the bytes.

writeString

writeString : Writer e -> String ->{e} ()Source

Emits the text’s UTF-8 bytes.

import Bytes
import Tuple
import Writer

written =
    Writer.collecting { writer ->
        Writer.writeString writer "é"
        Writer.write writer (Bytes.fromList [33])
    }

Bytes.toList (Tuple.second written) == [195, 169, 33]