Skip to main content

SseDecoder

Struct SseDecoder 

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

The core state-machine parser for SSE.

This decoder does not perform any I/O. It consumes bytes from a given buffer and yields parsed SseEvents. It is suitable for no_std environments.

Implementations§

Source§

impl SseDecoder

Source

pub fn new() -> Self

Creates a new decoder with the default payload size limit of 512KiB.

§Example
let mut decoder = SseDecoder::new();
let mut buf = Bytes::from("data: standard stream\n\n");

let event = decoder.next(&mut buf).transpose()?;
assert!(event.is_some());
Source

pub fn with_limit(max_payload_size: NonZeroUsize) -> Self

Creates a new decoder with a custom maximum payload size limit.

This is useful in memory-constrained environments or when connecting to untrusted servers to prevent memory exhaustion from infinitely long lines.

§Example
// Create a strict decoder that rejects payloads over 1024 bytes
let limit = NonZeroUsize::new(1024).unwrap();
let mut decoder = SseDecoder::with_limit(limit);

let mut buf = Bytes::from("data: small payload\n\n");
let Some(event) = decoder.next(&mut buf) else {
    panic!();
};
assert!(event.is_ok());
Source

pub fn last_event_id(&self) -> Option<&Arc<str>>

Returns the current Last-Event-ID known to the decoder, if any.

Source

pub fn reconnect_with_id(&mut self, id: Option<Arc<str>>)

Resets the decoder state for a new connection, explicitly overriding the currently tracked Last-Event-ID.

This method clears all internal byte buffers and resets the parser, but instead of keeping the previous ID (like reconnect()) or dropping it (like clear()), it injects the provided ID.

It is typically used to prime the state machine with a known ID (e.g., from a local database) right before feeding the decoder bytes from a newly established connection.

Source

pub fn clear(&mut self)

Resets the decoder state completely, dropping the current Last-Event-ID.

This clears all internal byte buffers and purges the parser’s state, effectively starting fresh. Because it drops the Last-Event-ID, the next connection will start from the present moment rather than resuming.

Source

pub fn reconnect(&mut self)

Resets the buffer state for a new connection while retaining the Last-Event-ID.

This clears the internal byte buffers to prepare for a fresh stream of data, but safely preserves the most recently parsed Last-Event-ID. This ensures that when you reconnect to the server, you can resume exactly where you left off.

Source

pub fn next( &mut self, buf: &mut impl Buf, ) -> Option<Result<SseEvent, PayloadTooLargeError>>

Consumes bytes from the provided buffer and attempts to yield an event.

The decoder does not store unparsed bytes internally. It reads directly from the provided buffer, advancing the buffer’s cursor only for the bytes it successfully parses.

If Ok(None) is returned, the provided buffer has been exhausted and more bytes are needed to complete the current event. You should fetch more data, append it to your buffer, and call next() again.

§Example
use bytes::{Buf, Bytes};

let mut decoder = SseDecoder::new();
let mut buffer = Bytes::from("data: hello\n\n");

// Call next() in a loop to drain all available events
while let Some(event) = decoder.next(&mut buffer) {
    println!("Received: {event:?}");
}

// When next() returns None, the decoder is waiting for more data.
assert!(buffer.is_empty());
§Errors

Returns a PayloadTooLargeError if a single field (like data or event name) exceeds the maximum payload size limit configured for this decoder.

Trait Implementations§

Source§

impl Clone for SseDecoder

Source§

fn clone(&self) -> SseDecoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SseDecoder

Source§

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

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

impl Default for SseDecoder

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 = 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.