Skip to content

Commit

Permalink
feat(core): add access methods for Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Miao committed Apr 25, 2024
1 parent 1130fab commit c8e35d2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/types/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ impl Buffer {
self.len() == 0
}

/// Number of [`Bytes`] in [`Buffer`].
///
/// For contiguous buffer, it's always 1. For non-contiguous buffer, it's number of bytes
/// available for use.
pub fn count(&self) -> usize {
match &self.0 {
Inner::Contiguous(_) => 1,
Inner::NonContiguous { parts, idx, .. } => parts.len() - idx,
}
}

/// Get current [`Bytes`].
pub fn current(&self) -> Bytes {
match &self.0 {
Inner::Contiguous(inner) => inner.clone(),
Inner::NonContiguous {
parts, idx, offset, ..
} => parts[idx].slice(offset..),
}
}

/// Shortens the buffer, keeping the first `len` bytes and dropping the rest.
///
/// If `len` is greater than the buffer’s current length, this has no effect.
Expand Down

0 comments on commit c8e35d2

Please sign in to comment.