silk.collections.bytes.
Owned byte buffers for file contents, process output, and other data with no fixed encoding.
When to use#
Use Bytes when each byte must stay unchanged and the data is not necessarily text. Use
silk.text.string.String when the data is valid UTF-8 and the code needs text operations. Use
silk.collections.list.List<u8> when the code appends many single bytes.
Details#
Bytes.make does not allocate. Bytes.makeWithCapacity, Bytes.zeroed, Bytes.copy,
Bytes.append, Bytes.reserve, and Bytes.reserveExact can allocate and need an
Allocator. Bytes.clear and Bytes.truncate keep the storage for reuse, and
Bytes.clearAndFree releases it. Bytes.asSlice and Bytes.asMutSlice borrow the bytes.
Dropping a Bytes value releases its storage.
Examples#
Copy, append, and change bytes#
import silk.collections.bytes { Bytes }
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
pub effect fn main() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let mut bytes = run Bytes.copy(b"AB") |> Effect.provideMut<Allocator>(&mut allocator)
run Bytes.append(&mut bytes, b"C") |> Effect.provideMut<Allocator>(&mut allocator)
let mut writable = Bytes.asMutSlice(&mut bytes)
writable[1] = 48
drop writable
// The bytes are now "A0C".
let readable = Bytes.asSlice(&bytes)
if readable.length != 3 || readable[1] != 48 || readable[2] != 67 {
return 1
}
return 0
}Import as Bytes with import silk.collections.bytes { Bytes }.
Public declarations: 1.
Bytes#
pub struct BytesAn owned sequence of bytes. Dropping it releases its storage.
Details#
The length is the number of bytes in the value. The length grows with Bytes.append. The
capacity is the number of bytes that fit before the next allocation. Bytes.clear and
Bytes.truncate keep the capacity, so a buffer cleared and filled again in a loop reuses its
storage. Bytes.clearAndFree releases the storage.
Implementation Debug for Bytes#
impl Debug for BytesOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites each byte as a number, as [104, 105], like a &[u8].
Implementation Bytes#
impl BytesAssociated function make#
pub fn make() -> BytesReturns an empty Bytes value. This function does not allocate.
Associated function makeWithCapacity#
pub effect fn makeWithCapacity(capacity: usize) -> Bytes ! OutOfMemoryError ? &mut AllocatorReturns an empty Bytes value with storage for exactly capacity bytes.
When to use#
Use makeWithCapacity when the number of bytes is known or can be estimated before the
appends, so that the value allocates once.
Details#
The length is 0 and the capacity is capacity. A capacity of 0 does not allocate.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the storage.
Associated function zeroed#
pub effect fn zeroed(length: usize) -> Bytes ! OutOfMemoryError ? &mut AllocatorReturns a Bytes value of length zero bytes.
When to use#
Use zeroed to make a buffer that code then fills through Bytes.asMutSlice, such as the
destination of a read.
Details#
A length of 0 returns an empty value and does not allocate.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the storage.
Method appendWithinCapacity#
pub fn appendWithinCapacity(self: &mut Self, value: u8) -> boolAdds value at the end if the capacity has room, and returns true. Returns false and
changes nothing when the capacity is full. It never allocates.
Method appendSliceWithinCapacity#
pub fn appendSliceWithinCapacity(self: &mut Self, values: &[u8]) -> boolAdds a copy of values at the end if the capacity has room for all of them, and returns
true. Returns false and changes nothing otherwise. It never allocates.
Method length#
pub fn length(self: &Self) -> usizeReturns the number of bytes.
Associated function copy#
pub effect fn copy(values: &[u8]) -> Bytes ! OutOfMemoryError ? &mut AllocatorReturns a new Bytes value that holds a copy of values.
Details#
An empty values does not allocate. Otherwise, the function makes one allocation of exactly
values.length bytes.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the storage.
Method reserveExact#
pub effect fn reserveExact(self: &mut Self, additional: usize) -> ()
! OutOfMemoryError
? &mut AllocatorMakes sure that at least additional more bytes fit without another allocation. The length
does not change.
Details#
If the capacity is already sufficient, this function does nothing. Otherwise, it makes one
allocation for exactly length + additional bytes.
Errors#
OutOfMemoryErroriflength + additionaloverflowsusize, or theAllocatorcannot supply the storage. The value does not change.
Method reserve#
pub effect fn reserve(self: &mut Self, additional: usize) -> ()
! OutOfMemoryError
? &mut AllocatorMakes sure that at least additional more bytes fit without another allocation, with room
to spare for later appends. The length does not change.
When to use#
Use reserve before a run of appends whose total is not known. Use Bytes.reserveExact
when the final length is known.
Details#
If the capacity is already sufficient, this function does nothing. Otherwise, it grows the capacity by doubling until the bytes fit.
Errors#
OutOfMemoryErroriflength + additionaloverflowsusize, or theAllocatorcannot supply the storage. The value does not change.
Method shrinkToFit#
pub effect fn shrinkToFit(self: &mut Self) -> () ! OutOfMemoryError ? &mut AllocatorReleases the capacity beyond the length.
Details#
If the capacity equals the length, this function does nothing. If the value is empty, it releases the storage. Otherwise, it moves the bytes into storage for exactly the length.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the new storage. The value does not change. An empty value never fails.
Method capacity#
pub fn capacity(self: &Self) -> usizeReturns the number of bytes the storage holds before the next append allocates.
Method storage#
pub unsafe fn storage(self: &mut Self) -> ?[*]mut u8Returns a pointer to the first byte of the storage, which has room for Bytes.capacity
bytes.
When to use#
Use it with Bytes.setLength in code that fills many bytes with no check for each, such as
a decoder: reserve the room, write the bytes through the pointer, then set the length.
Gotchas#
The pointer is null while the capacity is 0. Any call that grows, shrinks or frees the storage
makes the pointer stale. Only the first Bytes.length bytes are initialized.
Method setLength#
pub unsafe fn setLength(self: &mut Self, length: usize) -> ()Sets the number of bytes to length without writing any byte.
When to use#
Use it after code wrote bytes through Bytes.storage, to make them part of the value.
Gotchas#
The caller must prove that length is at most Bytes.capacity and that the first length
bytes of the storage are initialized.
Method clear#
pub fn clear(self: &mut Self) -> ()Removes all bytes. The storage stays allocated for reuse.
Details#
Later appends up to the old capacity do not allocate. Use Bytes.clearAndFree to also
release the storage.
Method clearAndFree#
pub fn clearAndFree(self: &mut Self) -> ()Removes all bytes and releases the storage. The length and the capacity become 0.
Details#
This function does not allocate and does not need an Allocator. Use Bytes.clear to keep
the storage for reuse.
Method truncate#
pub fn truncate(self: &mut Self, length: usize) -> ()Removes the bytes from position length to the end. The storage stays allocated for reuse.
Details#
If length is more than or equal to the current length, this function does nothing.
Method append#
pub effect fn append(self: &mut Self, values: &[u8]) -> () ! OutOfMemoryError ? &mut AllocatorAdds a copy of values at the end, in order.
Errors#
OutOfMemoryErrorif theAllocatorcannot supply the storage. The value does not change.
Method asSlice#
pub fn asSlice(self: &Self) -> &[u8]Returns a shared borrow of all bytes as a slice.
Method asMutSlice#
pub fn asMutSlice(self: &mut Self) -> &mut [u8]Returns an exclusive borrow of all bytes as a slice.
Details#
Use the slice to change bytes in place. The slice cannot change the length. The compiler
rejects other uses of the value, such as Bytes.append, while the slice is live.
Category: Collections.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.