From 456fbba236f3217fb81dbbe55132da644be5b958 Mon Sep 17 00:00:00 2001 From: Zachary Sunforge Date: Tue, 20 Jun 2023 19:33:03 -0700 Subject: [PATCH] Device macro to generate stateful and publish variants of device specific poll inputs. --- macros/node-poll-variants/src/lib.rs | 60 ++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/macros/node-poll-variants/src/lib.rs b/macros/node-poll-variants/src/lib.rs index b8e4015..4b1dcfc 100644 --- a/macros/node-poll-variants/src/lib.rs +++ b/macros/node-poll-variants/src/lib.rs @@ -138,11 +138,15 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { let stateful_path: Path = parse_quote!(physical_node::transducer::Stateful); let publish_path: Path = parse_quote!(physical_node::transducer::Publish); + let state_path: Path = parse_quote!(physical_node::transducer::State); + let publisher_path: Path = parse_quote!(physical_node::transducer::Publisher); + let cellview_path: Path = parse_quote!(physical_node::cell::CellView); + let expanded = quote! { // ----- Stateful struct ---------------------------------- #vis struct #stateful_variant_ident #og_generics #og_where_clause { pub poll: #ident #og_type_generics, - pub state: physical_node::transducer::State<#value_type_ident>, + pub state: #state_path<#value_type_ident>, } // ----- Stateful impls ---------------------------------- @@ -161,7 +165,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { type Value = #value_type_ident; #[inline(always)] - fn state_cell(&self) -> physical_node::cell::CellView { + fn state_cell(&self) -> #cellview_path { self.state.state_cell() } @@ -175,7 +179,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { //#[cfg(feature = "embassy-sync")] #vis struct #publish_variant_ident #publish_generics #publ_where_clause { pub poll: #ident #og_type_generics, - pub publisher: physical_node::transducer::Publisher<#value_type_ident, #mutex_t_ident, #capacity_ident, #num_subs_ident>, + pub publisher: #publisher_path<#value_type_ident, #mutex_t_ident, #capacity_ident, #num_subs_ident>, } // ----- Publish impl ---------------------------------- @@ -191,6 +195,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { } } + //#[cfg(feature = "embassy-sync")] impl #publ_impl_generics #publish_path<#capacity_ident, #num_subs_ident> for #publish_variant_ident #publ_type_generics #publ_where_clause { type Value = #value_type_ident; type Mutex = #mutex_t_ident; @@ -204,8 +209,55 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { } // ----- StatePub struct ---------------------------------- + //#[cfg(feature = "embassy-sync")] + #vis struct #state_pub_variant_ident #publish_generics #publ_where_clause { + pub poll: #ident #og_type_generics, + pub state: #state_path<#value_type_ident>, + pub publisher: #publisher_path<#value_type_ident, #mutex_t_ident, #capacity_ident, #num_subs_ident>, + } + + //#[cfg(feature = "embassy-sync")] + impl #publ_impl_generics #poll_path for #state_pub_variant_ident #publ_type_generics #publ_where_clause { + type Value = #value_type_ident; + + #[inline] + async fn poll(&self) -> Self::Value { + let value = self.poll.poll().await; + self.state.update(value); + self.publisher.update(value); + value + } + } + + //#[cfg(feature = "embassy-sync")] + impl #publ_impl_generics #stateful_path for #state_pub_variant_ident #publ_type_generics #publ_where_clause { + type Value = #value_type_ident; + + #[inline(always)] + fn state_cell(&self) -> #cellview_path { + self.state.state_cell() + } + + #[inline(always)] + fn state(&self) -> Self::Value { + self.state.state() + } + } + + //#[cfg(feature = "embassy-sync")] + impl #publ_impl_generics #publish_path<#capacity_ident, #num_subs_ident> for #state_pub_variant_ident #publ_type_generics #publ_where_clause { + type Value = #value_type_ident; + type Mutex = #mutex_t_ident; + + #[inline(always)] + fn subscribe( + &self, + ) -> Result<#pubsub_sub_path, #pubsub_error_path> { + self.publisher.subscribe() + } + } + - // ----- StatePub impl ---------------------------------- }; TokenStream::from(expanded)