Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ReadBufCursor::remaining and ReadBufCursor::put_slice #3700

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
nox marked this conversation as resolved.
Show resolved Hide resolved
#[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
Loading