Skip to content

Commit

Permalink
Write slice in single command
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Nov 9, 2023
1 parent 7233774 commit 7975a8b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions misc/quick-protobuf-codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ impl<'a> WriterBackend for MaybeUninitWriterBackend<'a> {
return Err(quick_protobuf::errors::Error::UnexpectedEndOfBuffer);
}

for b in buf {
self.memory[*self.written].write(*b);
*self.written += 1;
}
// SAFETY: &[u8] and &[MaybeUninit<u8>] have the same layout
let uninit_src: &[MaybeUninit<u8>] = unsafe { std::mem::transmute(buf) };

let start = *self.written;
let end = *self.written + buf.len();

self.memory[start..end].copy_from_slice(uninit_src);
*self.written += buf.len();

Ok(())
}
Expand Down

0 comments on commit 7975a8b

Please sign in to comment.