Skip to content
Open
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
17 changes: 17 additions & 0 deletions rmpv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ pub struct Utf8String {
}

impl Utf8String {
/// Try to parse bytes as UTF-8.
#[must_use]
pub fn from_bytes(b: Vec<u8>) -> Self {
Self {
s: String::from_utf8(b).map_err(|e| {
let ue = e.utf8_error();
(e.into_bytes(), ue)
})
}
}

/// Returns `true` if the string is valid UTF-8.
#[inline]
#[must_use]
Expand Down Expand Up @@ -337,6 +348,12 @@ pub struct Utf8StringRef<'a> {
}

impl<'a> Utf8StringRef<'a> {
/// Try to parse bytes as UTF-8.
#[must_use]
pub fn from_bytes(b: &'a [u8]) -> Self {
Self { s: str::from_utf8(b).map_err(|e| (b, e)) }
}

/// Returns `true` if the string is valid UTF-8.
#[inline]
#[must_use]
Expand Down