Skip to content

Commit

Permalink
feat(rt): add ReadBufCursor methods remaining() and put_slice() (
Browse files Browse the repository at this point in the history
…#3700)

There is currently no way to write to ReadBufCursor without
unsafe code, even though writing a slice to it like one would
do to a Buf shouldn't require unsafe code.
  • Loading branch information
nox committed Jul 15, 2024
1 parent 4fda6b3 commit 5a13041
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/rt/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,29 @@ impl<'data> ReadBufCursor<'data> {
self.buf.init = self.buf.filled.max(self.buf.init);
}

/// Returns the number of bytes that can be written from the current
/// position until the end of the buffer is reached.
///
/// This value is equal to the length of the slice returned by `as_mut()``.
#[inline]
pub(crate) fn remaining(&self) -> usize {
pub fn remaining(&self) -> usize {
self.buf.remaining()
}

/// Transfer bytes into `self`` from `src` and advance the cursor
/// by the number of bytes written.
///
/// # Panics
///
/// `self` must have enough remaining capacity to contain all of `src`.
#[inline]
pub(crate) fn put_slice(&mut self, buf: &[u8]) {
pub fn put_slice(&mut self, src: &[u8]) {
assert!(
self.buf.remaining() >= buf.len(),
"buf.len() must fit in remaining()"
self.buf.remaining() >= src.len(),
"src.len() must fit in remaining()"
);

let amt = buf.len();
let amt = src.len();
// Cannot overflow, asserted above
let end = self.buf.filled + amt;

Expand All @@ -265,7 +275,7 @@ impl<'data> ReadBufCursor<'data> {
self.buf.raw[self.buf.filled..end]
.as_mut_ptr()
.cast::<u8>()
.copy_from_nonoverlapping(buf.as_ptr(), amt);
.copy_from_nonoverlapping(src.as_ptr(), amt);
}

if self.buf.init < end {
Expand Down

0 comments on commit 5a13041

Please sign in to comment.