Skip to content

Commit

Permalink
Implement BufMut on UninitSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Jan 20, 2021
1 parent 4202f3b commit 15a3834
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,29 @@ unsafe impl BufMut for Vec<u8> {
}
}

unsafe impl BufMut for &mut UninitSlice {
#[inline]
fn remaining_mut(&self) -> usize {
self.len()
}

#[inline]
fn chunk_mut(&mut self) -> &mut UninitSlice {
self
}

#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
// Create an empty UninitSlice.
let mut empty_slice = [];
let empty_slice = UninitSlice::from_raw_parts_mut(empty_slice.as_mut_ptr(), empty_slice.len());

// Lifetime dance taken from `impl Write for &mut [u8]`.
let (_, b) = core::mem::replace(self, empty_slice).split_at_mut(cnt);
*self = b;
}
}

// The existence of this function makes the compiler catch if the BufMut
// trait is "object-safe" or not.
fn _assert_trait_object(_b: &dyn BufMut) {}

0 comments on commit 15a3834

Please sign in to comment.