TextWriter interfacev0.1.0
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.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the UTF-8 run to write, whole |
Returns: success when the complete run was accepted
Implementations
| Type | Summary |
|---|---|
BufferWriter | A fixed buffer being filled with text from the front. |
StringBuilder | A growable buffer of text. |
Related
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.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
bytes | char8[..] | 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.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
byte | char8 | the 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.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
scalar | char32 | the Unicode scalar value to encode and write |
Returns: success when the encoded character was accepted