pub trait Product<A = Self>: Sized {
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'a>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
S: Stream<Item = A> + 'a;
}
Expand description
Trait to represent types that can be created by multiplying the elements of a stream.
This trait is used to implement the product
method on streams. Types which
implement the trait can be generated by the product
method. Like
FromStream
this trait should rarely be called directly and instead
interacted with through Stream::product
.
Required methods
Method which takes a stream and generates Self
from the elements by
multiplying the items.
Implementations on Foreign Types
sourceimpl<T, U, E> Product<Result<U, E>> for Result<T, E> where
T: Product<U>,
impl<T, U, E> Product<Result<U, E>> for Result<T, E> where
T: Product<U>,
sourcefn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
S: Stream<Item = Result<U, E>> + 'a,
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
S: Stream<Item = Result<U, E>> + 'a,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Takes each element in the Stream
: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur,
the product of all elements is returned.
Examples
This multiplies every integer in a vector, rejecting the product if a negative element is encountered:
use async_std::prelude::*;
use async_std::stream;
let v = stream::from_iter(vec![1, 2, 4]);
let res: Result<i32, &'static str> = v.map(|x|
if x < 0 {
Err("Negative element found")
} else {
Ok(x)
}).product().await;
assert_eq!(res, Ok(8));
sourceimpl<T, U> Product<Option<U>> for Option<T> where
T: Product<U>,
impl<T, U> Product<Option<U>> for Option<T> where
T: Product<U>,
sourcefn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
S: Stream<Item = Option<U>> + 'a,
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
S: Stream<Item = Option<U>> + 'a,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Takes each element in the Stream
: if it is a None
, no further
elements are taken, and the None
is returned. Should no None
occur,
the product of all elements is returned.
Examples
This multiplies every integer in a vector, rejecting the product if a negative element is encountered:
use async_std::prelude::*;
use async_std::stream;
let v = stream::from_iter(vec![1, 2, 4]);
let prod: Option<i32> = v.map(|x|
if x < 0 {
None
} else {
Some(x)
}).product().await;
assert_eq!(prod, Some(8));