Skip to content

Commit

Permalink
fix: Don't override generics impl in proc-macros on encode impl (#354)
Browse files Browse the repository at this point in the history
* fix: Don't override generics impl in proc-mac

* fix: lint new bench test
  • Loading branch information
Nicceboy authored Oct 25, 2024
1 parent d90deee commit 2d60308
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
8 changes: 7 additions & 1 deletion benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ fn x509(c: &mut Criterion) {
b.iter(|| black_box(rasn::der::decode::<rasn_pkix::Certificate>(data).unwrap()))
});
group.bench_function("x509-parser", |b| {
b.iter(|| black_box(<x509_parser::certificate::X509Certificate as x509_parser::prelude::FromDer<x509_parser::error::X509Error>>::from_der(data)))
b.iter(|| {
black_box(
<x509_parser::certificate::X509Certificate as x509_parser::prelude::FromDer<
x509_parser::error::X509Error,
>>::from_der(data),
)
})
});
group.bench_function("x509-cert", |b| {
b.iter(|| black_box(<x509_cert::Certificate as x509_cert::der::Decode>::from_der(data)))
Expand Down
41 changes: 19 additions & 22 deletions macros/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@ pub trait GenericsExt {
impl GenericsExt for syn::Generics {
fn add_trait_bounds(&mut self, crate_root: &syn::Path, ident: syn::Ident) {
for param in self.type_params_mut() {
param.colon_token = Some(Default::default());
param.bounds = {
let mut punct = syn::punctuated::Punctuated::new();
punct.push(
syn::TraitBound {
paren_token: None,
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: {
let mut path = crate_root.clone();
path.segments.push(syn::PathSegment {
ident: ident.clone(),
arguments: syn::PathArguments::None,
});
if param.colon_token.is_none() {
param.colon_token = Some(Default::default());
}
param.bounds.push(
syn::TraitBound {
paren_token: None,
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: {
let mut path = crate_root.clone();
path.segments.push(syn::PathSegment {
ident: ident.clone(),
arguments: syn::PathArguments::None,
});

path
},
}
.into(),
);

punct
};
path
},
}
.into(),
);
}
}
}
42 changes: 42 additions & 0 deletions tests/generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use rasn::prelude::*;

// https://github.com/librasn/rasn/issues/193
#[test]
fn test_sequence_with_generics_issue_193() {
pub trait LeetTrait {
type Leet: Encode + Decode + core::fmt::Debug + Clone;

fn leet(&self) -> Self::Leet;
}

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice, automatic_tags)]
pub enum Messages<T: LeetTrait> {
Content(T::Leet),
All(()),
}
#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(delegate, automatic_tags)]
pub struct Message<T: LeetTrait>(T);

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct Hello(String);

impl LeetTrait for Hello {
type Leet = u16;

fn leet(&self) -> Self::Leet {
1337
}
}

let hello_message: Message<Hello> = Message(Hello("Hello".to_string()));
let hello_selection: Messages<Hello> = Messages::Content(hello_message.0.leet());

let encoded = rasn::oer::encode::<Messages<Hello>>(&hello_selection).unwrap();
assert_eq!(encoded, vec![0x80, 0x05, 0x39]);
assert_eq!(
hello_selection,
rasn::oer::decode::<Messages<Hello>>(&encoded).unwrap()
)
}

0 comments on commit 2d60308

Please sign in to comment.