macro_rules! impl_shared_supplier_methods {
(@let_supplier Supplier, $name:ident, $value:expr) => {
let $name = $value;
};
(@let_supplier StatefulSupplier, $name:ident, $value:expr) => {
let mut $name = $value;
};
(
$struct_name:ident < $t:ident >,
$supplier_trait:ident,
callback_bounds = ($($callback_bounds:tt)+)
) => {
#[inline]
pub fn map<U, M>(&self, mapper: M) -> $struct_name<U>
where
$t: 'static,
M: Transformer<$t, U> + $($callback_bounds)+,
U: 'static,
{
let metadata = self.metadata.clone();
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
$struct_name::new_with_metadata(
move || {
let value = self_cloned.get();
mapper.apply(value)
},
metadata,
)
}
#[inline]
pub fn filter<P>(&self, predicate: P) -> $struct_name<Option<$t>>
where
$t: 'static,
P: Predicate<$t> + $($callback_bounds)+,
{
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
$struct_name::new(move || {
let value = self_cloned.get();
if predicate.test(&value) {
Some(value)
} else {
None
}
})
}
#[inline]
pub fn zip<U, S>(&self, other: S) -> $struct_name<($t, U)>
where
$t: 'static,
S: $supplier_trait<U> + $($callback_bounds)+,
U: 'static,
{
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
impl_shared_supplier_methods!(@let_supplier $supplier_trait, other, other);
$struct_name::new(move || {
let first = self_cloned.get();
let second = other.get();
(first, second)
})
}
};
(
$struct_name:ident < $t:ident >,
$supplier_trait:ident,
($($extra_bounds:tt)*)
) => {
#[inline]
pub fn map<U, M>(&self, mapper: M) -> $struct_name<U>
where
$t: 'static,
M: Transformer<$t, U> + $($extra_bounds)+,
U: $($extra_bounds)+,
{
let metadata = self.metadata.clone();
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
$struct_name::new_with_metadata(
move || {
let value = self_cloned.get();
mapper.apply(value)
},
metadata,
)
}
#[inline]
pub fn filter<P>(&self, predicate: P) -> $struct_name<Option<$t>>
where
$t: 'static,
P: Predicate<$t> + $($extra_bounds)+,
{
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
$struct_name::new(move || {
let value = self_cloned.get();
if predicate.test(&value) {
Some(value)
} else {
None
}
})
}
#[inline]
pub fn zip<U, S>(&self, other: S) -> $struct_name<($t, U)>
where
$t: 'static,
S: $supplier_trait<U> + $($extra_bounds)+,
U: $($extra_bounds)+,
{
impl_shared_supplier_methods!(@let_supplier $supplier_trait, self_cloned, self.clone());
impl_shared_supplier_methods!(@let_supplier $supplier_trait, other, other);
$struct_name::new(move || {
let first = self_cloned.get();
let second = other.get();
(first, second)
})
}
};
}
pub(crate) use impl_shared_supplier_methods;