Device macro to generate stateful and publish variants of device specific poll inputs.

This commit is contained in:
Zachary Sunforge
2023-06-20 19:33:03 -07:00
parent 996bc66431
commit 456fbba236

View File

@ -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<Self::Value> {
fn state_cell(&self) -> #cellview_path<Self::Value> {
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::Value> {
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<Self::Mutex, Self::Value, #capacity_ident, #num_subs_ident, 0>, #pubsub_error_path> {
self.publisher.subscribe()
}
}
// ----- StatePub impl ----------------------------------
};
TokenStream::from(expanded)