diff --git a/Cargo.lock b/Cargo.lock index 827f35a..bc966a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "bytes-str" -version = "0.2.6" +version = "0.2.7" dependencies = [ "bytes", "rkyv", diff --git a/crates/bytes-str/Cargo.toml b/crates/bytes-str/Cargo.toml index fdfe186..90e59f7 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.6" +version = "0.2.7" [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 231255b..fa220ac 100644 --- a/crates/bytes-str/src/byte_str.rs +++ b/crates/bytes-str/src/byte_str.rs @@ -284,6 +284,41 @@ impl BytesStr { self.bytes } + /// Converts the [BytesStr] into a [Vec]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesStr; + /// + /// let s = BytesStr::from_static("hello"); + /// let vec = s.into_vec(); + /// + /// assert_eq!(vec, b"hello"); + /// ``` + pub fn into_vec(self) -> Vec { + self.into_bytes().to_vec() + } + + /// Converts the [BytesStr] into a [String]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesStr; + /// + /// let s = BytesStr::from_static("hello"); + /// let string = s.into_string(); + /// + /// assert_eq!(string, "hello"); + /// ``` + pub fn into_string(self) -> String { + unsafe { + // Safety: BytesStr is backed by a valid UTF-8 string. + String::from_utf8_unchecked(self.into_vec()) + } + } + /// Returns the length of the [BytesStr]. /// /// # Examples diff --git a/crates/bytes-str/src/byte_string.rs b/crates/bytes-str/src/byte_string.rs index 202d459..6f6dcf3 100644 --- a/crates/bytes-str/src/byte_string.rs +++ b/crates/bytes-str/src/byte_string.rs @@ -367,6 +367,22 @@ impl BytesString { }) } + /// Converts the [BytesString] into a [Vec]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesString; + /// + /// let s = BytesString::from("hello"); + /// let vec = s.into_vec(); + /// + /// assert_eq!(vec, b"hello"); + /// ``` + pub fn into_vec(self) -> Vec { + self.bytes.into() + } + /// Converts a [BytesString] into a [String]. /// /// # Examples