Skip to content

Commit

Permalink
fix support for lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
valsteen committed Dec 21, 2023
1 parent d506e13 commit 3c6eb4f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/enum_variant_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use either::Either;
use proc_macro2::{Delimiter, Ident, Span, TokenStream, TokenTree};
use quote::{quote_spanned, ToTokens, TokenStreamExt};
use syn::token::Mut;
use syn::{
self, spanned::Spanned, Attribute, Data, DeriveInput, Error, Expr, ExprParen, ExprPath, Fields, FieldsNamed, Meta,
MetaList, Token, Type, TypeReference, Variant,
};
use syn::{self, spanned::Spanned, Attribute, Data, DeriveInput, Error, Expr, ExprParen, ExprPath, Fields, FieldsNamed, Meta, MetaList, Token, Type, TypeReference, Variant, GenericParam};

const ATTR_HELP: &str = "EnumAccessor: Invalid accessor declaration, expected #[accessor(field1: type, except(VariantWithoutAccessor1,VariantWithoutAccessor2))]";
const ENUM_HELP: &str = "EnumAccessor: only variants with one unnamed parameter, unit and named variants are supported";
Expand Down Expand Up @@ -445,11 +442,21 @@ pub fn impl_enum_accessor(input: DeriveInput) -> TokenStream {

let generics = &input.generics;
let where_clause = &input.generics.where_clause;
let generics_params = &input.generics.params.to_token_stream();

let generics_params = &input
.generics
.params
.iter()
.map(|p| match p {
GenericParam::Type(t) => t.ident.to_token_stream(),
GenericParam::Const(t) => t.ident.to_token_stream(),
GenericParam::Lifetime(t) => t.to_token_stream(),
})
.collect::<Vec<_>>();

syn::parse_quote_spanned! {input_span =>
#[allow(dead_code)]
impl #generics #ident <#generics_params> #where_clause {
impl #generics #ident <#(#generics_params),*> #where_clause {
#(#accessor_impls)*
}
}
Expand Down Expand Up @@ -563,10 +570,10 @@ impl SomeEnum {
fn test_lifetimes() {
let input = syn::parse_quote! {
#[accessor(acc1: usize, (B))]
enum SomeEnum<'a> {
A(a),
enum SomeEnum<'a, Get: Fn() -> u8> {
A(A),
B,
C(b<'a>)
C(&'a Get)
}
};

Expand All @@ -576,7 +583,7 @@ impl SomeEnum {
assert_eq!(
output,
r"#[allow(dead_code)]
impl<'a> SomeEnum<'a> {
impl<'a, Get: Fn() -> u8> SomeEnum<'a, Get> {
pub fn acc1(&self) -> std::option::Option<&usize> {
match self {
Self::A(x, ..) => std::option::Option::Some(&x.acc1),
Expand Down

0 comments on commit 3c6eb4f

Please sign in to comment.