Skip to content
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
32 changes: 2 additions & 30 deletions src/common/rewind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ where
if let Some(mut prefix) = self.pre.take() {
// If there are no remaining bytes, let the bytes get dropped.
if !prefix.is_empty() {
let copy_len = cmp::min(prefix.len(), remaining(&mut buf));
// TODO: There should be a way to do following two lines cleaner...
put_slice(&mut buf, &prefix[..copy_len]);
let copy_len = cmp::min(prefix.len(), buf.remaining());
buf.put_slice(&prefix[..copy_len]);
prefix.advance(copy_len);
// Put back what's left
if !prefix.is_empty() {
Expand All @@ -53,33 +52,6 @@ where
}
}

fn remaining(cursor: &mut ReadBufCursor<'_>) -> usize {
// SAFETY:
// We do not uninitialize any set bytes.
unsafe { cursor.as_mut().len() }
}

// Copied from `ReadBufCursor::put_slice`.
// If that becomes public, we could ditch this.
fn put_slice(cursor: &mut ReadBufCursor<'_>, slice: &[u8]) {
assert!(
remaining(cursor) >= slice.len(),
"buf.len() must fit in remaining()"
);

let amt = slice.len();

// SAFETY:
// the length is asserted above
unsafe {
cursor.as_mut()[..amt]
.as_mut_ptr()
.cast::<u8>()
.copy_from_nonoverlapping(slice.as_ptr(), amt);
cursor.advance(amt);
}
}

impl<T> Write for Rewind<T>
where
T: Write + Unpin,
Expand Down