Skip to content

Commit

Permalink
Add lifetime bounds to derived struct and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
miam-miam committed Dec 7, 2023
1 parent f516d3c commit 5e7a421
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
13 changes: 10 additions & 3 deletions prse-derive/src/expand_derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use proc_macro2::{Ident, TokenStream};
use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens;
use syn::{GenericParam, Generics, ImplGenerics, WhereClause, WherePredicate};
use syn::{
GenericParam, Generics, ImplGenerics, Lifetime, LifetimeParam, WhereClause, WherePredicate,
};

use crate::derive::{Derive, Fields};
use crate::instructions::Instructions;
Expand Down Expand Up @@ -175,7 +177,12 @@ fn split_for_impl(
) -> (ImplGenerics, TokenStream, Option<&WhereClause>) {
let ty_generics = generics.split_for_impl().1.to_token_stream();

generics.params.push(parse_quote!('__prse_a));
generics.params.push(GenericParam::Lifetime(LifetimeParam {
attrs: vec![],
lifetime: Lifetime::new("'__prse_a", Span::call_site()),
colon_token: None,
bounds: generics.lifetimes().map(|l| l.lifetime.clone()).collect(),
}));

let type_predicates: Vec<WherePredicate> = generics
.params
Expand Down
23 changes: 23 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,27 @@ mod common {
parse!("A-B", "{}")
)
}

#[derive(Parse, Debug, PartialEq, Eq)]
enum Capture<'c> {
Single(&'c str),
}

#[derive(Parse, Debug, Eq, PartialEq)]

Check failure on line 38 in tests/common.rs

View workflow job for this annotation

GitHub Actions / Lints

the trait bound `common::Capture<'_>: std::str::FromStr` is not satisfied
#[prse = "{b} {c:-:2}"]
struct Lifetimes<'a, 'b> {
b: Capture<'a>,
c: [&'b str; 2],
}

#[test]
fn parse_lifetime_derive() {
assert_eq!(
Lifetimes {
b: Capture::Single("yummy"),
c: ["gummy", "bear"]
},
parse!("yummy gummy-bear", "{}")
);
}
}

0 comments on commit 5e7a421

Please sign in to comment.