pub struct Utf8StreamDecoder { /* private fields */ }Expand description
Incrementally decodes a byte stream into UTF-8 text.
Chunks can split a multibyte code point (CJK, emoji, accented characters,
smart quotes) across boundaries. Decoding each chunk independently with
String::from_utf8_lossy corrupts such code points into U+FFFD
replacement characters. This decoder buffers any trailing incomplete
sequence until the rest of its bytes arrive, while still replacing genuinely
invalid bytes with U+FFFD (matching from_utf8_lossy semantics). Call
Utf8StreamDecoder::finish at end of stream so a truncated final sequence
becomes U+FFFD instead of vanishing.
Implementations§
Source§impl Utf8StreamDecoder
impl Utf8StreamDecoder
pub fn new() -> Utf8StreamDecoder
Sourcepub fn push(&mut self, bytes: &[u8]) -> String
pub fn push(&mut self, bytes: &[u8]) -> String
Appends bytes and returns the decodable UTF-8 prefix. A trailing
incomplete multibyte sequence is retained for the next call.
Sourcepub fn push_cow<'a>(&mut self, bytes: &'a [u8]) -> Cow<'a, str>
pub fn push_cow<'a>(&mut self, bytes: &'a [u8]) -> Cow<'a, str>
Like Utf8StreamDecoder::push, but borrows bytes without copying when
nothing is pending and the chunk is valid UTF-8 (the common case for
process output), so hot read loops avoid a per-chunk allocation.
Sourcepub fn push_bytes(&mut self, bytes: &[u8], out: &mut Vec<u8>)
pub fn push_bytes(&mut self, bytes: &[u8], out: &mut Vec<u8>)
Appends bytes and writes the decodable UTF-8 prefix directly into
out. A trailing incomplete multibyte sequence is retained for the
next call.
This avoids the intermediate String allocation that push creates
when the caller only needs bytes (e.g. feeding an SSE byte buffer).
Trait Implementations§
Source§impl Debug for Utf8StreamDecoder
impl Debug for Utf8StreamDecoder
Source§impl Default for Utf8StreamDecoder
impl Default for Utf8StreamDecoder
Source§fn default() -> Utf8StreamDecoder
fn default() -> Utf8StreamDecoder
Auto Trait Implementations§
impl Forget for Utf8StreamDecoder
impl Freeze for Utf8StreamDecoder
impl RefUnwindSafe for Utf8StreamDecoder
impl Send for Utf8StreamDecoder
impl Sync for Utf8StreamDecoder
impl Unpin for Utf8StreamDecoder
impl UnsafeUnpin for Utf8StreamDecoder
impl UnwindSafe for Utf8StreamDecoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more