Skip to main content

IoBuf

Trait IoBuf 

pub unsafe trait IoBuf: Send + 'static {
    // Required method
    fn as_init(&self) -> &[u8] ;

    // Provided methods
    fn buf_len(&self) -> usize { ... }
    fn buf_ptr(&self) -> *const u8 { ... }
    fn slice(self, range: impl RangeBounds<usize>) -> Slice<Self>
       where Self: Sized { ... }
}
Expand description

An owned, stable byte buffer that can back an IO read or write.

§Safety

The backing storage must stay valid and at a fixed address for as long as a backend owns the buffer during an in-flight op: from when the backend reads buf_ptr / buf_mut_ptr until the op completes (the kernel may read/write it after the call returns). A heap buffer (Vec/BytesMut/Bytes) satisfies this trivially, moving the handle moves a header, not the allocation. An inline (stack) buffer (ArrayBuf) satisfies it too if: a completion backend moves the buffer into its op-slab before taking the pointer and does not move it again until the CQE, so the address the kernel sees stays fixed for the op.

Required Methods§

fn as_init(&self) -> &[u8]

The initialized (readable / already-written) bytes.

The core accessor: pointer and length both derive from it, so an impl is a one-liner and use-sites get a safe, bounded slice instead of raw ptr+len.

Provided Methods§

fn buf_len(&self) -> usize

Number of initialized bytes (as_init().len()).

fn buf_ptr(&self) -> *const u8

Raw const pointer to the start of the initialized region: for handing an address to a kernel/FFI sink (io_uring SEND_ZC, a TLS BIO). Most code should prefer as_init.

Warning: make sure to read SAFETY rules of IoBuf

fn slice(self, range: impl RangeBounds<usize>) -> Slice<Self>
where Self: Sized,

Restrict this buffer to the sub-range [begin, end), consuming it.

Used by writers to express “the not-yet-written tail” without copying. Recover the original buffer with Slice::into_inner.

The range must lie within the initialized region [0, buf_len), the resulting Slice reports its whole length as initialized, so slicing into the uninitialized spare would expose uninitialized bytes as readable. Panics if the range is out of bounds for buf_len.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

§

impl IoBuf for Bytes

§

impl IoBuf for BytesMut

§

impl IoBuf for Vec<u8>

§

impl<B> IoBuf for Slice<B>
where B: IoBuf,

§

impl<B> IoBuf for Uninit<B>
where B: IoBuf,

§

impl<const N: usize> IoBuf for ArrayBuf<N>