Trait WriteBuf
unsafe trait WriteBuf
Describe a bytes container, like Vec<u8>.
Represents a contiguous segment of allocated memory, a prefix of which is initialized.
It allows starting from an uninitializes chunk of memory and writing to it, progressively initializing it. No re-allocation typically occur after the initial creation.
The main implementors are:
-
Vec<u8>and similar structures. These hold both a length (initialized data) and a capacity (allocated memory).Use
Vec::with_capacityto create an emptyVecwith non-zero capacity, and the length field will be updated to cover the data written to it (as long as it fits in the given capacity). -
[u8]and[u8; N]. These must start already-initialized, and will not be resized. It will be up to the caller to only use the part that was written (as returned by the various writing operations). -
std::io::Cursor<T: WriteBuf>. This will ignore data before the cursor's position, and append data after that.
Required Methods
fn as_slice(self: &Self) -> &[u8]Returns the valid data part of this container. Should only cover initialized data.
fn capacity(self: &Self) -> usizeReturns the full capacity of this container. May include uninitialized data.
fn as_mut_ptr(self: &mut Self) -> *mut u8Returns a pointer to the start of the data.
unsafe fn filled_until(self: &mut Self, n: usize)Indicates that the first
nbytes of the container have been written.Safety: this should only be called if the
nfirst bytes of this buffer have actually been initialized.
Provided Methods
unsafe fn write_from<F>(self: &mut Self, f: F) -> SafeResult where F: FnOnce(*mut c_void, usize) -> SafeResultCall the given closure using the pointer and capacity from
self.Assumes the given function returns a parseable code, which if valid, represents how many bytes were written to
self.The given closure must treat its first argument as pointing to potentially uninitialized memory, and should not read from it.
In addition, it must have written at least
nbytes contiguously from this pointer, wherenis the returned value.