Buffer
public struct Buffer[T, A]: not Copyable where A: Allocator { /* private fields */ }Owning, allocator-parameterised contiguous storage.
Buffer is the building block underneath Array, String, and any
other COW/growable collection. It owns its allocation, deallocates on
drop, and is not Copyable to keep ownership unique. For a non-owning
view see Slice; for a refcounted owning wrapper see RcBox.
Examples
var buf = Buffer[Int64, SystemAllocator](capacity: 4, allocator: SystemAllocator());
buf.write(at: 0, value: 10);
buf.write(at: 1, value: 20);
buf.read(at: 0) // .Some(10)
buf.resize(to: 8); // grow in place if possible
Representation
A Pointer[T] to the storage, an Int64 capacity, and the allocator
instance. The buffer's contents are not initialised on construction —
reading an uninitialised slot is undefined behavior.
Memory Model
Owning, unique. The deinit reclaims storage via the same allocator.
Marked not Copyable so an accidental let b2 = b1 is rejected at
compile time; use a higher-level COW wrapper (e.g. via RcBox) for
shared semantics.
Properties
public var capacity: Int64 { get }
public var capacity: Int64 { get }Number of element slots — not the count of initialised elements.
public var pointer: Pointer[T] { get }
public var pointer: Pointer[T] { get }Pointer to the first slot.
Initializers
public init(Int64, A)
public init(Int64, A)Allocates a buffer holding capacity elements. Storage is
uninitialised; the caller is responsible for writing valid Ts
before reading them.
Errors
Panics with "Buffer allocation failed" if allocator.allocate
returns .None.
Methods
public func asSlice() -> ArraySlice[T]
public func asSlice() -> ArraySlice[T]Returns a ArraySlice[T] over the entire buffer. The slice does not
extend the buffer's lifetime; callers must keep the buffer alive
for as long as they use the slice.
public func read(unchecked: Int64) -> T
public func read(unchecked: Int64) -> TReads slot index without bounds checking.
Safety
index must satisfy 0 <= index < capacity, and the slot must
already hold an initialised T. Out-of-range or uninitialised
reads are undefined behavior.
public func read(at: Int64) -> T?
public func read(at: Int64) -> T?Reads slot index, returning .None when out of range. As with
the unchecked form, the slot must already hold an initialised T.
public mutating func resize(to: Int64)
public mutating func resize(to: Int64)Grows or shrinks the storage to hold newCapacity elements via
the allocator's reallocate. On success, existing initialised
elements are preserved up to the smaller of the two capacities;
the new pointer becomes the buffer's storage.
Errors
Panics with "Buffer resize failed" if reallocate returns
.None (the original allocation is left intact, but the panic
aborts).
public func slice(from: Int64, to: Int64) -> ArraySlice[T]?
public func slice(from: Int64, to: Int64) -> ArraySlice[T]?Returns a slice over [start, end), or .None when the range
falls outside [0, capacity]. As with asSlice, the slice
borrows from the buffer.
public func write(unchecked: Int64, T)
public func write(unchecked: Int64, T)Writes value into slot index without bounds checking.
Safety
Same precondition as read(unchecked:) — 0 <= index < capacity.
public func write(at: Int64, T) -> Bool
public func write(at: Int64, T) -> BoolWrites value to slot index. Returns false (and does
nothing) when out of range.
Defined in lang/std/memory/buffer.ks