accompany/
lib.rs

1#[macro_export] macro_rules! bound {
2    (with $($patterned_identifier:pat = $expression:expr) , + =>$bound_code:block) => {
3       {
4           // patterned_identifier includes examples like tuple destruction, struct destruction and conventional identifier
5           $(let $patterned_identifier = $expression;)+
6           {$bound_code}
7       }
8    };
9    (with $($patterned_identifier:pat = $expression:expr) , + =>$bound_code:expr) => {
10       {
11           // patterned_identifier includes examples like tuple destruction, struct destruction and conventional identifier
12           $(let $patterned_identifier = $expression;)+
13           {$bound_code}
14       }
15    }
16}
17
18
19#[cfg(test)]
20mod macro_tests {
21    #[derive(Copy, Clone)]
22    struct A {
23        pub field: u8,
24        pub field_u16: u16,
25    }
26
27    struct B {
28        pub field: u8,
29    }
30
31    #[test]
32    fn test_with_bound() {
33        let simple_result = bound! {
34            with i = 3 => {
35                let m = i+1;
36                m
37            }
38        };
39        assert_eq!(4, simple_result);
40
41        let simple_result = bound! {
42            with mut i = 3 => {
43                i += 1;
44                let m = i+1;
45                m
46            }
47        };
48        assert_eq!(5, simple_result);
49
50        let a = A {
51            field: 1,
52            field_u16: 2,
53        };
54        let destruct_result = bound! {
55            with A { field: i, field_u16: _j } = a.clone() =>{
56                let m = i+1;
57                m
58            }
59        };
60        assert_eq!(2, destruct_result);
61
62        let destruct_result = bound! {
63            with A { field: mut i, field_u16: _j } = a =>{
64                i += 1;
65                let m = i+1;
66                m
67            }
68        };
69        assert_eq!(3, destruct_result);
70
71        let detuple_result = bound! {
72            with (mut i,_j)=(4,3) =>{
73                i += 1;
74                let m = i+1;
75                m
76            }
77        };
78        assert_eq!(6, detuple_result);
79    }
80
81    #[test]
82    fn test_with_bound_multiple() {
83        let result = bound! {
84            with i = 1, j=2, k=3 => {
85                i + j +k
86            }
87        };
88        assert_eq!(result, 6);
89        let a = B { field: 1 };
90        let b = B { field: 2 };
91        let result = bound! {
92            with B{ field: i} = a, B{ field: j}= b =>{
93                i+j
94            }
95        };
96        assert_eq!(result, 3);
97    }
98
99    #[test]
100    fn test_with_bound_multiple_and_expression() {
101        let result = bound! {
102            with i = 1, j=2, k=3 => i + j +k
103        };
104        assert_eq!(result, 6);
105        let a = B { field: 1 };
106        let b = B { field: 2 };
107        let result = bound! {
108            with B{ field: i} = a, B{ field: j} = b => i+j
109        };
110        assert_eq!(result, 3);
111    }
112}