Skip to content

Commit

Permalink
Better document api contract for WritableBuffer
Browse files Browse the repository at this point in the history
Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>
  • Loading branch information
philipc and bjorn3 committed Aug 28, 2021
1 parent ad96f22 commit c8c83b1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/write/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ use crate::pod::{bytes_of, bytes_of_slice, Pod};
#[allow(clippy::len_without_is_empty)]
pub trait WritableBuffer {
/// Returns position/offset for data to be written at.
///
/// Should only be used in debug assertions
fn len(&self) -> usize;

/// Reserves specified number of bytes in the buffer.
fn reserve(&mut self, additional: usize) -> Result<(), ()>;
///
/// This will be called exactly once before writing anything to the buffer,
/// and the given size is the exact total number of bytes that will be written.
fn reserve(&mut self, size: usize) -> Result<(), ()>;

/// Writes zero bytes at the end of the buffer until the buffer
/// has the specified length.
Expand Down Expand Up @@ -54,8 +59,9 @@ impl WritableBuffer for Vec<u8> {
}

#[inline]
fn reserve(&mut self, additional: usize) -> Result<(), ()> {
self.reserve(additional);
fn reserve(&mut self, size: usize) -> Result<(), ()> {
debug_assert!(self.is_empty());
self.reserve(size);
Ok(())
}

Expand All @@ -67,6 +73,7 @@ impl WritableBuffer for Vec<u8> {

#[inline]
fn write_bytes(&mut self, val: &[u8]) {
debug_assert!(self.len() + val.len() <= self.capacity());
self.extend_from_slice(val)
}
}
Expand Down

0 comments on commit c8c83b1

Please sign in to comment.