From 5b645d0cf48569c5a506e287d907154915de2434 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 15:26:01 -0700 Subject: [PATCH 1/4] fmt::Write --- crates/bytes-str/src/byte_string.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/bytes-str/src/byte_string.rs b/crates/bytes-str/src/byte_string.rs index 91f1aeb..c32daeb 100644 --- a/crates/bytes-str/src/byte_string.rs +++ b/crates/bytes-str/src/byte_string.rs @@ -727,6 +727,18 @@ impl Hash for BytesString { } } +impl fmt::Write for BytesString { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.push_str(s); + Ok(()) + } + + fn write_char(&mut self, c: char) -> fmt::Result { + self.push(c); + Ok(()) + } +} + #[cfg(feature = "serde")] mod serde_impl { use serde::{Deserialize, Deserializer, Serialize, Serializer}; From 400470cc71f35047bcde8461e8c62ef738584cac 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 15:26:09 -0700 Subject: [PATCH 2/4] 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 ef9a54c..827f35a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "bytes-str" -version = "0.2.5" +version = "0.2.6" dependencies = [ "bytes", "rkyv", diff --git a/crates/bytes-str/Cargo.toml b/crates/bytes-str/Cargo.toml index 85548d0..fdfe186 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.5" +version = "0.2.6" [features] rkyv = ["dep:rkyv", "rkyv/bytes-1"] From 5491f3c6e0edfd6de57f47f3b307cec3dcbb5774 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 15:31:10 -0700 Subject: [PATCH 3/4] From for String --- crates/bytes-str/src/byte_string.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/bytes-str/src/byte_string.rs b/crates/bytes-str/src/byte_string.rs index c32daeb..6a7db66 100644 --- a/crates/bytes-str/src/byte_string.rs +++ b/crates/bytes-str/src/byte_string.rs @@ -402,6 +402,17 @@ impl From for BytesString { } } +impl From for String { + fn from(s: BytesString) -> Self { + let vec: Vec<_> = s.bytes.freeze().into(); + + unsafe { + // SAFETY: We know the bytes are valid UTF-8 because we created them + String::from_utf8_unchecked(vec) + } + } +} + impl From<&str> for BytesString { fn from(s: &str) -> Self { Self { From e9924f551948a4e98e10cdb1ccd462bee9a98d3f 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 15:35:40 -0700 Subject: [PATCH 4/4] into_string() --- crates/bytes-str/src/byte_string.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/bytes-str/src/byte_string.rs b/crates/bytes-str/src/byte_string.rs index 6a7db66..202d459 100644 --- a/crates/bytes-str/src/byte_string.rs +++ b/crates/bytes-str/src/byte_string.rs @@ -366,6 +366,24 @@ impl BytesString { bytes: BytesMut::from(bytes), }) } + + /// Converts a [BytesString] into a [String]. + /// + /// # Examples + /// + /// ``` + /// use bytes_str::BytesString; + /// ``` + /// use bytes_str::BytesString; + /// + /// let s = BytesString::from("hello"); + /// + /// let string = s.into_string(); + /// + /// assert_eq!(string, "hello"); + pub fn into_string(self) -> String { + self.into() + } } impl Deref for BytesString {