signals2/
emit.rs

1// Copyright Christian Daley 2021
2// Copyright Frank Mori Hess 2007-2008.
3// Distributed under the Boost Software License, Version 1.0. 
4// See http://www.boost.org/LICENSE_1_0.txt
5
6use std::mem;
7
8use crate::{Signal, EmitHandle};
9use crate::combiner::Combiner;
10
11macro_rules! impl_emit {
12    ($name:ident; $($args:ident)*; $($params:ident)*) => {
13
14        /// Emit trait for signals with slots that accept the corresponding number of arguments. 
15        pub trait $name<R, C, $($args,)*> 
16        where 
17            ($($args,)*): Clone,
18            C: Combiner<R> + 'static
19        {
20            /// The return value of `emit` will be `C::Output` for [Signals](Signal) and `Option<C::Output>` for [EmitHandles](EmitHandle)
21            type Output;
22            /// Executes the signal's underlying slots, passing clones of the given arguments to the slot
23            /// functions. 
24            fn emit(&self, $($params: $args,)*) -> Self::Output;
25        }
26
27        impl<R, C, G, $($args,)*> $name<R, C, $($args,)*> for Signal<($($args,)*), R, C, G> 
28        where 
29            ($($args,)*): Clone,
30            C: Combiner<R> + 'static,
31            G: Ord + Send + Sync
32        {
33            type Output = C::Output;
34
35            fn emit(&self, $($params: $args,)*) -> C::Output {
36                let lock = self.core.read().unwrap();
37                let handle = lock.clone();
38                mem::drop(lock);
39                handle.emit(&($($params,)*))
40            }
41        }
42
43        impl<R, C, G, $($args,)*> $name<R, C, $($args,)*> for EmitHandle<($($args,)*), R, C, G> 
44        where 
45            ($($args,)*): Clone,
46            C: Combiner<R> + 'static,
47            G: Ord + Send + Sync
48        {
49            type Output = Option<C::Output>;
50
51            fn emit(&self, $($params: $args,)*) -> Option<C::Output> {
52                self.weak_sig
53                    .upgrade()
54                    .map(|sig| sig.emit($($params,)*))
55            }
56        }
57    };
58}
59
60impl_emit!(Emit0;;);
61impl_emit!(Emit1; T0; a);
62impl_emit!(Emit2; T0 T1; a b);
63impl_emit!(Emit3; T0 T1 T2; a b c);
64impl_emit!(Emit4; T0 T1 T2 T3; a b c d);
65impl_emit!(Emit5; T0 T1 T2 T3 T4; a b c d e);
66impl_emit!(Emit6; T0 T1 T2 T3 T4 T5; a b c d e f);
67impl_emit!(Emit7; T0 T1 T2 T3 T4 T5 T6; a b c d e f g);
68impl_emit!(Emit8; T0 T1 T2 T3 T4 T5 T6 T7; a b c d e f g h);
69impl_emit!(Emit9; T0 T1 T2 T3 T4 T5 T6 T7 T8; a b c d e f g h i);
70impl_emit!(Emit10; T0 T1 T2 T3 T4 T5 T6 T7 T8 T9; a b c d e f g h i j);
71impl_emit!(Emit11; T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10; a b c d e f g h i j k);
72impl_emit!(Emit12; T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11; a b c d e f g h i j k l);