Struct FromUtf8Error

struct FromUtf8Error { ... }

An error that may occur when converting a Vec<u8> to a String.

This error includes the original Vec<u8> that failed to convert to a String. This permits callers to recover the allocation used even if it it not valid UTF-8.

Examples

Basic usage:

use bstr::{B, ByteVec};

let bytes = Vec::from_slice(b"foo\xFFbar");
let err = bytes.into_string().unwrap_err();

assert_eq!(err.utf8_error().valid_up_to(), 3);
assert_eq!(err.utf8_error().error_len(), Some(1));

// At no point in this example is an allocation performed.
let bytes = Vec::from(err.into_vec());
assert_eq!(bytes, B(b"foo\xFFbar"));

Implementations

impl FromUtf8Error

fn as_bytes(self: &Self) -> &[u8]

Return the original bytes as a slice that failed to convert to a String.

Examples

Basic usage:

use bstr::{B, ByteVec};

let bytes = Vec::from_slice(b"foo\xFFbar");
let err = bytes.into_string().unwrap_err();

// At no point in this example is an allocation performed.
assert_eq!(err.as_bytes(), B(b"foo\xFFbar"));
fn into_vec(self: Self) -> Vec<u8>

Consume this error and return the original byte string that failed to convert to a String.

Examples

Basic usage:

use bstr::{B, ByteVec};

let bytes = Vec::from_slice(b"foo\xFFbar");
let err = bytes.into_string().unwrap_err();
let original = err.into_vec();

// At no point in this example is an allocation performed.
assert_eq!(original, B(b"foo\xFFbar"));
fn utf8_error(self: &Self) -> &Utf8Error

Return the underlying UTF-8 error that occurred. This error provides information on the nature and location of the invalid UTF-8 detected.

Examples

Basic usage:

use bstr::{B, ByteVec};

let bytes = Vec::from_slice(b"foo\xFFbar");
let err = bytes.into_string().unwrap_err();

assert_eq!(err.utf8_error().valid_up_to(), 3);
assert_eq!(err.utf8_error().error_len(), Some(1));

impl Debug for FromUtf8Error

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Display for FromUtf8Error

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Eq for FromUtf8Error

impl Error for FromUtf8Error

fn description(self: &Self) -> &str

impl Freeze for FromUtf8Error

impl PartialEq for FromUtf8Error

fn eq(self: &Self, other: &FromUtf8Error) -> bool

impl RefUnwindSafe for FromUtf8Error

impl Send for FromUtf8Error

impl StructuralPartialEq for FromUtf8Error

impl Sync for FromUtf8Error

impl Unpin for FromUtf8Error

impl UnsafeUnpin for FromUtf8Error

impl UnwindSafe for FromUtf8Error

impl<T> Any for FromUtf8Error

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for FromUtf8Error

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for FromUtf8Error

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for FromUtf8Error

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToString for FromUtf8Error

fn to_string(self: &Self) -> String

impl<T, U> Into for FromUtf8Error

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for FromUtf8Error

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for FromUtf8Error

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>