Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/bytes-str/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = { workspace = true }
name = "bytes-str"
repository = { workspace = true }
version = "0.2.3"
version = "0.2.4"

[features]
rkyv = ["dep:rkyv", "rkyv/bytes-1"]
Expand Down
85 changes: 84 additions & 1 deletion crates/bytes-str/src/byte_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ffi::OsStr,
fmt::{self, Debug, Display},
hash::{Hash, Hasher},
ops::{Deref, Index},
ops::{Deref, Index, RangeBounds},
path::Path,
slice::SliceIndex,
str::Utf8Error,
Expand Down Expand Up @@ -114,6 +114,20 @@ impl BytesStr {
})
}

/// Creates a new BytesStr from an owner.
///
/// See [Bytes::from_owner] for more information.
pub fn from_owned_utf8<T>(owner: T) -> Result<Self, Utf8Error>
where
T: AsRef<[u8]> + Send + 'static,
{
std::str::from_utf8(owner.as_ref())?;

Ok(Self {
bytes: Bytes::from_owner(owner),
})
}

/// Creates a new BytesStr from a [Bytes] without checking if the bytes
/// are valid UTF-8.
///
Expand Down Expand Up @@ -236,6 +250,75 @@ impl BytesStr {
self.bytes
}

/// Returns the length of the [BytesStr].
///
/// # Examples
///
/// ```
/// use bytes_str::BytesStr;
///
/// let s = BytesStr::from_static("hello");
///
/// assert_eq!(s.len(), 5);
/// ```
pub const fn len(&self) -> usize {
self.bytes.len()
}

/// Returns true if the [BytesStr] is empty.
///
/// # Examples
///
/// ```
/// use bytes_str::BytesStr;
///
/// let s = BytesStr::new();
///
/// assert!(s.is_empty());
/// ```
pub const fn is_empty(&self) -> bool {
self.bytes.is_empty()
}

/// Returns a slice of the [BytesStr].
///
/// # Panics
///
/// Panics if the bounds are not character boundaries.
///
/// # Examples
///
/// ```
/// use bytes_str::BytesStr;
///
/// let s = BytesStr::from_static("hello");
/// let slice = s.slice(1..3);
///
/// assert_eq!(slice.as_str(), "el");
/// ```
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
let s = Self {
bytes: self.bytes.slice(range),
};

if !s.is_char_boundary(0) {
panic!("range start is not a character boundary");
}

if !s.is_char_boundary(s.len()) {
panic!("range end is not a character boundary");
}

s
}

/// See [Bytes::slice_ref]
pub fn slice_ref(&self, subset: &str) -> Self {
Self {
bytes: self.bytes.slice_ref(subset.as_bytes()),
}
}

/// Advances the [BytesStr] by `n` bytes.
///
/// # Panics
Expand Down
Loading