diff --git a/Cargo.lock b/Cargo.lock index 67a6940..ef9a54c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "bytes-str" -version = "0.2.4" +version = "0.2.5" dependencies = [ "bytes", "rkyv", diff --git a/crates/bytes-str/Cargo.toml b/crates/bytes-str/Cargo.toml index 388ff42..85548d0 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.4" +version = "0.2.5" [features] rkyv = ["dep:rkyv", "rkyv/bytes-1"] diff --git a/crates/bytes-str/src/byte_str.rs b/crates/bytes-str/src/byte_str.rs index 6a6a6e2..231255b 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -185,6 +185,40 @@ impl BytesStr { } } + /// Creates a new BytesStr from a [str]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesStr; + /// + /// let s = BytesStr::from_str_slice("hello"); + /// + /// assert_eq!(s.as_str(), "hello"); + /// ``` + pub fn from_str_slice(bytes: &str) -> Self { + Self { + bytes: Bytes::copy_from_slice(bytes.as_bytes()), + } + } + + /// Creates a new BytesStr from a [String]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesStr; + /// + /// let s = BytesStr::from_string("hello".to_string()); + /// + /// assert_eq!(s.as_str(), "hello"); + /// ``` + pub fn from_string(bytes: String) -> Self { + Self { + bytes: Bytes::from(bytes), + } + } + /// Creates a new BytesStr from a static UTF-8 slice. /// /// # Examples