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

docs(primitives): report some Bytes methods may panic #877

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
18 changes: 18 additions & 0 deletions crates/primitives/src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,43 @@ impl Bytes {
}

/// Returns a slice of self for the provided range.
///
/// # Panics
///
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
/// will panic.
#[inline]
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
Self(self.0.slice(range))
}

/// Returns a slice of self that is equivalent to the given `subset`.
///
/// # Panics
///
/// Requires that the given `subset` slice is in fact contained within the
/// `Bytes` buffer; otherwise this function will panic.
#[inline]
pub fn slice_ref(&self, subset: &[u8]) -> Self {
Self(self.0.slice_ref(subset))
}

/// Splits the bytes into two at the given index.
///
/// # Panics
///
/// Panics if `at > len`.
#[must_use = "consider Bytes::truncate if you don't need the other half"]
#[inline]
pub fn split_off(&mut self, at: usize) -> Self {
Self(self.0.split_off(at))
}

/// Splits the bytes into two at the given index.
///
/// # Panics
///
/// Panics if `at > len`.
#[must_use = "consider Bytes::advance if you don't need the other half"]
#[inline]
pub fn split_to(&mut self, at: usize) -> Self {
Expand Down
Loading