From 5af4f03d54e4e57bdb88e818faaea4deccbc9ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 9 Jun 2025 10:48:32 -0700 Subject: [PATCH 1/5] from_owner --- crates/bytes-str/src/byte_str.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/bytes-str/src/byte_str.rs b/crates/bytes-str/src/byte_str.rs index ce9b697..1cbc38c 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -114,6 +114,19 @@ impl BytesStr { }) } + /// Creates a new BytesStr from an owner. + /// + /// + /// See [Bytes::from_owner] for more information. + pub fn from_owner(owner: T) -> Self + where + T: AsRef<[u8]> + Send + 'static, + { + Self { + bytes: Bytes::from_owner(owner), + } + } + /// Creates a new BytesStr from a [Bytes] without checking if the bytes /// are valid UTF-8. /// From 09df2440c4040a5aabbe8bc2bd24fa3e2fdf7a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 9 Jun 2025 10:52:13 -0700 Subject: [PATCH 2/5] more api --- crates/bytes-str/src/byte_str.rs | 71 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/bytes-str/src/byte_str.rs b/crates/bytes-str/src/byte_str.rs index 1cbc38c..7a37875 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -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, @@ -249,6 +249,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) -> 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 From 1da7fcca87c967e91c99a8c67988727b1664e6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 9 Jun 2025 10:52:36 -0700 Subject: [PATCH 3/5] bump --- Cargo.lock | 2 +- crates/bytes-str/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9610864..67a6940 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "bytes-str" -version = "0.2.3" +version = "0.2.4" dependencies = [ "bytes", "rkyv", diff --git a/crates/bytes-str/Cargo.toml b/crates/bytes-str/Cargo.toml index 293986f..388ff42 100644 --- a/crates/bytes-str/Cargo.toml +++ b/crates/bytes-str/Cargo.toml @@ -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"] From 136a3e6a10ebaadfaffebe336217dac115810d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 9 Jun 2025 10:54:19 -0700 Subject: [PATCH 4/5] utf8 check --- crates/bytes-str/src/byte_str.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/bytes-str/src/byte_str.rs b/crates/bytes-str/src/byte_str.rs index 7a37875..4130675 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -118,13 +118,15 @@ impl BytesStr { /// /// /// See [Bytes::from_owner] for more information. - pub fn from_owner(owner: T) -> Self + pub fn from_owner(owner: T) -> Result where T: AsRef<[u8]> + Send + 'static, { - Self { + 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 From 3103da786bac132f90adf80f1e8e413eaaa3363b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 9 Jun 2025 10:55:57 -0700 Subject: [PATCH 5/5] rename --- crates/bytes-str/src/byte_str.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bytes-str/src/byte_str.rs b/crates/bytes-str/src/byte_str.rs index 4130675..6a6a6e2 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -116,9 +116,8 @@ impl BytesStr { /// Creates a new BytesStr from an owner. /// - /// /// See [Bytes::from_owner] for more information. - pub fn from_owner(owner: T) -> Result + pub fn from_owned_utf8(owner: T) -> Result where T: AsRef<[u8]> + Send + 'static, {