Skip to content

NifTuple doesn't work with polymorphic structs #424

Closed

Description

@turion

Small failing example:

use rustler::NifTuple;

#[derive(NifTuple)]
struct Foo<T> {
    t: T,
    t2: T,
}

fn main() {
}
error[E0107]: missing generics for struct `Foo`
 --> src/main.rs:4:8
  |
4 | struct Foo<T> {
  |        ^^^ expected 1 generic argument
  |
note: struct defined here, with 1 generic parameter: `T`
 --> src/main.rs:4:8
  |
4 | struct Foo<T> {
  |        ^^^ -
help: add missing generic argument
  |
4 | struct Foo<T><T> {
  |        ~~~~~~

For more information about this error, try `rustc --explain E0107`.

The code compiles both when I remove the derive clause, or when I remove <T> and change the T to a scalar type.

Activity

  1. turion commented on Jan 18, 2022

    @turion
    ContributorAuthor
  2. evnu commented on Jan 18, 2022

    @evnu
    Member

    @turion codegen does not support generic structs (yet?). For example, NifStruct also doesn't work for a polymorphic struct:

    #[derive(NifStruct)]
    #[module = "hello"]
    pub struct GenericStruct<T> {
        lhs: T,
    }
      --> rustler_tests/native/rustler_test/src/test_codegen.rs:12:12
       |
    12 | pub struct GenericStruct<T> {
       |            ^^^^^^^^^^^^^ expected 1 generic argument
       |
    note: struct defined here, with 1 generic parameter: `T`
      --> rustler_tests/native/rustler_test/src/test_codegen.rs:12:12
       |
    12 | pub struct GenericStruct<T> {
       |            ^^^^^^^^^^^^^ -
    help: add missing generic argument
       |
    12 | pub struct GenericStruct<T><T> {
    
  3. added 3 commits that reference this issue on Jan 27, 2022
    3da1e00
    599cf04
    7bbdfc4
  4. evnu commented on Aug 3, 2022

    @evnu
    Member

    #483 might push this a little bit forward as it uses split_for_impl() from syn to generate the impls.

  5. added 2 commits that reference this issue on Aug 25, 2022
    441eafb
    9549d2d
  6. evnu commented on Sep 15, 2022

    @evnu
    Member

    Does #483 fix this?

  7. turion commented on Sep 15, 2022

    @turion
    ContributorAuthor

    Not out of the box. I tried the following:

    diff --git a/rustler_tests/native/rustler_test/src/test_codegen.rs b/rustler_tests/native/rustler_test/src/test_codegen.rs
    index 00f97c9..d054a72 100644
    --- a/rustler_tests/native/rustler_test/src/test_codegen.rs
    +++ b/rustler_tests/native/rustler_test/src/test_codegen.rs
    @@ -195,6 +195,16 @@ pub fn tuplestruct_record_echo(tuplestruct: TupleStructRecord) -> TupleStructRec
         tuplestruct
     }
     
    +#[derive(NifStruct)]
    +#[module="Foo"]
    +struct Foo<T> {
    +    t: T,
    +    t2: T,
    +}
    +
    +fn main() {
    +}
    +
     pub mod reserved_keywords {
         use rustler::{NifMap, NifRecord, NifStruct, NifTuple, NifUntaggedEnum};
    
    

    Error message:

    the parameter type `T` may not live long enough
    ...so that the type `Foo<T>` will meet its required lifetime bounds...rustc[E0309](https://doc.rust-lang.org/error-index.html#E0309)
    mod.rs(43, 32): ...that is required by this bound
    test_codegen.rs(200, 13): consider adding an explicit lifetime bound...: `: '__rustler_decode_lifetime`
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions