Skip to content

Commit

Permalink
signature_derive: adds support for lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Aug 6, 2024
1 parent f1ae691 commit b76ff3a
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions signature_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{
parse_macro_input, parse_quote, punctuated::Punctuated, DeriveInput, Ident, PredicateType,
Token, TraitBound, Type, TypeParam, TypeParamBound, WhereClause, WherePredicate,
parse_macro_input, parse_quote, punctuated::Punctuated, DeriveInput, Ident, Lifetime,
PredicateType, Token, TraitBound, Type, TypeParam, TypeParamBound, WhereClause, WherePredicate,
};

/// Derive the [`Signer`] trait for a type which impls [`DigestSigner`].
Expand Down Expand Up @@ -51,12 +51,13 @@ fn emit_signer_impl(input: DeriveInput) -> TokenStream2 {
);

let name = params.name;
let lifetimes = params.lifetimes;
let impl_generics = params.impl_generics;
let ty_generics = params.ty_generics;
let where_clause = params.where_clause;

quote! {
impl<#(#impl_generics),*> ::signature::Signer<#s_ident> for #name<#(#ty_generics),*>
impl<#(#lifetimes),*, #(#impl_generics),*> ::signature::Signer<#s_ident> for #name<#(#lifetimes),*, #(#ty_generics),*>
#where_clause
{
fn try_sign(&self, msg: &[u8]) -> ::signature::Result<#s_ident> {
Expand Down Expand Up @@ -99,12 +100,13 @@ fn emit_verifier_impl(input: DeriveInput) -> TokenStream2 {
);

let name = params.name;
let lifetimes = params.lifetimes;
let impl_generics = params.impl_generics;
let ty_generics = params.ty_generics;
let where_clause = params.where_clause;

quote! {
impl<#(#impl_generics),*> ::signature::Verifier<#s_ident> for #name<#(#ty_generics),*>
impl<#(#lifetimes),*,#(#impl_generics),*> ::signature::Verifier<#s_ident> for #name<#(#lifetimes),*,#(#ty_generics),*>
#where_clause
{
fn verify(&self, msg: &[u8], signature: &#s_ident) -> ::signature::Result<()> {
Expand Down Expand Up @@ -137,12 +139,13 @@ fn emit_digest_signer_impl(input: DeriveInput) -> TokenStream2 {
);

let name = params.name;
let lifetimes = params.lifetimes;
let impl_generics = params.impl_generics;
let ty_generics = params.ty_generics;
let where_clause = params.where_clause;

quote! {
impl<#(#impl_generics),*> ::signature::DigestSigner<#d_ident, #s_ident> for #name<#(#ty_generics),*>
impl<#(#lifetimes),*,#(#impl_generics),*> ::signature::DigestSigner<#d_ident, #s_ident> for #name<#(#lifetimes),*,#(#ty_generics),*>
#where_clause
{
fn try_sign_digest(&self, digest: #d_ident) -> ::signature::Result<#s_ident> {
Expand Down Expand Up @@ -175,12 +178,13 @@ fn emit_digest_verifier_impl(input: DeriveInput) -> TokenStream2 {
);

let name = params.name;
let lifetimes = params.lifetimes;
let impl_generics = params.impl_generics;
let ty_generics = params.ty_generics;
let where_clause = params.where_clause;

quote! {
impl<#(#impl_generics),*> ::signature::DigestVerifier<#d_ident, #s_ident> for #name<#(#ty_generics),*>
impl<#(#lifetimes),*,#(#impl_generics),*> ::signature::DigestVerifier<#d_ident, #s_ident> for #name<#(#lifetimes),*,#(#ty_generics),*>
#where_clause
{
fn verify_digest(&self, digest: #d_ident, signature: &#s_ident) -> ::signature::Result<()> {
Expand All @@ -195,6 +199,8 @@ struct DeriveParams {
/// Name of the struct the trait impls are being added to.
name: Ident,

lifetimes: Vec<Lifetime>,

/// Generic parameters of `impl`.
impl_generics: Vec<TypeParam>,

Expand All @@ -210,6 +216,12 @@ impl DeriveParams {
fn new(input: DeriveInput) -> Self {
let impl_generics = input.generics.type_params().cloned().collect();

let lifetimes = input
.generics
.lifetimes()
.map(|lt| lt.lifetime.clone())
.collect();

let ty_generics = input
.generics
.type_params()
Expand All @@ -227,6 +239,7 @@ impl DeriveParams {

Self {
name: input.ident,
lifetimes,
impl_generics,
ty_generics,
where_clause,
Expand Down Expand Up @@ -291,7 +304,7 @@ mod tests {
assert_eq!(
output.to_string(),
quote! {
impl<C, __S> ::signature::Signer<__S> for MySigner<C>
impl<, C, __S> ::signature::Signer<__S> for MySigner<, C>
where
C: EllipticCurve,
__S: ::signature::PrehashSignature,
Expand Down Expand Up @@ -320,7 +333,7 @@ mod tests {
assert_eq!(
output.to_string(),
quote! {
impl<C: EllipticCurve, __S> ::signature::Verifier<__S> for MyVerifier<C>
impl<,C: EllipticCurve, __S> ::signature::Verifier<__S> for MyVerifier<,C>
where
__S: ::signature::PrehashSignature,
Self: ::signature::DigestVerifier<__S::Digest, __S>
Expand Down Expand Up @@ -348,7 +361,7 @@ mod tests {
assert_eq!(
output.to_string(),
quote! {
impl<C: EllipticCurve, __D, __S> ::signature::DigestSigner<__D, __S> for MySigner<C>
impl<,C: EllipticCurve, __D, __S> ::signature::DigestSigner<__D, __S> for MySigner<,C>
where
__D: ::signature::digest::Digest,
Self: ::signature::hazmat::PrehashSigner<__S>
Expand Down Expand Up @@ -376,7 +389,7 @@ mod tests {
assert_eq!(
output.to_string(),
quote! {
impl<C: EllipticCurve, __D, __S> ::signature::DigestVerifier<__D, __S> for MyVerifier<C>
impl<,C: EllipticCurve, __D, __S> ::signature::DigestVerifier<__D, __S> for MyVerifier<,C>
where
__D: ::signature::digest::Digest,
Self: ::signature::hazmat::PrehashVerifier<__S>
Expand All @@ -389,4 +402,37 @@ mod tests {
.to_string()
);
}

#[test]
fn signer_lifetimes() {
let input = parse_quote! {
#[derive(Signer)]
struct MySigner<'a, C>
where
C: EllipticCurve
{
scalar: Scalar<C::ScalarSize>,
_lifetime: &'a (),
}
};

let output = emit_signer_impl(input);

assert_eq!(
output.to_string(),
quote! {
impl<'a, C, __S> ::signature::Signer<__S> for MySigner<'a, C>
where
C: EllipticCurve,
__S: ::signature::PrehashSignature,
Self: ::signature::DigestSigner<__S::Digest, __S>
{
fn try_sign(&self, msg: &[u8]) -> ::signature::Result<__S> {
self.try_sign_digest(__S::Digest::new_with_prefix(msg))
}
}
}
.to_string()
);
}
}

0 comments on commit b76ff3a

Please sign in to comment.