From 5a13041ed7033c9dab6e2adafd08b6af20cd33fb Mon Sep 17 00:00:00 2001 From: Anthony Ramine <123095+nox@users.noreply.github.com> Date: Mon, 15 Jul 2024 20:25:49 +0200 Subject: [PATCH] feat(rt): add `ReadBufCursor` methods `remaining()` and `put_slice()` (#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. --- src/rt/io.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/rt/io.rs b/src/rt/io.rs index fbe9ecc901..e8f6ed9994 100644 --- a/src/rt/io.rs +++ b/src/rt/io.rs @@ -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; @@ -265,7 +275,7 @@ impl<'data> ReadBufCursor<'data> { self.buf.raw[self.buf.filled..end] .as_mut_ptr() .cast::() - .copy_from_nonoverlapping(buf.as_ptr(), amt); + .copy_from_nonoverlapping(src.as_ptr(), amt); } if self.buf.init < end {