]>
Commit | Line | Data |
---|---|---|
7cac9316 XL |
1 | #[cfg(all(feature = "collections", not(feature = "std")))] |
2 | use collections::String; | |
3 | ||
4 | #[cfg(feature = "std")] | |
5 | use std::borrow::Cow; | |
6 | #[cfg(all(feature = "collections", not(feature = "std")))] | |
7 | use collections::borrow::Cow; | |
8 | ||
9 | pub use core::clone::Clone; | |
10 | pub use core::convert::{From, Into}; | |
11 | pub use core::default::Default; | |
12 | pub use core::fmt; | |
13 | pub use core::marker::PhantomData; | |
14 | pub use core::option::Option::{self, None, Some}; | |
15 | pub use core::result::Result::{self, Ok, Err}; | |
16 | ||
17 | #[cfg(any(feature = "collections", feature = "std"))] | |
18 | pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> { | |
19 | String::from_utf8_lossy(bytes) | |
20 | } | |
21 | ||
22 | // The generated code calls this like: | |
23 | // | |
24 | // let value = &_serde::export::from_utf8_lossy(bytes); | |
25 | // Err(_serde::de::Error::unknown_variant(value, VARIANTS)) | |
26 | // | |
27 | // so it is okay for the return type to be different from the std case as long | |
28 | // as the above works. | |
29 | #[cfg(not(any(feature = "collections", feature = "std")))] | |
30 | pub fn from_utf8_lossy(bytes: &[u8]) -> &str { | |
31 | use core::str; | |
32 | // Three unicode replacement characters if it fails. They look like a | |
33 | // white-on-black question mark. The user will recognize it as invalid | |
34 | // UTF-8. | |
35 | str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}") | |
36 | } |