Skip to content

Commit

Permalink
fix(derive): use const blocks and add generic bounds when generatin…
Browse files Browse the repository at this point in the history
…g an enum's `AsnType` impl (#398)
  • Loading branch information
repnop authored Jan 3, 2025
1 parent 8e3b20a commit 26c0117
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion macros/macros_impl/src/asn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn derive_struct_impl(
paren_token: Some(<_>::default()),
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: syn::parse_str("rasn::AsnType").unwrap(),
path: syn::parse_quote!(#crate_root::AsnType),
}));
}

Expand Down
33 changes: 23 additions & 10 deletions macros/macros_impl/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,35 @@ impl Enum {

quote! {
{
const VARIANT_LIST: &'static [#crate_root::types::TagTree] = &[#(#field_tags),*];
const VARIANT_TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(VARIANT_LIST);
const _: () = assert!(VARIANT_TAG_TREE.is_unique(), #error_message);
VARIANT_TAG_TREE
let variant_list: &'static [#crate_root::types::TagTree] = const { &[#(#field_tags),*] };
let variant_tag_tree: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(variant_list);
assert!(variant_tag_tree.is_unique(), #error_message);
variant_tag_tree
}
}
} else {
quote!(#crate_root::types::TagTree::Leaf(#tag))
};

let name = &self.name;
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
let mut generics = self.generics.clone();

for param in &mut generics.params {
if let syn::GenericParam::Type(type_param) = param {
type_param
.bounds
.push(syn::parse_quote!(#crate_root::AsnType));
}
}

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let return_val = self
.config
.tag
.is_some()
.then(|| quote!(#crate_root::types::TagTree::Leaf(Self::TAG)))
.unwrap_or_else(|| quote!(TAG_TREE));
.unwrap_or_else(|| quote!(tag_tree));

let (base_variants, extended_variants): (Vec<_>, Vec<_>) = self
.variants
Expand Down Expand Up @@ -174,10 +185,12 @@ impl Enum {
const TAG: #crate_root::types::Tag = {
#tag
};
const TAG_TREE: #crate_root::types::TagTree = {
const LIST: &'static [#crate_root::types::TagTree] = &[#tag_tree];
const TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(LIST);
const _: () = assert!(TAG_TREE.is_unique(), #error_message);
const TAG_TREE: #crate_root::types::TagTree = const {
let list: &'static [#crate_root::types::TagTree] = const {
&[#tag_tree]
};
let tag_tree: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(list);
assert!(tag_tree.is_unique(), #error_message);
#return_val
};
#alt_identifier
Expand Down
7 changes: 7 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ pub enum ExplicitChoice {
ByKey,
}

#[derive(AsnType, Decode, Encode)]
#[rasn(choice)]
pub enum GenericEnum<T: Clone, const N: usize> {
FixedString(FixedOctetString<N>),
Sequence(Vec<T>),
}

#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BasicConstraints {
#[rasn(default)]
Expand Down

0 comments on commit 26c0117

Please sign in to comment.