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.5"
version = "0.2.6"

[features]
rkyv = ["dep:rkyv", "rkyv/bytes-1"]
Expand Down
41 changes: 41 additions & 0 deletions crates/bytes-str/src/byte_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -402,6 +420,17 @@ impl From<String> for BytesString {
}
}

impl From<BytesString> 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 {
Expand Down Expand Up @@ -727,6 +756,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};
Expand Down
Loading