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

nif macro fails with macro arguments #519

Closed
denumerate opened this issue Feb 27, 2023 · 2 comments · Fixed by #527
Closed

nif macro fails with macro arguments #519

denumerate opened this issue Feb 27, 2023 · 2 comments · Fixed by #527

Comments

@denumerate
Copy link

The nif macro fails when a tuple is passed to a function, like below:

#[rustler::nif]
fn test_fn(item: (usize, usize)) -> (usize, usize) {
    (item.0 + 1, item.1 + 1)
}

The error is:

error: custom attribute panicked
  --> src/lib.rs:37:1
   |
37 | #[rustler::nif]
   | ^^^^^^^^^^^^^^^
   |
   = help: message: unsupported input given: Tuple(TypeTuple { paren_token: Paren, elems: [Path(TypePath { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { ident: "usize", span: #0 bytes(1029..1034) }, arguments: None }] } }), Comma, Path(TypePath { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { ident: "usize", span: #0 bytes(1036..1041) }, arguments: None }] } })] })

I looked into rustler_codegen and it looks like tuple isn't supported in the macro. Is there any special reason for this? Tuples are types provided by the standard library in both rust and elixir, and already have both decode and encode implemented. I could use the NifTuple macro, but I'd probably just be re-implementing tuples, which seems wasteful.

@filmor
Copy link
Member

filmor commented Feb 27, 2023

It should be enough to add a syn::Type::Tuple branch to this match https://github.com/rusterlium/rustler/blob/master/rustler_codegen/src/nif.rs#L118

The decoder already exists, so you can probably just use the exact same implementation as https://github.com/rusterlium/rustler/blob/master/rustler_codegen/src/nif.rs#L119-L128

@denumerate
Copy link
Author

Thanks for the help! That at least worked locally

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 a pull request may close this issue.

2 participants