Skip to content

Commit c799237

Browse files
committed
Expose ReadBufCursor::remaining and ReadBufCursor::put_slice
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.
1 parent 4fda6b3 commit c799237

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/rt/io.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,27 @@ impl<'data> ReadBufCursor<'data> {
244244
self.buf.init = self.buf.filled.max(self.buf.init);
245245
}
246246

247+
/// Returns the number of bytes that can be written from the current
248+
/// position until the end of the buffer is reached.
249+
///
250+
/// This value is equal tho the length of the slice returned by `as_mut()``.
247251
#[inline]
248-
pub(crate) fn remaining(&self) -> usize {
252+
pub fn remaining(&self) -> usize {
249253
self.buf.remaining()
250254
}
251255

256+
/// Transfer bytes into `self`` from `src` and advance the cursor
257+
/// by the number of bytes written.
258+
///
259+
/// `self` must have enough remaining capacity to contain all of `src`.
252260
#[inline]
253-
pub(crate) fn put_slice(&mut self, buf: &[u8]) {
261+
pub fn put_slice(&mut self, src: &[u8]) {
254262
assert!(
255-
self.buf.remaining() >= buf.len(),
256-
"buf.len() must fit in remaining()"
263+
self.buf.remaining() >= src.len(),
264+
"src.len() must fit in remaining()"
257265
);
258266

259-
let amt = buf.len();
267+
let amt = src.len();
260268
// Cannot overflow, asserted above
261269
let end = self.buf.filled + amt;
262270

@@ -265,7 +273,7 @@ impl<'data> ReadBufCursor<'data> {
265273
self.buf.raw[self.buf.filled..end]
266274
.as_mut_ptr()
267275
.cast::<u8>()
268-
.copy_from_nonoverlapping(buf.as_ptr(), amt);
276+
.copy_from_nonoverlapping(src.as_ptr(), amt);
269277
}
270278

271279
if self.buf.init < end {

0 commit comments

Comments
 (0)