maple_core/
macros.rs

1//! Definition of `cloned!` macro. Proc-macros are defined in the separate `maple-core-macro` crate.
2
3/// Utility macro for cloning all the arguments and expanding the expression.
4///
5/// Temporary workaround for [Rust RFC #2407](https://github.com/rust-lang/rfcs/issues/2407).
6///
7/// # Example
8/// ```
9/// use maple_core::prelude::*;
10///
11/// let state = Signal::new(0);
12///
13/// create_effect(cloned!((state) => move || {
14///    state.get();
15/// }));
16///
17/// // state still accessible outside of the effect
18/// let _ = state.get();
19/// ```
20#[macro_export]
21macro_rules! cloned {
22    (($($arg:ident),*) => $e:expr) => {{
23        // clone all the args
24        $( let $arg = ::std::clone::Clone::clone(&$arg); )*
25
26        $e
27    }};
28}
29
30#[cfg(test)]
31mod tests {
32    use crate::prelude::*;
33
34    #[test]
35    fn cloned() {
36        let state = Signal::new(0);
37
38        let _x = cloned!((state) => state);
39
40        // state still accessible because it was cloned instead of moved
41        let _ = state.get();
42    }
43
44    #[test]
45    fn cloned_closure() {
46        let state = Signal::new(0);
47
48        create_effect(cloned!((state) => move || {
49            state.get();
50        }));
51
52        // state still accessible outside of the effect
53        let _ = state.get();
54    }
55}