Struct Utf8Chunk

pub struct Utf8Chunk<'a> { pub(in ::str::lossy) valid: &'a str, pub(in ::str::lossy) invalid: &'a [u8] }

An item returned by the Utf8Chunks iterator.

A Utf8Chunk stores a sequence of u8 up to the first broken character when decoding a UTF-8 string.

Examples

// An invalid UTF-8 string
let bytes = b"foo\xF1\x80bar";

// Decode the first `Utf8Chunk`
let chunk = bytes.utf8_chunks().next().unwrap();

// The first three characters are valid UTF-8
assert_eq!("foo", chunk.valid());

// The fourth character is broken
assert_eq!(b"\xF1\x80", chunk.invalid());

Fields

valid: &'a str
invalid: &'a [u8]

Implementations

impl<'a> Utf8Chunk<'a>

fn valid(&self) -> &'a str

Returns the next validated UTF-8 substring.

This substring can be empty at the start of the string or between broken UTF-8 characters.

fn invalid(&self) -> &'a [u8]

Returns the invalid sequence that caused a failure.

The returned slice will have a maximum length of 3 and starts after the substring given by valid. Decoding will resume after this sequence.

If empty, this is the last chunk in the string. If non-empty, an unexpected byte was encountered or the end of the input was reached unexpectedly.

Lossy decoding would replace this sequence with U+FFFD REPLACEMENT CHARACTER.

Trait Implementations

impl<'a> Clone for Utf8Chunk<'a>

fn clone(&self) -> Utf8Chunk<'a>

impl<'a> Debug for Utf8Chunk<'a>

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

impl<'a> Eq for Utf8Chunk<'a>

fn assert_fields_are_eq(&self)

impl<'a> PartialEq for Utf8Chunk<'a>

fn eq(&self, other: &Utf8Chunk<'a>) -> bool

impl<'a> StructuralPartialEq for Utf8Chunk<'a>

Auto Trait Implementations

impl<'a> Freeze for Utf8Chunk<'a>

impl<'a> RefUnwindSafe for Utf8Chunk<'a>

impl<'a> Send for Utf8Chunk<'a>

impl<'a> Sync for Utf8Chunk<'a>

impl<'a> Unpin for Utf8Chunk<'a>

impl<'a> UnsafeUnpin for Utf8Chunk<'a>

impl<'a> UnwindSafe for Utf8Chunk<'a>

Blanket Implementations

impl<T> Any for Utf8Chunk<'a> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Utf8Chunk<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Utf8Chunk<'a> where T: ?Sized,

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

impl<T> CloneToUninit for Utf8Chunk<'a> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Utf8Chunk<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Utf8Chunk<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Utf8Chunk<'a>

impl<T, U> Into<U> for Utf8Chunk<'a> where U: From<T>,

fn into(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<U> for Utf8Chunk<'a> where U: Into<T>,

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

impl<T, U> TryInto<U> for Utf8Chunk<'a> where U: TryFrom<T>,

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