pub unsafe trait IoBuf: 'static {
// Required methods
fn as_buf_ptr(&self) -> *const u8;
fn buf_len(&self) -> usize;
fn buf_capacity(&self) -> usize;
// Provided methods
fn as_slice(&self) -> &[u8] ⓘ { ... }
unsafe fn as_io_slice(&self) -> IoSlice { ... }
unsafe fn as_io_buffer(&self) -> IoBuffer { ... }
fn slice(self, range: impl RangeBounds<usize>) -> Slice<Self>
where Self: Sized { ... }
fn uninit(self) -> Uninit<Self>
where Self: Sized { ... }
fn filled(&self) -> bool { ... }
}
Expand description
A trait for buffers.
The IoBuf
trait is implemented by buffer types that can be passed to
compio operations. Users will not need to use this trait directly.
§Safety
The implementer should ensure the pointer, len and capacity are valid, so
that the returned slice of IoBuf::as_slice
is valid.
Required Methods§
Sourcefn as_buf_ptr(&self) -> *const u8
fn as_buf_ptr(&self) -> *const u8
Returns a raw pointer to the vector’s buffer.
This method is to be used by the compio
runtime and it is not
expected for users to call it directly.
Sourcefn buf_len(&self) -> usize
fn buf_len(&self) -> usize
Number of initialized bytes.
This method is to be used by the compio
runtime and it is not
expected for users to call it directly.
For Vec
, this is identical to len()
.
Sourcefn buf_capacity(&self) -> usize
fn buf_capacity(&self) -> usize
Total size of the buffer, including uninitialized memory, if any.
This method is to be used by the compio
runtime and it is not
expected for users to call it directly.
For Vec
, this is identical to capacity()
.
Provided Methods§
Sourceunsafe fn as_io_slice(&self) -> IoSlice
unsafe fn as_io_slice(&self) -> IoSlice
Sourceunsafe fn as_io_buffer(&self) -> IoBuffer
unsafe fn as_io_buffer(&self) -> IoBuffer
Sourcefn slice(self, range: impl RangeBounds<usize>) -> Slice<Self>where
Self: Sized,
fn slice(self, range: impl RangeBounds<usize>) -> Slice<Self>where
Self: Sized,
Returns a view of the buffer with the specified range.
This method is similar to Rust’s slicing (&buf[..]
), but takes
ownership of the buffer.
§Examples
use compio_buf::IoBuf;
let buf = b"hello world";
assert_eq!(buf.slice(6..).as_slice(), b"world");
Sourcefn uninit(self) -> Uninit<Self>where
Self: Sized,
fn uninit(self) -> Uninit<Self>where
Self: Sized,
Returns an Uninit
, which is a Slice
that only exposes
uninitialized bytes.
It will always point to uninitialized area of a IoBuf
even after
reading in some bytes, which is done by SetBufInit
. This is useful
for writing data into buffer without overwriting any existing bytes.
§Examples
Creating an uninit slice
use compio_buf::IoBuf;
let mut buf = Vec::from(b"hello world");
buf.reserve_exact(10);
let slice = buf.uninit();
assert_eq!(slice.as_slice(), b"");
assert_eq!(slice.buf_capacity(), 10);
Implementations on Foreign Types§
Source§impl IoBuf for BorrowedBuf<'static>
Available on crate feature read_buf
only.
impl IoBuf for BorrowedBuf<'static>
read_buf
only.