pub struct SseStream<T: TryStream> { /* private fields */ }Expand description
An asynchronous stream wrapper that parses SSE events from an underlying byte stream.
Implementations§
Source§impl<T: TryStream> SseStream<T>
impl<T: TryStream> SseStream<T>
Sourcepub fn disconnected() -> Self
pub fn disconnected() -> Self
Creates a new, disconnected SseStream.
A disconnected stream will immediately yield None (terminated) if polled.
This constructor is primarily useful when you need to store the SseStream
inside a struct before the network connection is established.
To make the stream active, you must attach an inner stream using
attach().
§Example
let mut stream = SseStream::disconnected();
// ... later, when the network is ready:
let byte_stream = fetch_http_stream().await;
stream.attach(byte_stream);Sourcepub fn with_decoder(decoder: SseDecoder) -> Self
pub fn with_decoder(decoder: SseDecoder) -> Self
Creates a disconnected stream initialized with a custom decoder.
See the disconnected() function for more information.
Sourcepub fn take_decoder(self) -> SseDecoder
pub fn take_decoder(self) -> SseDecoder
Consumes the stream and returns the underlying state-machine decoder.
Sourcepub fn last_event_id(&self) -> Option<&Arc<str>>
pub fn last_event_id(&self) -> Option<&Arc<str>>
Returns the current Last-Event-ID parsed by the underlying decoder.
Sourcepub fn close(&mut self)
pub fn close(&mut self)
Disconnects the inner stream while retaining the underlying parser’s state.
This drops the active network connection but safely preserves the most
recently parsed Last-Event-ID within the decoder. This is the standard
method to temporarily pause a stream or handle a dropped connection,
allowing you to later resume exactly where you left off.
- To close the stream and inject a new ID for the next connection, use
close_with_id(). - To close the stream and completely wipe the session state, use
close_and_clear().
Sourcepub fn close_and_clear(&mut self)
pub fn close_and_clear(&mut self)
Disconnects the stream and completely purges the underlying parser’s state.
This drops the inner stream, clears all internal byte buffers, and
permanently drops the currently tracked Last-Event-ID. It effectively
returns the SseStream to the exact state it was in when initially
created via disconnected().
- To close the stream and keep the current ID, use
close(). - To close the stream and inject a new ID, use
close_with_id().
Sourcepub fn close_with_id(&mut self, id: Option<Arc<str>>)
pub fn close_with_id(&mut self, id: Option<Arc<str>>)
Disconnects the inner stream and explicitly overrides the underlying
decoder’s Last-Event-ID in preparation for a future connection.
This is particularly useful in async contexts where you must drop the
active stream, inject a new ID, and then yield back to the runtime before
establishing a new network connection. The injected ID will be available
immediately via last_event_id().
- To close the stream and keep the current ID, use
close(). - To close the stream and completely wipe the session state, use
close_and_clear().
Sourcepub fn attach(&mut self, inner: T)
pub fn attach(&mut self, inner: T)
Attaches a new inner stream to resume processing events.
This method resets the underlying parser’s buffers but safely retains the most
recently parsed Last-Event-ID. It is the standard way to recover from
a dropped network connection, allowing you to resume exactly where you left off.
- To attach a stream and inject a new ID, use
attach_with_id(). - To attach a stream and completely wipe the session state, use
clear_and_attach().
Sourcepub fn clear_and_attach(&mut self, inner: T)
pub fn clear_and_attach(&mut self, inner: T)
Attaches a new inner stream and completely purges the underlying parser’s state.
This method is used when you want to reuse an existing SseStream allocation
for a completely fresh connection or a different server. It clears all internal
byte buffers and permanently drops the currently tracked Last-Event-ID.
- To attach a stream and keep the current ID, use
attach(). - To attach a stream and inject a new ID, use
attach_with_id().
Sourcepub fn attach_with_id(&mut self, inner: T, id: Option<Arc<str>>)
pub fn attach_with_id(&mut self, inner: T, id: Option<Arc<str>>)
Attaches a new inner stream to resume processing, explicitly overriding
the Last-Event-ID in the underlying decoder.
This method is primarily used when recovering an offline session where you need to initialize the stream with a saved ID (e.g., from a local database) right as you provide the new HTTP response stream.
- To attach a stream and keep the current ID, use
attach(). - To attach a stream and completely wipe the session state, use
clear_and_attach().
Trait Implementations§
Source§impl<T: TryStream> FusedStream for SseStream<T>
impl<T: TryStream> FusedStream for SseStream<T>
Source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true if the stream should no longer be polled.