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

Tee
can someone please explain to me how Enum.reduce works with maps
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
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
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

We're in Beta

About us Mission Statement