Trait async_std::stream::Product

source ·
pub trait Product<A = Self>: Sized {
    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§

Method which takes a stream and generates Self from the elements by multiplying the items.

Implementations on Foreign Types§

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));

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));

Implementors§