scsys_core/macros/
get.rs

1/*
2    Appellation: get <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6#[macro_export]
7macro_rules! getter {
8    ($($call:ident$(.$field:ident)?<$out:ty>),* $(,)?) => {
9        $($crate::getter!(@impl $call$(.$field)?<$out>);)*
10    };
11    ($via:ident::<[$($call:ident$(.$field:ident)?<$out:ty>),* $(,)?]>) => {
12        $($crate::getter!(@impl $via::$call$(.$field)?<$out>);)*
13    };
14    ($($call:ident$(.$field:ident)?),* $(,)? => $out:ty) => {
15        $($crate::getter!(@impl $call$(.$field)?<$out>);)*
16    };
17    ($via:ident::<[$($call:ident$(.$field:ident)?),* $(,)?]> => $out:ty) => {
18        $crate::getter!($via::<[$($call$(.$field)?<$out>),*]>);
19    };
20
21    (@impl $call:ident<$out:ty>) => {
22        $crate::getter!(@impl $call.$call<$out>);
23    };
24    (@impl $via:ident::$call:ident<$out:ty>) => {
25        $crate::getter!(@impl $via::$call.$call<$out>);
26    };
27    (@impl $call:ident.$field:ident<$out:ty>) => {
28        pub const fn $call(&self) -> &$out {
29            &self.$field
30        }
31        paste::paste! {
32            pub fn [< $call _mut>](&mut self) -> &mut $out {
33                &mut self.$field
34            }
35        }
36    };
37    (@impl $via:ident::$call:ident.$field:ident<$out:ty>) => {
38        /// Returns an immutable reference to
39        pub const fn $call(&self) -> &$out {
40            &self.$via.$field
41        }
42
43        paste::paste! {
44            pub fn [< $call _mut>](&mut self) -> &mut $out {
45                &mut self.$via.$field
46            }
47        }
48    };
49}
50
51macro_rules! impl_getters {
52    (@impl $call:ident<$out:ty>) => {
53        $crate::getter!(@impl $call.$call<$out>);
54    };
55    (@impl $via:ident::$call:ident<$out:ty>) => {
56        $crate::getter!(@impl $via::$call.$call<$out>);
57    };
58    (@impl $call:ident.$field:ident<$out:ty>) => {
59        pub const fn $call(&self) -> &$out {
60            &self.$field
61        }
62        paste::paste! {
63            pub fn [< $call _mut>](&mut self) -> &mut $out {
64                &mut self.$field
65            }
66        }
67    };
68    (@impl $via:ident::$call:ident.$field:ident<$out:ty>) => {
69        /// Returns an immutable reference to
70        pub const fn $call(&self) -> &$out {
71            &self.$via.$field
72        }
73
74        paste::paste! {
75            pub fn [< $call _mut>](&mut self) -> &mut $out {
76                &mut self.$via.$field
77            }
78        }
79    };
80}