Changes to Derive(PollVariants) to accept any path for the value type.
This commit is contained in:
@ -5,12 +5,11 @@ use syn::__private::Span;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::token::{Colon, Comma, PathSep, Plus};
|
||||
use syn::{
|
||||
parse_macro_input, parse_quote, parse_quote_spanned, Data, DeriveInput, Expr, GenericParam,
|
||||
Generics, Ident, Lit, Meta, Path, PathSegment, Token, TraitBound, TraitBoundModifier,
|
||||
TypeParam, TypeParamBound,
|
||||
parse_macro_input, parse_quote, parse_quote_spanned, parse_str, Data, DeriveInput, Expr,
|
||||
GenericParam, Generics, Ident, Lit, Meta, Path, PathSegment, Token, TraitBound,
|
||||
TraitBoundModifier, TypeParam, TypeParamBound,
|
||||
};
|
||||
|
||||
// Struct name: Ads1256PollStateful, Ads1256PollPublish, Ads1256PollStatePub
|
||||
#[proc_macro_derive(PollVariants, attributes(value_type))]
|
||||
pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
// ----- Parse input ----------------------------------
|
||||
@ -31,7 +30,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
|
||||
// ----- Extract value type attribute ----------------------------------
|
||||
const VALUE_T_NAME: &str = "value_type";
|
||||
let mut value_type_ident: Option<Ident> = None;
|
||||
let mut value_type_path: Option<Path> = None;
|
||||
for attribute in attrs.iter() {
|
||||
// if the attribute is a named value
|
||||
if let Meta::NameValue(meta) = &attribute.meta {
|
||||
@ -41,25 +40,28 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
if let Expr::Lit(lit) = &meta.value {
|
||||
// if the literal is a string
|
||||
if let Lit::Str(lit) = &lit.lit {
|
||||
value_type_ident = Some(Ident::new(lit.value().deref(), Span::call_site()));
|
||||
let span = lit.span();
|
||||
let string = lit.token().to_string();
|
||||
let mut segments: Punctuated<PathSegment, PathSep> = Punctuated::new();
|
||||
string
|
||||
.trim_matches('"')
|
||||
.split("::")
|
||||
.map(|segment| Ident::new(segment, span))
|
||||
.for_each(|ident| segments.push(parse_quote_spanned!(span=> #ident)));
|
||||
|
||||
let path: Path = parse_quote_spanned!(span=> #segments);
|
||||
value_type_path = Some(path);
|
||||
} else {
|
||||
panic!("{VALUE_T_NAME} must be set with a string literal.")
|
||||
}
|
||||
} else {
|
||||
panic!("{VALUE_T_NAME} must be set with a literal.")
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let value_type_ident = if let Some(val) = value_type_ident {
|
||||
val
|
||||
} else {
|
||||
panic!("Need attribute {VALUE_T_NAME}: #[{VALUE_T_NAME} = \"type\"]")
|
||||
};
|
||||
let value_type_path = value_type_path
|
||||
.expect(format!("Need attribute {VALUE_T_NAME}: #[{VALUE_T_NAME} = \"type\"]").deref());
|
||||
|
||||
// ----- Add publisher generics ----------------------------------
|
||||
// MutexT
|
||||
@ -144,12 +146,12 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
// ----- Stateful struct ----------------------------------
|
||||
#vis struct #stateful_variant_ident #og_generics #og_where_clause {
|
||||
pub poll: #ident #og_type_generics,
|
||||
pub state: #state_path<#value_type_ident>,
|
||||
pub state: #state_path<#value_type_path>,
|
||||
}
|
||||
|
||||
// ----- Stateful impls ----------------------------------
|
||||
impl #og_impl_generics #poll_path for #stateful_variant_ident #og_type_generics #og_where_clause {
|
||||
type Value = #value_type_ident;
|
||||
type Value = #value_type_path;
|
||||
|
||||
#[inline(always)]
|
||||
async fn poll(&self) -> Self::Value {
|
||||
@ -160,7 +162,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
|
||||
impl #og_impl_generics #stateful_path for #stateful_variant_ident #og_type_generics #og_where_clause {
|
||||
type Value = #value_type_ident;
|
||||
type Value = #value_type_path;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> #cellview_path<Self::Value> {
|
||||
@ -177,13 +179,13 @@ 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: #publisher_path<#value_type_ident, #mutex_t_ident, #capacity_ident, #num_subs_ident>,
|
||||
pub publisher: #publisher_path<#value_type_path, #mutex_t_ident, #capacity_ident, #num_subs_ident>,
|
||||
}
|
||||
|
||||
// ----- Publish impl ----------------------------------
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
impl #publ_impl_generics #poll_path for #publish_variant_ident #publ_type_generics #publ_where_clause {
|
||||
type Value = #value_type_ident;
|
||||
type Value = #value_type_path;
|
||||
|
||||
#[inline(always)]
|
||||
async fn poll(&self) -> Self::Value {
|
||||
@ -195,7 +197,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 Value = #value_type_path;
|
||||
type Mutex = #mutex_t_ident;
|
||||
|
||||
#[inline(always)]
|
||||
@ -210,13 +212,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
#[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>,
|
||||
pub state: #state_path<#value_type_path>,
|
||||
pub publisher: #publisher_path<#value_type_path, #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;
|
||||
type Value = #value_type_path;
|
||||
|
||||
#[inline]
|
||||
async fn poll(&self) -> Self::Value {
|
||||
@ -229,7 +231,7 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[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;
|
||||
type Value = #value_type_path;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> #cellview_path<Self::Value> {
|
||||
@ -244,7 +246,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 #state_pub_variant_ident #publ_type_generics #publ_where_clause {
|
||||
type Value = #value_type_ident;
|
||||
type Value = #value_type_path;
|
||||
type Mutex = #mutex_t_ident;
|
||||
|
||||
#[inline(always)]
|
||||
|
Reference in New Issue
Block a user