pub struct BorrowedCursor<'a, T> { /* private fields */ }Expand description
A writeable view of the unfilled portion of a BorrowedBuf.
The unfilled portion may be uninitialized; see BorrowedBuf for details.
Data can be written directly to the cursor by using append or
indirectly by getting a slice of part or all of the cursor and writing into the slice. In the
indirect case, the caller must call advance after writing to inform
the cursor how many elements have been written.
Once elements are written to the cursor, they become part of the filled portion of the
underlying BorrowedBuf and can no longer be accessed or re-written by the cursor. In other
words, the cursor tracks the unfilled part of the underlying BorrowedBuf.
The lifetime 'a is a bound on the lifetime of the underlying buffer (which means it is a bound
on the elements in that buffer by transitivity).
§Examples
use borrowed_buf::{BorrowedBuf, BorrowedCursor};
use core::mem::MaybeUninit;
// A producer that only sees the free space.
fn produce(mut cursor: BorrowedCursor<'_, u32>) {
let n = cursor.capacity().min(3);
// SAFETY: only initialized values are written.
let out = unsafe { cursor.as_mut() };
for (i, slot) in out[..n].iter_mut().enumerate() {
slot.write(i as u32 * 10);
}
// SAFETY: the first `n` elements of the cursor were just written.
unsafe { cursor.advance(n) };
}
let mut storage = [MaybeUninit::uninit(); 4];
let mut buf = BorrowedBuf::from(&mut storage[..]);
buf.unfilled().append(&[7]);
produce(buf.unfilled());
assert_eq!(buf.filled(), &[7, 0, 10, 20]);Implementations§
Source§impl<'a, T: Copy> BorrowedCursor<'a, T>
impl<'a, T: Copy> BorrowedCursor<'a, T>
Sourcepub fn reborrow<'this>(&'this mut self) -> BorrowedCursor<'this, T>
pub fn reborrow<'this>(&'this mut self) -> BorrowedCursor<'this, T>
Reborrows this cursor by cloning it with a smaller lifetime.
Since a cursor maintains unique access to its underlying buffer, the borrowed cursor is not accessible while the new cursor exists.
Sourcepub fn written(&self) -> usize
pub fn written(&self) -> usize
Returns the number of elements written to the BorrowedBuf this cursor was created from.
In particular, the count returned is shared by all reborrows of the cursor.
Sourcepub unsafe fn set_init(&mut self)
pub unsafe fn set_init(&mut self)
Set the buffer as fully initialized.
§Safety
All the elements of the cursor must be initialized.
use borrowed_buf::BorrowedBuf;
use core::mem::MaybeUninit;
let mut storage = [MaybeUninit::<u8>::uninit(); 4];
let mut buf = BorrowedBuf::from(&mut storage[..]);
let mut cursor = buf.unfilled();
// SAFETY: only initialized values are written.
unsafe { cursor.as_mut() }.fill(MaybeUninit::new(b'x'));
// SAFETY: every element of the cursor was just written.
unsafe { cursor.set_init() };
cursor.advance_checked(4);
assert_eq!(buf.filled(), b"xxxx");Sourcepub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<T>]
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<T>]
Returns a mutable reference to the whole cursor.
§Safety
The caller must not uninitialize any elements of the cursor if it is initialized.
Sourcepub fn advance_checked(&mut self, n: usize) -> &mut Self
pub fn advance_checked(&mut self, n: usize) -> &mut Self
Advances the cursor by asserting that n elements have been filled.
After advancing, the n elements are no longer accessible via the cursor and can only be
accessed via the underlying buffer. I.e., the buffer’s filled portion grows by n elements
and its unfilled portion (and the capacity of this cursor) shrinks by n elements.
If less than n elements initialized (by the cursor’s point of view), set_init should be
called first.
§Panics
Panics if there are less than n elements initialized.
Sourcepub unsafe fn advance(&mut self, n: usize) -> &mut Self
pub unsafe fn advance(&mut self, n: usize) -> &mut Self
Advances the cursor by asserting that n elements have been filled.
After advancing, the n elements are no longer accessible via the cursor and can only be
accessed via the underlying buffer. I.e., the buffer’s filled portion grows by n elements
and its unfilled portion (and the capacity of this cursor) shrinks by n elements.
§Safety
The caller must ensure that the first n elements of the cursor have been initialized.
Sourcepub fn append(&mut self, buf: &[T])
pub fn append(&mut self, buf: &[T])
Append elements to the cursor, advancing position within its buffer.
§Panics
Panics if self.capacity() is less than buf.len().
use borrowed_buf::BorrowedBuf;
use core::mem::MaybeUninit;
let mut storage = [MaybeUninit::uninit(); 4];
let mut buf = BorrowedBuf::from(&mut storage[..]);
buf.unfilled().append(&[1u64, 2]);
buf.unfilled().append(&[3]);
assert_eq!(buf.filled(), &[1, 2, 3]);Sourcepub fn append_fill(&mut self, n: usize, value: T)
pub fn append_fill(&mut self, n: usize, value: T)
Appends n copies of value to the cursor, advancing position within its buffer.
Like append, this writes each element once and leaves the rest of the
cursor untouched. It does not need a source slice of n elements.
This is not part of core::io::BorrowedCursor.
§Panics
Panics if self.capacity() is less than n.
use borrowed_buf::BorrowedBuf;
use core::mem::MaybeUninit;
let mut storage = [MaybeUninit::uninit(); 8];
let mut buf = BorrowedBuf::<u8>::from(&mut storage[..]);
let mut cursor = buf.unfilled();
cursor.append(b"ab");
cursor.append_fill(3, b'-');
assert_eq!(buf.filled(), b"ab---");
assert!(!buf.is_init());Sourcepub fn with_unfilled_buf<R>(
&mut self,
f: impl FnOnce(&mut BorrowedBuf<'_, T>) -> R,
) -> R
pub fn with_unfilled_buf<R>( &mut self, f: impl FnOnce(&mut BorrowedBuf<'_, T>) -> R, ) -> R
Runs the given closure with a BorrowedBuf containing the unfilled part
of the cursor.
This enables inspecting what was written to the cursor.
§Panics
Panics if the BorrowedBuf given to the closure is replaced by another
one.
use borrowed_buf::BorrowedBuf;
use core::mem::MaybeUninit;
let mut storage = [MaybeUninit::uninit(); 8];
let mut buf = BorrowedBuf::from(&mut storage[..]);
let mut cursor = buf.unfilled();
cursor.append(b"ab");
let fresh = cursor.with_unfilled_buf(|sub| {
sub.unfilled().append(b"cd");
sub.filled().to_vec()
});
assert_eq!(fresh, b"cd");
assert_eq!(buf.filled(), b"abcd");Source§impl<'a, T: Default + Copy> BorrowedCursor<'a, T>
impl<'a, T: Default + Copy> BorrowedCursor<'a, T>
Sourcepub fn ensure_init(&mut self) -> &mut [T]
pub fn ensure_init(&mut self) -> &mut [T]
Initializes all elements in the cursor with their default value and returns them.
use borrowed_buf::BorrowedBuf;
use core::mem::MaybeUninit;
let mut storage = [MaybeUninit::<f32>::uninit(); 4];
let mut buf = BorrowedBuf::from(&mut storage[..]);
let mut cursor = buf.unfilled();
let out = cursor.ensure_init();
assert_eq!(out, &[0.0; 4]);
out[0] = 1.5;
cursor.advance_checked(1);
assert_eq!(buf.filled(), &[1.5]);Trait Implementations§
Source§impl<T> Debug for BorrowedCursor<'_, T>
impl<T> Debug for BorrowedCursor<'_, T>
Source§impl<'data, T: Copy> From<BorrowedCursor<'data, T>> for BorrowedBuf<'data, T>
Creates a new BorrowedBuf from a cursor.
impl<'data, T: Copy> From<BorrowedCursor<'data, T>> for BorrowedBuf<'data, T>
Creates a new BorrowedBuf from a cursor.
Use BorrowedCursor::with_unfilled_buf instead for a safer alternative.
Source§fn from(buf: BorrowedCursor<'data, T>) -> BorrowedBuf<'data, T>
fn from(buf: BorrowedCursor<'data, T>) -> BorrowedBuf<'data, T>
impl<T: Send> Send for BorrowedCursor<'_, T>
impl<T: Sync> Sync for BorrowedCursor<'_, T>
Source§impl Write for BorrowedCursor<'_, u8>
impl Write for BorrowedCursor<'_, u8>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)