Buffer

public struct Buffer { /* private fields */ }

Writable that appends bytes to a growable Array[UInt8] — the in-memory counterpart to writing to a file. Useful for capturing output, building byte sequences before flushing to a real sink, or testing formatters.

Buffer clones share the underlying COW array.

Examples

var b = Buffer(); try writeString(b, "Hello, "); try writeString(b, "World!"); b.toString() // "Hello, World!"

Representation

One Array[UInt8] field; capacity grows on demand.

Properties

public var count: Int64 { get }

Bytes currently held.

var data: Array[UInt8]
public var isEmpty: Bool { get }

true when no bytes have been written.

Initializers

public init()

Builds an empty buffer.

public init(Int64)

Builds an empty buffer pre-sized to capacity bytes. Use when the approximate final size is known to skip intermediate growth.

Methods

public func asSlice() -> ArraySlice[UInt8]

Returns a non-owning slice view over the buffered bytes. The slice dangles once the buffer is mutated again — copy via toArray if you need to outlive the next write.

public mutating func clear()

Drops every byte but keeps the allocated capacity for reuse.

public func toArray() -> Array[UInt8]

Returns an owned copy of the buffered bytes.

public func toString() -> String

Interprets the buffered bytes as UTF-8 and returns the resulting String. Behaviour for invalid UTF-8 is currently undefined — validate upstream if untrusted bytes are involved.

ImplementsWritable

Methods

public mutating func flush() -> Result[(), IoError]

No-op; bytes are already "in" the buffer.

public mutating func write(from: ArraySlice[UInt8]) -> Result[Int64, IoError]

Appends every byte from buf. Always succeeds with .Ok(buf.count).

ImplementsCloneable

Methods

public func clone() -> Buffer

Deep-clones the underlying byte array.

Defined in lang/std/io/write.ks