1#![cfg(any(
5 feature = "storage",
6 feature = "wire",
7 feature = "descriptive",
8 feature = "json",
9 feature = "value"
10))]
11
12#[cfg(feature = "alloc")]
13use rust_alloc::string::String;
14#[cfg(feature = "alloc")]
15use rust_alloc::vec::Vec;
16
17use core::fmt;
18
19#[cfg(not(feature = "simdutf8"))]
20#[doc(inline)]
21pub use core::str::from_utf8;
22
23#[derive(Debug)]
25#[non_exhaustive]
26pub struct Utf8Error;
27
28impl core::error::Error for Utf8Error {}
29
30impl fmt::Display for Utf8Error {
31 #[inline]
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "invalid or incomplete utf-8 sequence")
34 }
35}
36
37#[inline(always)]
42#[cfg(all(feature = "alloc", not(feature = "simdutf8")))]
43pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
44 match String::from_utf8(bytes) {
45 Ok(string) => Ok(string),
46 Err(..) => Err(Utf8Error),
47 }
48}
49
50#[inline(always)]
55#[cfg(all(feature = "alloc", feature = "simdutf8"))]
56pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
57 if simdutf8::basic::from_utf8(&bytes).is_err() {
58 return Err(Utf8Error);
59 }
60
61 Ok(unsafe { String::from_utf8_unchecked(bytes) })
63}
64
65#[inline]
75#[cfg(feature = "simdutf8")]
76pub fn from_utf8(input: &[u8]) -> Result<&str, Utf8Error> {
77 simdutf8::basic::from_utf8(input).map_err(|_| Utf8Error)
78}