Skip to main content

ntex_service/
macros.rs

1/// An implementation of [`crate::Service::ready`] that forwards readiness checks to a field.
2#[macro_export]
3macro_rules! forward_ready {
4    ($st:ty, $field:ident) => {
5        #[inline]
6        async fn ready(&self, ctx: $crate::Ctx<'_, Self, $st>) -> Result<(), Self::Error> {
7            ctx.ready(&self.$field)
8                .await
9                .map_err(::core::convert::Into::into)
10        }
11    };
12    ($st:ty, $field:ident, $err:expr) => {
13        #[inline]
14        async fn ready(&self, ctx: $crate::Ctx<'_, Self, $st>) -> Result<(), Self::Error> {
15            ctx.ready(&self.$field).await.map_err($err)
16        }
17    };
18}
19
20/// An implementation of [`crate::Service::shutdown`] that forwards shutdown checks to a field.
21#[macro_export]
22macro_rules! forward_shutdown {
23    ($st:ty, $field:ident) => {
24        #[inline]
25        async fn shutdown(&self, ctx: $crate::Ctx<'_, Self, $st>) {
26            ctx.shutdown(&self.$field).await
27        }
28    };
29}
30
31/// An implementation of [`crate::Service::ready`] that forwards readiness checks to a
32/// [`crate::Pipeline`] field.
33#[macro_export]
34macro_rules! forward_pl_ready {
35    ($st:ty, $field:ident) => {
36        #[inline]
37        async fn ready(&self, _: $crate::Ctx<'_, Self, $st>) -> Result<(), Self::Error> {
38            self.$field
39                .ready()
40                .await
41                .map_err(::core::convert::Into::into)
42        }
43    };
44    ($st:ty, $field:ident, $err:expr) => {
45        #[inline]
46        async fn ready(&self, _: $crate::Ctx<'_, Self, $st>) -> Result<(), Self::Error> {
47            self.$field.ready().await.map_err($err)
48        }
49    };
50}
51
52/// An implementation of [`crate::Service::shutdown`] that shuts down a
53/// [`crate::Pipeline`] field.
54#[macro_export]
55macro_rules! forward_pl_shutdown {
56    ($st:ty, $field:ident) => {
57        #[inline]
58        async fn shutdown(&self, _: $crate::Ctx<'_, Self, $st>) {
59            self.$field.shutdown().await
60        }
61    };
62}