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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement