Skip to main content

WriteBuf

Trait WriteBuf 

Source
pub unsafe trait WriteBuf {
    // Required methods
    fn as_slice(&self) -> &[u8] ⓘ;
    fn capacity(&self) -> usize;
    fn as_mut_ptr(&mut self) -> *mut u8;
    unsafe fn filled_until(&mut self, n: usize);

    // Provided method
    unsafe fn write_from<F>(&mut self, f: F) -> SafeResult
       where F: FnOnce(*mut c_void, usize) -> SafeResult { ... }
}
Expand description

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_capacity to create an empty Vec with 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.

§Safety

An implementation has to keep three promises, because the zstd functions this is handed to write straight through the pointer:

  • as_mut_ptr() points at a buffer that is valid to write capacity() bytes to.
  • as_slice() covers only initialized bytes - never any of the capacity that has not been written yet.
  • filled_until(n) is what marks the first n bytes as initialized, so after it returns, as_slice() must cover them.

Required Methods§

Source

fn as_slice(&self) -> &[u8] ⓘ

Returns the valid data part of this container. Should only cover initialized data.

Source

fn capacity(&self) -> usize

Returns the full capacity of this container. May include uninitialized data.

Source

fn as_mut_ptr(&mut self) -> *mut u8

Returns a pointer to the start of the data.

Source

unsafe fn filled_until(&mut self, n: usize)

Indicates that the first n bytes of the container have been written.

§Safety

The first n bytes of this buffer must actually have been initialized.

Provided Methods§

Source

unsafe fn write_from<F>(&mut self, f: F) -> SafeResult

Call the given closure using the pointer and capacity from self.

The closure returns a parseable code which, if valid, is how many bytes it wrote to self.

§Safety

The closure must treat its first argument as pointing to potentially uninitialized memory, and must not read from it. It must have written at least n bytes contiguously from that pointer, where n is the value it returns.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl WriteBuf for &mut Vec<u8>

Available on crate feature std only.
Source§

fn as_slice(&self) -> &[u8] ⓘ

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn filled_until(&mut self, n: usize)

Source§

impl WriteBuf for Vec<u8>

Available on crate feature std only.
Source§

fn as_slice(&self) -> &[u8] ⓘ

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn filled_until(&mut self, n: usize)

Source§

impl WriteBuf for [u8]

Source§

fn as_slice(&self) -> &[u8] ⓘ

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn filled_until(&mut self, _n: usize)

Source§

impl<T> WriteBuf for Cursor<T>
where T: WriteBuf,

Available on crate feature std only.
Source§

fn as_slice(&self) -> &[u8] ⓘ

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn filled_until(&mut self, n: usize)

Source§

impl<const N: usize> WriteBuf for [u8; N]

Available on crate feature arrays only.
Source§

fn as_slice(&self) -> &[u8] ⓘ

Source§

fn capacity(&self) -> usize

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

unsafe fn filled_until(&mut self, _n: usize)

Implementors§