Benjamin-Philip

Benjamin-Philip

Generic Structs with Rustler

The Problem

I have the following structs:

pub struct ExAfRef<T: HasAfEnum>(pub RwLock<Array<T>>);

#[derive(NifStruct)]
#[module = "ExAF.Backend"]
pub struct ExAf<T>
where
    T: HasAfEnum,
{
    pub resource: ResourceArc<ExAfRef<T>>,
}

On compiling it passes the following the error:

error[E0107]: missing generics for struct `datatypes::ExAf`
  --> src/datatypes.rs:10:12
   |
10 | pub struct ExAf<T>
   |            ^^^^ expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `T`
  --> src/datatypes.rs:10:12
   |
10 | pub struct ExAf<T>
   |            ^^^^ -
help: add missing generic argument
   |
10 | pub struct ExAf<T><T>
   |            ~~~~~~~

However when I comment out / remove the rustler specific bits, I don’t have the error:

pub struct ExAfRef<T: HasAfEnum>(pub RwLock<Array<T>>);

pub struct ExAf<T>
where
    T: HasAfEnum,
{
    pub resource: ExAfRef<T>,
}

How can I use generic structs with rustler?

What I am trying to do

I am writing some bindings to arrayfire using their rust crate. Their Array struct is typed and support multiple types like i64 and f64. I am trying to access references (and create references) to Arrays from Elixir. I figured that writing a struct with a trait bound to support all the various types would be the best way to do it.

If there is a better way of doing this without a generic struct, I would be happy to implement it.

Marked As Solved

SeokminHong

SeokminHong

AFAIK, Rustler does not support generics for now.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement