librasn / rasn

A Safe #[no_std] ASN.1 Codec Framework
Other
183 stars 43 forks source link

Traits for macro expansion not always correctly carried with generics when using Encode/Decode #193

Open Nicceboy opened 8 months ago

Nicceboy commented 8 months ago

It seems that syntax matters for declaring generics and trait declarations.

I have the following struct:

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice, automatic_tags)]
pub enum RPermissions<T: CertExtType> {
    Content(T::Req),
    All(()),
}

It does not compile, and if I expand Encode macro for example, CertExtType condition is not carried.

However, if I change syntax to following

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice, automatic_tags)]
pub enum RPermissions<T>
where
    T: CertExtType,
{
    Content(T::Req),
    All(()),
}

Correct macro code is generated. I almost ended up writing custom functions, but noticed by accident. If someone else fights the same problem 😁 .

XAMPPRocky commented 8 months ago

Huh, the first code should work.

Nicceboy commented 8 months ago

Huh, the first code should work.

It seems to work when newtype style is used, for example

#[derive(AsnType, Debug, Decode, Encode, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(automatic_tags)]
pub struct ContributedExtensionBlocks<T: ContributedExtension>(
    SequenceOf<ContributedExtensionBlock<T>>,
);

Edit, nevermind, it won't. The compiler error was just hidden by other errors.