Skip to main content

SseStreamExt

Trait SseStreamExt 

Source
pub trait SseStreamExt<T, E>: Stream<Item = Result<T, E>> + Sized
where E: Into<Error>,
{ // Provided method fn cast_events<F>( self, f: F, ) -> impl Stream<Item = Result<Event, Error>> + Send where F: FnMut(T) -> Result<Event, Error> + Send, T: Send, E: Send, Self: Send { ... } }
Expand description

Extension trait for converting streams into SSE event streams.

Operates on streams of Result<T, E> — errors pass through unchanged, and the conversion closure only operates on Ok values.

§Usage

Import the trait and call cast_events on any compatible stream:

use modo::sse::{Broadcaster, Event, SseConfig, SseStreamExt};

let bc: Broadcaster<String, Item> = Broadcaster::new(64, SseConfig::default());
let key = "topic".to_string();
let stream = bc.subscribe(&key).cast_events(|item| {
    Event::new(modo::id::short(), "update")?.json(&item)
});

Provided Methods§

Source

fn cast_events<F>(self, f: F) -> impl Stream<Item = Result<Event, Error>> + Send
where F: FnMut(T) -> Result<Event, Error> + Send, T: Send, E: Send, Self: Send,

Map each item to an Event with a custom closure.

Errors from the source stream pass through converted via Into<Error>. Errors returned by the closure also propagate.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<S, T, E> SseStreamExt<T, E> for S
where S: Stream<Item = Result<T, E>> + Sized, E: Into<Error>,