SubscribeLatestExt

Trait SubscribeLatestExt 

Source
pub trait SubscribeLatestExt<T>: Stream<Item = T> + Sized {
    // Required method
    fn subscribe_latest<'async_trait, F, Fut, E, OnError>(
        self,
        on_next_func: F,
        on_error_callback: OnError,
        cancellation_token: Option<CancellationToken>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where F: Fn(T, CancellationToken) -> Fut + Clone + Send + Sync + 'static + 'async_trait,
             Fut: Future<Output = Result<(), E>> + Send + Sync + 'static + 'async_trait,
             OnError: Fn(E) + Clone + Send + Sync + 'static + 'async_trait,
             E: Send + Sync + 'static + 'async_trait,
             T: Debug + Clone + Send + Sync + 'static,
             Self: 'async_trait;
}
Expand description

Extension trait providing async subscription with automatic cancellation of outdated work.

This trait enables processing stream items where newer items automatically cancel processing of older items, ensuring only the latest value is being processed.

Required Methods§

Source

fn subscribe_latest<'async_trait, F, Fut, E, OnError>( self, on_next_func: F, on_error_callback: OnError, cancellation_token: Option<CancellationToken>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where F: Fn(T, CancellationToken) -> Fut + Clone + Send + Sync + 'static + 'async_trait, Fut: Future<Output = Result<(), E>> + Send + Sync + 'static + 'async_trait, OnError: Fn(E) + Clone + Send + Sync + 'static + 'async_trait, E: Send + Sync + 'static + 'async_trait, T: Debug + Clone + Send + Sync + 'static, Self: 'async_trait,

Subscribes to the stream, automatically cancelling processing of older items when new items arrive.

This method is ideal for scenarios where you only care about processing the most recent value and want to abandon work on outdated values.

§Behavior
  • Only one processing task runs at a time per stream
  • When a new item arrives during processing, it queues as “latest”
  • After current processing completes, the latest queued item is processed
  • Intermediate items between current and latest are discarded
§Arguments
  • on_next_func - Async function called for each item
  • on_error_callback - Error handler for processing failures
  • cancellation_token - Optional token to stop all processing
§See Also
  • subscribe - Sequential processing of all items

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> SubscribeLatestExt<T> for S
where S: Stream<Item = T> + Unpin + Send + Sync + 'static, T: Debug + Clone + Send + Sync + 'static,