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

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

Closed
Nicceboy opened this issue Nov 2, 2023 · 4 comments · Fixed by #354
Labels
area/types Related to rasn’s types for ASN.1 good first issue Good for newcomers help wanted Extra attention is needed kind/bug Something isn't working

Comments

@Nicceboy
Copy link
Contributor

Nicceboy commented Nov 2, 2023

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
Copy link
Collaborator

Huh, the first code should work.

@Nicceboy
Copy link
Contributor Author

Nicceboy commented Nov 3, 2023

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.

@XAMPPRocky
Copy link
Collaborator

@Nicceboy Is this still the case?

@Nicceboy
Copy link
Contributor Author

Nicceboy commented Sep 6, 2024

Yeah, the commented in the following does not compile:

use rasn::prelude::*;

pub trait TestTrait: Encode + Decode + AsnType {
    type Req: Encode + Decode + AsnType;
}

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

// #[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
// #[rasn(choice, automatic_tags)]
// pub enum TraitNotCarried<T: TestTrait> {
//     Maybe(T::Req),
//     MaybeNot(()),
// }

impl TestTrait for Hello {
    type Req = u8;
}

#[derive(AsnType, Encode, Decode, Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[rasn(choice, automatic_tags)]
pub enum TraitNotCarried<T>
where
    T: TestTrait,
{
    Maybe(T::Req),
    MaybeNot(()),
}

fn main() {
    let hello = Hello("world!".to_string());
    println!("{}", hello.0);
}

@XAMPPRocky XAMPPRocky added kind/bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers area/types Related to rasn’s types for ASN.1 labels Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/types Related to rasn’s types for ASN.1 good first issue Good for newcomers help wanted Extra attention is needed kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants