serde_core/private/string.rs
1use crate::lib::*;
2
3#[cfg(any(feature = "std", feature = "alloc"))]
4#[doc(hidden)]
5pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<'_, str> {
6 String::from_utf8_lossy(bytes)
7}
8
9// The generated code calls this like:
10//
11// let value = &_serde::__private::from_utf8_lossy(bytes);
12// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
13//
14// so it is okay for the return type to be different from the std case as long
15// as the above works.
16#[cfg(not(any(feature = "std", feature = "alloc")))]
17#[doc(hidden)]
18pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
19 // Three unicode replacement characters if it fails. They look like a
20 // white-on-black question mark. The user will recognize it as invalid
21 // UTF-8.
22 str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
23}