bios_basic/spi/
macros.rs

1//! SPI macros
2
3#[macro_export]
4macro_rules! spi_service_call {
5    ($mod:path, $fun:ident, $funs:ident, $ctx:ident, $inst:ident, @args: {$($args: ident),*}) => {
6        {
7            use $mod::*;
8            $fun($($args,)* $funs, $ctx, $inst).await
9        }
10    };
11}
12
13#[macro_export]
14macro_rules! spi_dispatch_function {
15    (
16        $service:ident,
17        $funs:ident, $ctx:ident, $inst:ident,
18        @dispatch: {
19            $(
20                $(#[$attr:meta])*
21                $code:pat=>$mod:path,
22            )*
23        },
24        @args: $args: tt
25    ) => {
26        match $inst.kind_code() {
27            $(
28                $(#[$attr])*
29                $code => $crate::spi_service_call!($mod, $service, $funs, $ctx, $inst, @args: $args),
30            )*
31            kind_code => Err($funs.bs_not_implemented(kind_code)),
32        }
33
34    };
35}
36
37/// SPI request dispatch macro
38///
39/// SPI请求分发宏
40///
41/// Used to generate request dispatch code in the SPI service
42///
43/// 用于批量生成请求分发代码
44#[macro_export]
45macro_rules! spi_dispatch_service {
46    (
47        /// Whether it is a managed request
48        /// 是否是管理模式
49        @mgr: $mgr: expr,
50        /// Backend service initialization method, called when the backend service instance is not initialized
51        ///
52        /// 后端服务初始化方法,当后端服务实例未初始化时调用
53        @init: $init: expr,
54        /// Service object to dispatch to
55        ///
56        /// 分发到的服务对象
57        @dispatch: $dispatch:tt,
58        /// Collection of request methods
59        ///
60        /// 请求的方法集合
61        @method: {
62            $(
63                $(#[$attr:meta])*
64                $service:ident($($arg: ident: $type: ty),*) -> $ret:ty;
65            )*
66        }
67
68    ) => {
69        $(
70            $(#[$attr])*
71            pub async fn $service($($arg: $type,)* funs: &tardis::TardisFunsInst, ctx: &tardis::basic::dto::TardisContext) -> $ret {
72                let arc_inst = funs.init(None, ctx, $mgr, $init).await?;
73                let inst = arc_inst.as_ref();
74                $crate::spi_dispatch_service!(@dispatch $service, funs, ctx, inst, @dispatch: $dispatch, @args: {$($arg),*})
75            }
76        )*
77    };
78    (@dispatch
79        $service:ident,
80        $funs:ident, $ctx:ident, $inst:ident,
81        @dispatch: {
82            $(
83                $(#[$attr:meta])*
84                $code:pat=>$mod:path,
85            )*
86        },
87        @args: $args: tt
88    ) => {
89        match $inst.kind_code() {
90            $(
91                $(#[$attr])*
92                $code => $crate::spi_dispatch_service!(@call $mod, $service, $funs, $ctx, $inst, @args: $args),
93            )*
94            kind_code => Err($funs.bs_not_implemented(kind_code)),
95        }
96    };
97    (@call
98        $mod:path, $fun:ident, $funs:ident, $ctx:ident, $inst:ident, @args: {$($args: ident),*}) => {
99        {
100            use $mod::*;
101            $fun($($args,)* $funs, $ctx, $inst).await
102        }
103    };
104}