From 719d0c880bc4a13b07ceb2e9a17195a4f5957b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Fri, 31 Jul 2026 20:58:08 +0200 Subject: [PATCH] Add rmpv::Utf8String[Ref]::from_bytes() These are the only constructors for the error variants, there weren't any before Sponsored-by: https://beaverlabs.nets --- rmpv/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rmpv/src/lib.rs b/rmpv/src/lib.rs index cb2ae989..2e388ca8 100644 --- a/rmpv/src/lib.rs +++ b/rmpv/src/lib.rs @@ -225,6 +225,17 @@ pub struct Utf8String { } impl Utf8String { + /// Try to parse bytes as UTF-8. + #[must_use] + pub fn from_bytes(b: Vec) -> 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] @@ -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]