1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
crate::do_impl!("apply", tuple_apply, {
/// The resulting type when F is called with Tpl's elements.
///
/// See also: [apply()], [TupleApply].
#[cfg_attr(docsrs, doc(cfg(feature = "apply")))]
pub type Apply<F, Tpl> = <(F, Tpl) as TupleApply<F, Tpl>>::Type;
/// Call a function with the tuples members as arguments.
///
/// The function F can be <code>&[Fn]</code> or <code>&mut [FnMut]</code>.
///
/// ```
/// use tupleops::apply;
///
/// fn add3(a: u32, b: u32, c: u32) -> u32 {
/// a + b + c
/// }
///
/// let tpl3 = (1, 2, 3);
/// assert_eq!(apply(&add3, tpl3), 6);
/// ```
///
/// ```
/// use tupleops::apply;
///
/// let mut counter = 0;
/// let mut prefixsum3 = |a, b, c| {
/// counter += a + b + c;
/// counter
/// };
///
/// assert_eq!(apply(&mut prefixsum3, (1, 2, 3)), 6);
/// assert_eq!(apply(&mut prefixsum3, (4, 5, 6)), 21);
/// assert_eq!(apply(&mut prefixsum3, (7, 8, 9)), 45);
/// ```
///
/// See also: [Apply], [TupleApply].
#[cfg_attr(docsrs, doc(cfg(feature = "apply")))]
#[inline(always)]
pub fn apply<F, Tpl>(func: F, tpl: Tpl) -> Apply<F, Tpl>
where
(F, Tpl): TupleApply<F, Tpl>,
{
<(F, Tpl) as TupleApply<F, Tpl>>::apply(func, tpl)
}
/// A function and a tuple that are usable with [apply()].
///
/// See also: [apply()], [Apply].
#[cfg_attr(docsrs, doc(cfg(feature = "apply")))]
pub trait TupleApply<F, Tpl> {
#[doc(hidden)]
type Type;
#[doc(hidden)]
fn apply(func: F, tpl: Tpl) -> Self::Type;
}
impl<F> TupleApply<&F, ()> for (&F, ()) {
type Type = ();
fn apply(_func: &F, _tpl: ()) -> Self::Type {}
}
impl<F> TupleApply<&mut F, ()> for (&mut F, ()) {
type Type = ();
fn apply(_func: &mut F, _tpl: ()) -> Self::Type {}
}
});