1 //! Defines utf8 error type.
4 use crate::error::Error;
7 /// Errors which can occur when attempting to interpret a sequence of [`u8`]
10 /// As such, the `from_utf8` family of functions and methods for both [`String`]s
11 /// and [`&str`]s make use of this error, for example.
13 /// [`String`]: ../../std/string/struct.String.html#method.from_utf8
14 /// [`&str`]: super::from_utf8
18 /// This error type’s methods can be used to create functionality
19 /// similar to `String::from_utf8_lossy` without allocating heap memory:
22 /// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
24 /// match std::str::from_utf8(input) {
30 /// let (valid, after_valid) = input.split_at(error.valid_up_to());
32 /// push(std::str::from_utf8_unchecked(valid))
36 /// if let Some(invalid_sequence_length) = error.error_len() {
37 /// input = &after_valid[invalid_sequence_length..]
46 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub struct Utf8Error {
49 pub(super) valid_up_to: usize,
50 pub(super) error_len: Option<u8>,
54 /// Returns the index in the given string up to which valid UTF-8 was
57 /// It is the maximum index such that `from_utf8(&input[..index])`
58 /// would return `Ok(_)`.
67 /// // some invalid bytes, in a vector
68 /// let sparkle_heart = vec![0, 159, 146, 150];
70 /// // std::str::from_utf8 returns a Utf8Error
71 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
73 /// // the second byte is invalid here
74 /// assert_eq!(1, error.valid_up_to());
76 #[stable(feature = "utf8_error", since = "1.5.0")]
77 #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
80 pub const fn valid_up_to(&self) -> usize {
84 /// Provides more information about the failure:
86 /// * `None`: the end of the input was reached unexpectedly.
87 /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
88 /// If a byte stream (such as a file or a network socket) is being decoded incrementally,
89 /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
91 /// * `Some(len)`: an unexpected byte was encountered.
92 /// The length provided is that of the invalid byte sequence
93 /// that starts at the index given by `valid_up_to()`.
94 /// Decoding should resume after that sequence
95 /// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
98 /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
99 #[stable(feature = "utf8_error_error_len", since = "1.20.0")]
100 #[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")]
103 pub const fn error_len(&self) -> Option<usize> {
104 // FIXME: This should become `map` again, once it's `const`
105 match self.error_len {
106 Some(len) => Some(len as usize),
112 #[stable(feature = "rust1", since = "1.0.0")]
113 impl fmt::Display for Utf8Error {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 if let Some(error_len) = self.error_len {
118 "invalid utf-8 sequence of {} bytes from index {}",
119 error_len, self.valid_up_to
122 write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
127 #[cfg(not(bootstrap))]
128 #[stable(feature = "rust1", since = "1.0.0")]
129 impl Error for Utf8Error {
131 fn description(&self) -> &str {
132 "invalid utf-8: corrupt contents"
136 /// An error returned when parsing a `bool` using [`from_str`] fails
138 /// [`from_str`]: super::FromStr::from_str
139 #[derive(Debug, Clone, PartialEq, Eq)]
141 #[stable(feature = "rust1", since = "1.0.0")]
142 pub struct ParseBoolError;
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl fmt::Display for ParseBoolError {
146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147 "provided string was not `true` or `false`".fmt(f)
151 #[cfg(not(bootstrap))]
152 #[stable(feature = "rust1", since = "1.0.0")]
153 impl Error for ParseBoolError {
155 fn description(&self) -> &str {
156 "failed to parse bool"