struct Buffer! { ... }
Growable byte buffer with an explicit owner and initialized length.
Restricted: no other module can write the literal, which would otherwise let it wrap any pointer (say, one read off a live Slice) and destroy it -- an arbitrary free with no unsafe. Adopting storage requires an owned Uninit! and an explicit claim about which bytes are initialized.
Methods
method savesource
forall 'k, C, R where Allocate[C -> 'k, R].
fn save(self, &cx: C) -> Str['k] { ... }
Defined in alloc::arena.
The buffer's bytes copied into the context's arena in one block of exactly their length, as text that lives as long as the arena; the buffer is freed. Buffer!::new().put("x").put(n).save(&cx).
method from_initialized_allocationsource
unsafe fn from_initialized_allocation(storage: Uninit!, len: usize) -> Buffer! { ... }
Takes ownership of an allocation whose first len bytes are initialized. The allocation's recorded extent becomes the buffer's capacity. Aborts if len exceeds that extent.
Caller: storage records the allocation's capacity, and its prefix of len bytes has already been written.
method try_with_capacitysource
fn try_with_capacity(cap: usize) -> Result[Buffer!, AllocError] { ... }
A new, empty buffer with room for cap bytes before the first grow, or the allocation failure. Growing it later aborts as reserve does.
method with_capacitysource
fn with_capacity(cap: usize) -> Buffer! { ... }
A new, empty buffer with room for cap bytes, aborting when the allocation fails.
try_with_capacity, aborting on failure. new is this with a starting capacity of 64.
method newsource
fn new() -> Buffer! { ... }
A new, empty buffer with room for 64 bytes.
method new_zeroedsource
fn new_zeroed(n: usize) -> Buffer! { ... }
A buffer of exactly n zero bytes: len == cap == n, every byte initialized, so it is safe and carries no obligation.
For a caller that wants a fixed-size buffer whose every index is addressable after open (so get_ref admits it), and then writes through the brand. Zeroing costs one pass over n bytes and means no uninitialized byte is ever inside len.
method reservesource
fn reserve(self, extra: usize) -> Buffer! { ... }
Grows (doubling) until at least extra more bytes fit; a no-op otherwise. Aborts if the size overflows usize. Room reserved this way is what tail_ptr hands out, for a caller that writes into it directly (a system call filling the buffer in place) and commits.
method truncatesource
fn truncate(self, len: usize) -> Buffer! { ... }
The first len bytes, or all of them when there are fewer; the room stays for what is written next.
method clearsource
fn clear(self) -> Buffer! { ... }
No bytes, with the room kept.
method clonesource
fn clone(&self) -> Buffer! { ... }
A new buffer holding the same bytes; self comes back unchanged.
method lensource
fn len(&self) -> usize { ... }
Number of initialized bytes.
&self: self is linear, so even a query hands the buffer back first, as &b.len() at the call site.
method remaining_capacitysource
fn remaining_capacity(&self) -> usize { ... }
How many more bytes fit before the next grow: cap - len.
method as_ptrsource
unsafe fn as_ptr(&self) -> *u8 { ... }
Address of the first byte, valid until the next grow. Caller: uses it only for the buffer's first len bytes, and not after the next grow or destroy.
method tail_ptrsource
unsafe fn tail_ptr(&self) -> *u8 { ... }
Address of the first unused byte, writable up to the room reserve most recently guaranteed. Pairs with commit. The pointer is valid only until the next grow. Takes &self because a linear buffer must survive the call so the caller can commit afterwards. Caller: writes no further than the room reserve guaranteed, and stops using the pointer at the next grow or destroy.
method commitsource
unsafe fn commit(self, n: usize) -> Buffer! { ... }
Advances len by n without writing anything. Pairs with reserve/tail_ptr.
Capacity is checked: if the new length would exceed cap, this aborts. Without the check, open would hand out a Slice longer than the block, and Slice::get_ref's bounds check (against the slice's length) would admit out-of-bounds indices.
Caller: all n bytes at p[len .. len+n) have been written since the last reserve/grow, so the new len describes initialized storage; initialization cannot be checked.
A caller that only wants a fixed-size, fully addressable buffer should use Buffer!::new_zeroed, which carries no obligation.
method destroysource
fn destroy(self) -> () { ... }
Frees the buffer. Every Buffer! must reach this, or be handed on, before its function returns.
method opensource
fn open(self) -> exists '1. (Exclusive!['1], Slice[u8, '1], BufferRestore!['1]) { ... }
A branded view of the buffer's bytes, with its capability and the record that restores the buffer.
Mints a fresh brand over this buffer's storage and returns a Slice[u8,'1] to read and write through, plus BufferRestore!, the only way back to a growable Buffer!. The address the way back uses is captured here from self, never from the Slice (which Slice::sub can offset).
Allocation!::adopt keeps the base in a linear, restricted, unprojectable Allocation![u8,'1]; Region!::share hands out the Exclusive!/Ref access pair plus the RegionResume![u8,'1] receipt that restore needs.
Uses adopt_raw, not adopt_raw_sized(p, c): the allocation record holds extent 0usize ("not recorded") and BufferRestore! carries cap itself. Nothing reads the record's extent: restore discards it via into_raw.
Instances
instance Writer[Buffer!]source
instance Writer[Buffer!] { ... }
In alloc::buffer.
A buffer as a writer: every write appends, growing first if needed.
Infallible: Buffer! aborts on out-of-memory.
instance Hash[Buffer!]source
instance Hash[Buffer!] { ... }
In alloc::buffer.
Hashes the buffer's bytes in order.
A buffer hashes and compares by its contents, so it can key a map.
instance Eq[Buffer!, Buffer!]source
instance Eq[Buffer!, Buffer!] { ... }
In alloc::buffer.
Byte equality of two buffers.
instance Eq[Buffer!, Readable[C, S]]source
forall C, S where ReadBytes[C, S], Copy[S].
instance Eq[Buffer!, Readable[C, S]] { ... }
In alloc::buffer.
A buffer equals a borrowed view with the same bytes, which lets a map keyed by Buffer! be searched with a view (HashMap!::find). The two Hash instances agree: both hash the bytes in order.
instance Ord[Buffer!, Buffer!]source
instance Ord[Buffer!, Buffer!] { ... }
In alloc::buffer.
Buffers order by their bytes, as unsigned values, a shorter prefix first.
instance Format[Buffer!, W]source
forall W where Writer[W].
instance Format[Buffer!, W] { ... }
In alloc::fmt.
Formats an owned buffer as its contents. The buffer is handed back, not freed: an f-string formats a linear value only through {&name}.
instance Printable[Buffer!, W]source
forall W where Writer[W].
instance Printable[Buffer!, W] { ... }
In alloc::fmt.
A buffer prints as its contents, and is freed.