Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline annotations #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,25 @@ impl Buffer {
}

/// returns how much data can be read from the buffer
#[inline]
pub fn available_data(&self) -> usize {
self.end - self.position
}

/// returns how much free space is available to write to
#[inline]
pub fn available_space(&self) -> usize {
self.capacity - self.end
}

/// returns the underlying vector's size
#[inline]
pub fn capacity(&self) -> usize {
self.capacity
}

/// returns true if there is no more data to read
#[inline]
pub fn empty(&self) -> bool {
self.position == self.end
}
Expand All @@ -130,6 +134,7 @@ impl Buffer {
/// if the position gets past the buffer's half,
/// this will call `shift()` to move the remaining data
/// to the beginning of the buffer
#[inline]
pub fn consume(&mut self, count: usize) -> usize {
let cnt = cmp::min(count, self.available_data());
self.position += cnt;
Expand All @@ -144,6 +149,7 @@ impl Buffer {
///
/// This method is similar to `consume()` but will not move data
/// to the beginning of the buffer
#[inline]
pub fn consume_noshift(&mut self, count: usize) -> usize {
let cnt = cmp::min(count, self.available_data());
self.position += cnt;
Expand All @@ -156,6 +162,7 @@ impl Buffer {
/// if there is not enough available space, this function can call
/// `shift()` to move the remaining data to the beginning of the
/// buffer
#[inline]
pub fn fill(&mut self, count: usize) -> usize {
let cnt = cmp::min(count, self.available_space());
self.end += cnt;
Expand Down Expand Up @@ -186,24 +193,28 @@ impl Buffer {
/// assert_eq!(b.position(), 5);
/// assert_eq!(b.available_data(), 3);
/// ```
#[inline]
pub fn position(&self) -> usize {
self.position
}

/// moves the position and end trackers to the beginning
/// this function does not modify the data
#[inline]
pub fn reset(&mut self) {
self.position = 0;
self.end = 0;
}

/// returns a slice with all the available data
#[inline]
pub fn data(&self) -> &[u8] {
&self.memory[self.position..self.end]
}

/// returns a mutable slice with all the available space to
/// write to
#[inline]
pub fn space(&mut self) -> &mut[u8] {
&mut self.memory[self.end..self.capacity]
}
Expand Down