async_std/stream/
product.rs1use core::pin::Pin;
2use core::future::Future;
3
4use crate::stream::Stream;
5
6#[cfg(feature = "unstable")]
17#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
18pub trait Product<A = Self>: Sized {
19 fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'a>>
22 where
23 S: Stream<Item = A> + 'a;
24}
25
26use core::ops::Mul;
27use core::num::Wrapping;
28use crate::stream::stream::StreamExt;
29
30macro_rules! num_product {
31 ($one:expr, $($a:ty)*) => ($(
32 impl Product for $a {
33 fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self>+ 'a>>
34 where
35 S: Stream<Item = $a> + 'a,
36 {
37 Box::pin(async move { stream.fold($one, Mul::mul).await } )
38 }
39 }
40 impl<'a> Product<&'a $a> for $a {
41 fn product<'b, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'b>>
42 where
43 S: Stream<Item = &'a $a> + 'b,
44 {
45 Box::pin(async move { stream.fold($one, Mul::mul).await } )
46 }
47 }
48 )*);
49}
50
51macro_rules! integer_product {
52 ($($a:ty)*) => (
53 num_product!(1, $($a)*);
54 num_product!(Wrapping(1), $(Wrapping<$a>)*);
55 );
56}
57
58macro_rules! float_product {
59 ($($a:ty)*) => (
60 num_product!(1.0, $($a)*);
61 );
62}
63
64integer_product!{ i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
65float_product!{ f32 f64 }