std.bytes
Safe helpers around the compiler-known ByteBuf storage.
std.bytes is a small set of safe helpers over ByteBuf, the compiler-known owned byte buffer. ByteBuf itself has a method surface for the common in-place operations; std.bytes adds construction and bulk-copy helpers that read more clearly at the call site.
The ByteBuf type
Section titled “The ByteBuf type”ByteBuf is an owned, growable byte buffer with deterministic drop. It is built in to the compiler (not declared in source) and exposes:
ByteBuf.new() -> ByteBufb.len # -> usizeb.push(byte: u8) -> Noneb.push_bytes(src: view [u8]) -> Noneb.reserve(additional: usize) -> Noneb.clear() -> Noneb.truncate(len: usize) -> Noneb.as_slice() -> view [u8]b.as_mut_slice() -> mut view [u8]Structural mutations (push, push_bytes, reserve, clear, truncate) may reallocate, so they conflict with any live borrowed view of the same buffer until that view’s last use.
Helpers
Section titled “Helpers”import std.bytes as bytes
export def with_capacity(capacity: usize) -> ByteBufexport def filled(len: usize, value: u8) -> ByteBufexport def zeroed(len: usize) -> ByteBufexport def from_slice(src: view [u8]) -> ByteBufexport def append(dst: mut view ByteBuf, src: view [u8]) -> Noneexport def clear(buf: mut view ByteBuf) -> Noneexport def truncate(buf: mut view ByteBuf, len: usize) -> Noneexport def resize_zeroed(buf: mut view ByteBuf, len: usize) -> Noneexport def copy_from(dst: mut view [u8], src: view [u8]) -> usizewith_capacity(n): an empty buffer that has reserved room fornbytes.filled(len, value)/zeroed(len): a buffer oflenbytes set tovalue(or0).from_slice(src): a fresh buffer holding a copy ofsrc.append(dst, src): appendsrctodst(a thin wrapper overpush_bytes).clear/truncate: drop the tail while retaining capacity.resize_zeroed(buf, len): grow with zero bytes or truncate so the buffer is exactlylenlong.copy_from(dst, src): copymin(dst.len, src.len)bytes intodstand return how many were copied. Neither side reallocates, so this works on any mutable byte slice, not justByteBuf.
Example
Section titled “Example”import std.bytes as bytesimport std.io as io
def main() -> None: var buf: ByteBuf = bytes.with_capacity(16) bytes.append(buf, b"GET / HTTP/1.1\r\n") bytes.append(buf, b"\r\n") io.stdout_write_bytes(buf.as_slice()) return None