Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/infinite recursion #157

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions macros/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ impl Class {
}
}

impl quote::ToTokens for Class {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::TokenStreamExt;
tokens.append_all(match self {
Self::Universal => quote!(rasn::types::Class::Universal),
Self::Application => quote!(rasn::types::Class::Application),
Self::Context => quote!(rasn::types::Class::Context),
Self::Private => quote!(rasn::types::Class::Private),
});
impl Class {
pub fn to_tokens(&self, crate_root: &syn::Path) -> proc_macro2::TokenStream {
match self {
Self::Universal => quote!(#crate_root::types::Class::Universal),
Self::Application => quote!(#crate_root::types::Class::Application),
Self::Context => quote!(#crate_root::types::Class::Context),
Self::Private => quote!(#crate_root::types::Class::Private),
}
}
}

Expand Down Expand Up @@ -158,7 +157,10 @@ impl Tag {

pub fn to_tokens(&self, crate_root: &syn::Path) -> proc_macro2::TokenStream {
match self {
Self::Value { class, value, .. } => quote!(#crate_root::Tag::new(#class, #value)),
Self::Value { class, value, .. } => {
let cls = class.to_tokens(crate_root);
quote!(#crate_root::Tag::new(#cls, #value))
},
Self::Delegate { ty } => quote!(<#ty as #crate_root::AsnType>::TAG),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/per/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,13 @@ impl crate::Encoder for Encoder {
tag: Tag,
value: &V,
) -> Result<Self::Ok, Self::Error> {
if let Some((_,true)) = self.field_bitfield.get(&tag) {
value.encode(self)
} else {
self.set_bit(tag, true)?;
value.encode_with_tag(self, tag)
}
}

fn encode_some<E: Encode>(&mut self, value: &E) -> Result<Self::Ok, Self::Error> {
self.set_bit(E::TAG, true)?;
Expand Down
42 changes: 42 additions & 0 deletions src/uper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,4 +825,46 @@ mod tests {
&[0xA0, 0x08, 0x10, 0x18, 0x20, 0x08, 0x0A, 0x00]
);
}

#[test]
fn recursive_types() {
#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq)]
#[rasn(crate_root = "crate")]
#[rasn(choice, automatic_tags)]
#[non_exhaustive]
enum TestChoice {
Number1(()),
Number2(bool),
Number3(Box<TopLevel>),
}

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq)]
#[rasn(crate_root = "crate")]
#[rasn(automatic_tags)]
struct TopLevel {
#[rasn(value("1..=8"))]
pub test: u8,
pub choice: TestChoice,
}

impl TopLevel {
pub fn new(test: u8, choice: TestChoice) -> Self {
Self { test, choice }
}
}

let test_value = TopLevel::new(
1,
TestChoice::Number3(Box::new(TopLevel {
test: 2,
choice: TestChoice::Number1(()),
})),
);
round_trip!(
uper,
TopLevel,
test_value,
&[8,128]
);
}
}
Loading