TextWriter interfacev0.1.0

Source
Somewhere formatted text can be written.
pub interface TextWriter {
    func Write(
        self: &var Self,
        bytes: char8[..]
    ) -> ! FormatError;
}

Writing advances the writer, so Write borrows its receiver mutably and is called through a &var TextWriter view, which refers to the caller's own writer rather than a copy that would lose its position.

Requirements

Write

func Write(
    self: &var Self,
    bytes: char8[..]
) -> ! FormatError

Writes bytes, which must be well-formed UTF-8 and must not end part-way through a character. A successful write accepted the complete run: there is no partial success to report, and a caller may write the next run without asking how much of this one landed. May return only InsufficientCapacity, TextFailure or WriterFailure. An implementation must not originate InvalidSpecification, UnsupportedRequest, ArgumentCountMismatch or ValueOutOfRange, none of which a destination is in a position to observe. Whether anything was written before a failure is the implementation's own documented guarantee: a bounded buffer leaves itself unchanged, while a stream may already have emitted a prefix.

NameTypeDescription
byteschar8[..]the UTF-8 run to write, whole

Returns: success when the complete run was accepted

Source

Implementations

TypeSummary
BufferWriterA fixed buffer being filled with text from the front.
StringBuilderA growable buffer of text.

WriteBytes

pub func WriteBytes(
    writer: &var TextWriter,
    bytes: char8[..]
) -> ! FormatError

Writes bytes to writer. The writer is borrowed exclusively for the call, so position changes remain visible to its owner. This forwards without inspecting the bytes, which is what keeps the writer's permitted failures exactly the writer's.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
byteschar8[..]the UTF-8 run to write, whole

Returns: whatever the writer reported

WriteAscii

pub func WriteAscii(
    writer: &var TextWriter,
    byte: char8
) -> ! FormatError

Writes one ASCII byte. Reports TextFailure(InvalidUtf8) for a byte above 0x7F: a single byte above that is never a whole character, and a writer takes whole characters. WriteScalar is what writes one that needs more than a byte. Otherwise reports whatever the writer reported.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
bytechar8the ASCII byte to write

Returns: success when the byte was accepted

WriteScalar

pub func WriteScalar(
    writer: &var TextWriter,
    scalar: char32
) -> ! FormatError

Writes one character, encoded as UTF-8. Reports TextFailure(InvalidUtf8) for a value that is not a Unicode scalar value. This is strict encoding, not presentation: a caller that wants an invalid value shown as U+FFFD replaces it before calling.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
scalarchar32the Unicode scalar value to encode and write

Returns: success when the encoded character was accepted