Trait future_queue::StreamExt

source ·
pub trait StreamExt: Stream {
    // Provided methods
    fn future_queue<Fut>(self, max_weight: usize) -> FutureQueue<Self>
       where Self: Sized + Stream<Item = (usize, Fut)>,
             Fut: Future { ... }
    fn future_queue_grouped<Fut, I, K, Q>(
        self,
        max_global_weight: usize,
        groups: I,
    ) -> FutureQueueGrouped<Self, K>
       where I: IntoIterator<Item = (K, usize)>,
             K: Eq + Hash + Borrow<Q> + Debug,
             Q: Eq + Hash + Debug,
             Self: Sized + Stream<Item = (usize, Option<Q>, Fut)>,
             Fut: Future { ... }
}
Expand description

An extension trait for Streams that provides future_queue.

Provided Methods§

source

fn future_queue<Fut>(self, max_weight: usize) -> FutureQueue<Self>
where Self: Sized + Stream<Item = (usize, Fut)>, Fut: Future,

An adaptor for creating a queue of pending futures (unordered), where each future has a different weight.

This stream must return values of type (usize, impl Future), where the usize indicates the weight of each future. This adaptor will buffer futures up to weight max_weight, and then return the outputs in the order in which they complete.

  • The maximum weight is never exceeded while futures are being run.
  • If the weight of an individual future is greater than the maximum weight, its weight will be set to the maximum weight.

The adaptor will schedule futures in the order they’re returned by the stream, without doing any reordering based on weight.

The weight of a future can be zero, in which case it will not count towards the total weight.

The returned stream will be a stream of each future’s output.

§Examples

See the crate documentation for an example.

source

fn future_queue_grouped<Fut, I, K, Q>( self, max_global_weight: usize, groups: I, ) -> FutureQueueGrouped<Self, K>
where I: IntoIterator<Item = (K, usize)>, K: Eq + Hash + Borrow<Q> + Debug, Q: Eq + Hash + Debug, Self: Sized + Stream<Item = (usize, Option<Q>, Fut)>, Fut: Future,

An adaptor for creating a queue of pending futures, where each future has a different weight and optional group.

This method accepts a maximum global weight, as well as a set of groups of type K. Each group has a defined maximum weight. This stream must return values of type (usize, Option<Q>, impl Future), where K is Borrow<Q>.

This adapter will buffer futures up to weight max_weight. If the optional group is specified for a future, it will also check that the weight of futures in that group does not exceed the specified limit. Any futures that exceed the group’s weight limit will be queued up, but not scheduled until the weight of futures in that group falls below the limit.

Like with future_queue:

  • The maximum global and group weights is never exceeded while futures are being run.
  • While accounting against global weights, if the weight of an individual future is greater than the maximum weight, its weight will be set to the maximum weight.
  • If a future belongs to a group: While accounting against the group weight, if its weight is greater than the maximum group weight, its weight will be set to the maximum group weight.

The adaptor is as fair as possible under the given constraints: it will schedule futures in the order they’re returned by the stream, without doing any reordering based on weight. When a future from a group completes, queued up futures in this group will be preferentially scheduled before any other futures from the provided stream.

The weight of a future can be zero, in which case it will not count towards the total weight.

The returned stream will be a stream of each future’s output.

§Panics

The stream panics if the optional group provided by a stream element isn’t in the set of known groups.

Implementors§

source§

impl<T> StreamExt for T
where T: Stream + ?Sized,