1#[macro_export]
59macro_rules! forward {
60 (@go $st:ty; [$field:tt]; map_err = $err:expr $(,)?) => {
61 #[inline]
62 fn bind(&self, ctx: $crate::BindCtx<'_>) {
63 ctx.bind::<_, $st, _>(&self.$field);
64 }
65
66 #[inline]
67 fn ready(
68 &self,
69 ctx: $crate::Ctx<'_, Self, $st>,
70 ) -> impl ::core::future::Future<Output = Result<(), Self::Error>> {
71 $crate::__private::map_output(ctx.ready(&self.$field), |res| res.map_err($err))
72 }
73
74 #[inline]
75 fn shutdown(
76 &self,
77 ctx: $crate::Ctx<'_, Self, $st>,
78 ) -> impl ::core::future::Future<Output = ()> {
79 ctx.shutdown(&self.$field)
80 }
81 };
82 (@go $st:ty; [$($field:tt)+]; map_err = $err:expr $(,)?) => {
83 #[inline]
84 fn bind(&self, ctx: $crate::BindCtx<'_>) {
85 $(ctx.bind::<_, $st, _>(&self.$field);)+
86 }
87
88 fn ready(
89 &self,
90 ctx: $crate::Ctx<'_, Self, $st>,
91 ) -> impl ::core::future::Future<Output = Result<(), Self::Error>> {
92 $crate::__private::map_output(
93 $crate::forward!(@ready ctx, self, $err; $($field)+),
94 |res| res.map(|_| ()),
95 )
96 }
97
98 fn shutdown(
99 &self,
100 ctx: $crate::Ctx<'_, Self, $st>,
101 ) -> impl ::core::future::Future<Output = ()> {
102 $crate::forward!(@shutdown ctx, self; $($field)+)
103 }
104 };
105 (@go $st:ty; []; map_err $($rest:tt)*) => {
106 ::core::compile_error!(
107 "forward!: list at least one service field before `map_err`, e.g. `forward!(St, service, map_err = f)`"
108 );
109 };
110 (@go $st:ty; [$($field:tt)*]; self . $($rest:tt)*) => {
111 ::core::compile_error!(
112 "forward!: write field names without `self.`, e.g. `forward!(St, service)`"
113 );
114 };
115 (@go $st:ty; [$($field:tt)*]; $next:tt . $($rest:tt)*) => {
116 ::core::compile_error!(
117 "forward!: nested field paths are not supported, implement `bind`, `ready`, and `shutdown` manually"
118 );
119 };
120 (@go $st:ty; [$($field:tt)+]; ) => {
121 $crate::forward!(@go $st; [$($field)+]; map_err = ::core::convert::Into::into);
122 };
123 (@go $st:ty; [$($field:tt)*]; $next:tt $(, $($rest:tt)*)?) => {
124 $crate::forward!(@go $st; [$($field)* $next]; $($($rest)*)?);
125 };
126 (@ready $ctx:ident, $this:tt, $err:expr; $field:tt) => {
127 $crate::__private::ready_field($ctx, &$this.$field, $err)
128 };
129 (@ready $ctx:ident, $this:tt, $err:expr; $field:tt $($rest:tt)+) => {
130 $crate::__private::ready_join(
131 $ctx,
132 move |$ctx| $crate::__private::ready_field($ctx, &$this.$field, $err),
133 move |$ctx| $crate::forward!(@ready $ctx, $this, $err; $($rest)+),
134 )
135 };
136 (@shutdown $ctx:ident, $this:tt; $field:tt) => {
137 $ctx.shutdown(&$this.$field)
138 };
139 (@shutdown $ctx:ident, $this:tt; $field:tt $($rest:tt)+) => {
140 $crate::__private::join(
141 $ctx.shutdown(&$this.$field),
142 $crate::forward!(@shutdown $ctx, $this; $($rest)+),
143 )
144 };
145 ($one:tt $(,)?) => {
146 ::core::compile_error!(
147 "forward!: expected a state type and at least one service field, e.g. `forward!(St, service)`"
148 );
149 };
150 ($st:ty, $($rest:tt)+) => {
151 $crate::forward!(@go $st; []; $($rest)+);
152 };
153}
154
155#[doc(hidden)]
161#[macro_export]
162macro_rules! forward_bind {
163 ($st:ty, $($field:tt),+ $(,)?) => {
164 #[inline]
165 fn bind(&self, ctx: $crate::BindCtx<'_>) {
166 $(ctx.bind::<_, $st, _>(&self.$field);)+
167 }
168 };
169 ($st:ty, $this:ident.$field:tt) => {
170 #[inline]
171 fn bind(&$this, ctx: $crate::BindCtx<'_>) {
172 ctx.bind::<_, $st, _>(&$this.$field);
173 }
174 };
175}
176
177#[doc(hidden)]
183#[macro_export]
184macro_rules! forward_ready {
185 ($st:ty, $field:ident) => {
186 $crate::forward_ready!($st, self.$field, ::core::convert::Into::into);
187 };
188 ($st:ty, $field:ident, $err:expr) => {
189 $crate::forward_ready!($st, self.$field, $err);
190 };
191 ($st:ty, $this:ident.$field:tt) => {
192 $crate::forward_ready!($st, $this.$field, ::core::convert::Into::into);
193 };
194 ($st:ty, $this:ident.$field:tt, $err:expr) => {
195 #[inline]
196 fn ready(
197 &$this,
198 ctx: $crate::Ctx<'_, Self, $st>,
199 ) -> impl ::core::future::Future<Output = Result<(), Self::Error>> {
200 $crate::__private::map_output(ctx.ready(&$this.$field), |res| res.map_err($err))
201 }
202 };
203}
204
205#[doc(hidden)]
207#[macro_export]
208macro_rules! forward_shutdown {
209 ($st:ty, $field:ident) => {
210 $crate::forward_shutdown!($st, self.$field);
211 };
212 ($st:ty, $this:ident.$field:tt) => {
213 #[inline]
214 fn shutdown(
215 &$this,
216 ctx: $crate::Ctx<'_, Self, $st>,
217 ) -> impl ::core::future::Future<Output = ()> {
218 ctx.shutdown(&$this.$field)
219 }
220 };
221}