Skip to content
Myr

string.rs

This file

Path

serde_core/src/private/string.rs

23 lines · 794 bytes · Rust

Last changed by

Merge pull request #2969 from dtolnay/coreprivateDavid Tolnaybefore adoption0990d97

History of this file

Take it

1use crate::lib::*;23#[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}89// 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 long15// 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 a20    // white-on-black question mark. The user will recognize it as invalid21    // UTF-8.22    str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")23}