Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add deku_id()#176

Merged
sharksforarms merged 20 commits into
sharksforarms:masterfrom
wcampbell0x2a:feature/deku-id
Feb 23, 2021
Merged

Add deku_id()#176
sharksforarms merged 20 commits into
sharksforarms:masterfrom
wcampbell0x2a:feature/deku-id

Conversation

@wcampbell0x2a
Copy link
Copy Markdown
Collaborator

This creates a deku_id() function for enums that gives a user the ability for knowing the id of a enum variant at runtime. See #141

Any comments you have would be great! I'm still new to more advanced TokenStream fun.

  • Having some issues with implementing the Generic part of the enum, and not sure how to implement that part of the code generation.
error[E0412]: cannot find type `T` in this scope
  --> src/lib.rs:89:15
   |
89 | enum TestEnum<T>
   |               ^ not found in this scope

error: aborting due to previous error
  • Not sure on if you want this to be a trait function grouped with DekuRead?
  • I have more testing I can throw in that enum test file

Copy link
Copy Markdown
Owner

@sharksforarms sharksforarms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wcampbell0x2a ! Just some general comments on how to make it better. This looks great!

(Also PR is coming to fix bitvec/clippy/build errors... sorry about that)

Edit: Done. You can now rebase with master and the builds should be better

Comment thread deku-derive/src/macros/deku_read.rs
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread deku-derive/src/macros/deku_read.rs
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread tests/test_enum.rs
@wcampbell0x2a
Copy link
Copy Markdown
Collaborator Author

Only thing left is the tests/test_generic.rs, any hints?

@wcampbell0x2a
Copy link
Copy Markdown
Collaborator Author

Also adding docs, do you have an area in which you think docs for this function would be applicable?

@passcod
Copy link
Copy Markdown

passcod commented Feb 18, 2021

This make deku_id() an inherent method, right? Wouldn't it be better to have on a dedicated trait instead so it doesn't conflict with any user-defined deku_id()?

Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread src/error.rs Outdated
Comment thread src/error.rs Outdated
Comment thread tests/test_deku_id.rs Outdated
Comment thread tests/test_deku_id.rs Outdated
Comment thread tests/test_deku_id.rs Outdated
@sharksforarms
Copy link
Copy Markdown
Owner

Only thing left is the tests/test_generic.rs, any hints?

Here is an example:

https://github.com/sharksforarms/deku/blob/master/deku-derive/src/macros/deku_read.rs#L658

Also adding docs, do you have an area in which you think docs for this function would be applicable?

Let's see what the docs look like with a DekuEnumExt trait, this may be sufficient. We can also expand the example/enums.rs to include it.

@wcampbell0x2a
Copy link
Copy Markdown
Collaborator Author

Fixed generics, added to examples/enums.rs and added trait 🚀

Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread deku-derive/src/macros/deku_read.rs Outdated
Comment thread src/lib.rs
Comment thread deku-derive/src/macros/deku_read.rs Outdated
let deku_id_id_type = if let Some(id_type) = id_type {
id_type
} else {
&ctx_types
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with using ctx_types is when there's more than one, it breaks:

#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(ctx = "test: u16, my_id: u8", id = "my_id")]
enum EnumId {
    #[deku(id = "1")]
    VarA(u8),
    #[deku(id = "2")]
    VarB,
}

generates

impl DekuEnumExt<'_, (u16, u8)> for EnumId {
    fn deku_id(&self) -> Result<(u16, u8), DekuError> {
        match self {
            Self::VarA(__deku_field_0) => Ok(1),
            Self::VarB => Ok(2),
            _ => Err(DekuError::IdVariantNotFound),
        }
    }
}

One way of solving this would be to find the type of input.id in input.ctx

#[deku(ctx = "test: u16, my_id: u8", id = "my_id")]

Having a utility function in macros/mod.rs would be useful. You can look at gen_ctx_types_and_arg to see how it iterates ctx

Copy link
Copy Markdown
Collaborator Author

@wcampbell0x2a wcampbell0x2a Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed 3826536 with this fix. It works and the tests compile but the code quality might be lacking, any opinions or extra tests are good ;)

Comment thread tests/test_deku_id.rs Outdated
Comment thread tests/test_enum.rs
Comment thread tests/test_deku_id.rs
This creates a `deku_id()` function for enums that gives a user the
ability for knowing the id of a enum variant at runtime.

Signed-off-by: wcampbell <[email protected]>
Signed-off-by: wcampbell <[email protected]>
Signed-off-by: wcampbell <[email protected]>
Signed-off-by: wcampbell <[email protected]>
Signed-off-by: wcampbell <[email protected]>
@wcampbell0x2a wcampbell0x2a changed the title WIP: deku_id() Add deku_id() Feb 23, 2021
Copy link
Copy Markdown
Owner

@sharksforarms sharksforarms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks @wcampbell0x2a

@sharksforarms sharksforarms merged commit 4cfe682 into sharksforarms:master Feb 23, 2021
@wcampbell0x2a wcampbell0x2a deleted the feature/deku-id branch December 28, 2023 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants