utf8

Struct LossyDecoder

Source
pub struct LossyDecoder<F: FnMut(&str)> { /* private fields */ }
Expand description

A push-based, lossy decoder for UTF-8. Errors are replaced with the U+FFFD replacement character.

Users “push” bytes into the decoder, which in turn “pushes” &str slices into a callback.

For example, String::from_utf8_lossy (but returning String instead of Cow) can be rewritten as:

fn string_from_utf8_lossy(input: &[u8]) -> String {
    let mut string = String::new();
    utf8::LossyDecoder::new(|s| string.push_str(s)).feed(input);
    string
}

Note: Dropping the decoder signals the end of the input: If the last input chunk ended with an incomplete byte sequence for a code point, this is an error and a replacement character is emitted. Use std::mem::forget to inhibit this behavior.

Implementations§

Source§

impl<F: FnMut(&str)> LossyDecoder<F>

Source

pub fn new(push_str: F) -> Self

Create a new decoder from a callback.

Source

pub fn feed(&mut self, input: &[u8])

Feed one chunk of input into the decoder.

The input is decoded lossily and the callback called once or more with &str string slices.

If the UTF-8 byte sequence for one code point was split into this bytes chunk and previous bytes chunks, it will be correctly pieced back together.

Trait Implementations§

Source§

impl<F: FnMut(&str)> Drop for LossyDecoder<F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<F> Freeze for LossyDecoder<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for LossyDecoder<F>
where F: RefUnwindSafe,

§

impl<F> Send for LossyDecoder<F>
where F: Send,

§

impl<F> Sync for LossyDecoder<F>
where F: Sync,

§

impl<F> Unpin for LossyDecoder<F>
where F: Unpin,

§

impl<F> UnwindSafe for LossyDecoder<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.