pub struct OwnedBuf { /* private fields */ }
Expand description
An owned, fixed length buffer of bytes.
An OwnedBuf
is created from an existing collection type, it is an abstract
wrapper of sorts. See the module docs for details and examples.
This type is a sort of “double cursor”. It tracks three regions in the buffer: a region at the beginning of the buffer that has been logically filled with data, a region that has been initialized at some point but not yet logically filled, and a region at the end that is fully uninitialized. The filled region is guaranteed to be a subset of the initialized region. The filled part can grow or shrink, the initialized part can only grow, and capacity of the buffer (i.e., its total length) is fixed.
In summary, the contents of the buffer can be visualized as:
[ capacity ]
[ filled | unfilled ]
[ initialized | uninitialized ]
The API of OwnedBuf
is focussed on creation and destruction. To read from the filled part,
get a view of the buffer using an OwnedSlice
. To write into the unfilled part, get a view of
the buffer using and OwnedCursor
. Both of these types can be converted back into an OwnedBuf
.
Implementations§
Source§impl OwnedBuf
impl OwnedBuf
Sourcepub unsafe fn new(
data: *mut MaybeUninit<u8>,
dtor: unsafe fn(&mut OwnedBuf),
user_data: *const (),
capacity: usize,
filled: usize,
init: usize,
) -> OwnedBuf
pub unsafe fn new( data: *mut MaybeUninit<u8>, dtor: unsafe fn(&mut OwnedBuf), user_data: *const (), capacity: usize, filled: usize, init: usize, ) -> OwnedBuf
Sourcepub unsafe fn into_vec(self) -> Vec<u8> ⓘ
pub unsafe fn into_vec(self) -> Vec<u8> ⓘ
Convert this buffer into a Vec
of u8
.
§Safety
It is only safe to use this method if the buffer was created from a Vec<u8>
.
Sourcepub unsafe fn into_maybe_uninit_vec(self) -> Vec<MaybeUninit<u8>>
pub unsafe fn into_maybe_uninit_vec(self) -> Vec<MaybeUninit<u8>>
Convert this buffer into a Vec
of MaybeUninit<u8>
.
§Safety
It is only safe to use this method if the buffer was created from a Vec<MaybeUninit<u8>>
.
Sourcepub fn filled_len(&self) -> usize
pub fn filled_len(&self) -> usize
Returns the length of the filled part of the buffer.
Sourcepub fn filled(self) -> OwnedSlice
pub fn filled(self) -> OwnedSlice
Returns an OwnedSlice
covering the filled part of this buffer.
Sourcepub fn filled_slice(self, range: impl UsizeRange) -> OwnedSlice
pub fn filled_slice(self, range: impl UsizeRange) -> OwnedSlice
Returns an OwnedSlice
covering a slice of the filled part of this buffer.
The supplied range must be within the filled part of the buffer.
§Panics
This function will panic if range
is outside the bounds of the filled part of the buffer.
Sourcepub fn unfilled(self) -> OwnedCursor ⓘ
pub fn unfilled(self) -> OwnedCursor ⓘ
Returns a cursor over the unfilled part of the buffer.
Sourcepub fn unfilled_slice(self, range: RangeTo<usize>) -> OwnedCursor ⓘ
pub fn unfilled_slice(self, range: RangeTo<usize>) -> OwnedCursor ⓘ
Returns a cursor over a slice of the unfilled part of the buffer.
The supplied range covers the whole buffer, not just the unfilled part. The upper bound of the range must be within the unfilled part.
§Examples
Where buf
has a capacity (total length) of 16 bytes and the first 4 bytes are filled,
buf.unfilled_slice(..8)
will return a cursor over the first 4 unfilled bytes.
Sourcepub fn clear(&mut self) -> &mut Self
pub fn clear(&mut self) -> &mut Self
Clears the buffer, resetting the filled region to empty.
The number of initialized bytes is not changed, and the contents of the buffer are not modified.
Sourcepub unsafe fn set_init(&mut self, n: usize) -> &mut Self
pub unsafe fn set_init(&mut self, n: usize) -> &mut Self
Asserts that the first n
bytes of the buffer are initialized.
OwnedBuf
assumes that bytes are never de-initialized, so this method does nothing when called with fewer
bytes than are already known to be initialized.
§Safety
The caller must ensure that the first n
unfilled bytes of the buffer have already been initialized.
Sourcepub fn into_raw_parts(
self,
) -> (*mut MaybeUninit<u8>, unsafe fn(&mut OwnedBuf), *const (), usize, usize, usize)
pub fn into_raw_parts( self, ) -> (*mut MaybeUninit<u8>, unsafe fn(&mut OwnedBuf), *const (), usize, usize, usize)
Decomposes this OwnedBuf
into its raw components.
Returns (data, dtor, filled, init, capacity)
where data
is a pointer to the buffer’s
data in memory, dtor
is the buffer’s destructor function, filled
is the number of bytes
which are filled with data, init
is the number of bytes which have been initialised (i.e.,
which are safe to treat as u8
rather than MaybeUninit<u8>
), and capacity
is the total
length of the buffer. filled <= init <= capacity
.
§Safety
See module docs for safety requirements on the returned destructor function.