pub trait Product<A = Self>: Sized {
// Required method
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'a>>
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§
Object Safety§
Implementations on Foreign Types§
source§impl<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>,
source§fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>
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));
source§impl<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>,
source§fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>
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));