Skip to main content

SseParser

Struct SseParser 

Source
pub struct SseParser { /* private fields */ }
Expand description

Sans-IO incremental SSE parser.

Feed bytes with feed, drain completed events with next_event, and call finish at end of stream. Chunk boundaries never affect the output: any chunking of a byte stream yields the same events as feeding it whole.

Time is linear in the bytes fed. Each byte is scanned once for a line terminator and decoded once as part of its completed line, whatever the chunk size. Memory is proportional to the longest block in flight unless max_buffer caps it.

Implementations§

Source§

impl SseParser

Source

pub fn new() -> Self

A parser with an empty last event ID string and no buffer limit.

Source

pub fn with_last_event_id(id: impl Into<String>) -> Self

A parser seeded with a last event ID string, for reconnection.

Events dispatched before the stream’s first id field report this ID.

Source

pub fn max_buffer(self, limit: usize) -> Self

Cap the bytes held for one block.

A block overflows when the raw bytes of one of its lines, before the terminator, plus the bytes already in the block’s data and event buffers exceed limit, or when those buffers exceed limit after a line is decoded (invalid UTF-8 expands to the three-byte U+FFFD). The checks run on every completed line and on the unterminated line after every feed, so the outcome is the same for every chunking of the stream, and after each feed the parser holds at most limit bytes plus one chunk.

On overflow the parser drops the block and ignores every following line up to the next blank line, where it resynchronizes. Fields processed before the overflow keep their effect: an id or retry seen earlier in the block stands, and the blank line still commits the last event ID. overflows counts dropped blocks. The default is unlimited.

Source

pub fn overflows(&self) -> u64

Number of blocks dropped because they exceeded the max_buffer cap.

Source

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

Feed one chunk of bytes.

Every byte sequence is a valid event stream, so there is nothing to reject. tests/no_panic.rs and the fuzz target feed arbitrary bytes in arbitrary chunkings.

Feeding after finish is a contract violation: it trips a debug assertion and is ignored in release builds.

Source

pub fn next_event(&mut self) -> Option<Event>

Next completed event, in stream order, if any.

Source

pub fn finish(&mut self)

Signal end of stream.

A block with no closing blank line is discarded, per the WHATWG algorithm. Events completed before the end stay available through next_event. Calling this twice is harmless.

Source

pub fn last_event_id(&self) -> &str

The last event ID string: the value a client sends back as Last-Event-ID on reconnection.

Committed at every blank line, including blocks that dispatch no event.

Source

pub fn retry(&self) -> Option<Duration>

Reconnection time from the most recent valid retry field, if any.

Updated when the field is processed, whether or not its block dispatches an event.

Trait Implementations§

Source§

impl Clone for SseParser

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SseParser

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for SseParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.