Skip to content

Commit

Permalink
proto: Document Packetizer's spare_capacity_mut and bytes_written
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-hamester committed Nov 23, 2023
1 parent b8189d3 commit 7a17191
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions proto/src/message/packetizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ impl Packetizer {
self.buf.extend_from_slice(bytes.as_ref());
}

/// Returns a slice of uninitialized bytes at the end of the internal buffer.
///
/// This function, together with [`bytes_written`](Self::bytes_written), make it possible to
/// fill the packetizer without an intermediate buffer.
///
/// The slice returned by this function is guaranteed to be non-empty.
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
if let Some(len) = self.len {
if self.buf.capacity() < len {
Expand All @@ -39,6 +45,12 @@ impl Packetizer {
slice
}

/// Asserts that the next `len` bytes have been initialized.
///
/// # Safety
///
/// You must ensure that prior to calling this function, at least `len` bytes of the slice
/// returned by [`spare_capacity_mut`](Self::spare_capacity_mut) have been initialized.
pub unsafe fn bytes_written(&mut self, len: usize) {
unsafe {
self.buf.set_len(self.buf.len() + len);
Expand Down

0 comments on commit 7a17191

Please sign in to comment.