#Could a Rust/C dependency be compiled to a NIF or a WASM binary depending on target?

1 messages · Page 1 of 1 (latest)

uncut shore
#

Hi! I'm exploring Gleam as a possible solution to a project I'm working on. Part of what makes Gleam such an excellent fit is being able to target Erlang for my backend and JavaScript for my frontend.

However, I want to support post quantum encrypt. I've found Erlang packages and JavaScript packages that support the algorithms, but I'm concerned about the complexity that maintain separate packages for different backends would add to the code base.

It occurred to me that I could take a Rust or C package and compile it for both backends, as WASM for js or a NIF for erlang.

I don't see any docs or blogs about this so I'm wondering if this is not a discussed area because it is a dumb idea for some glaringly obvious reason.

quartz blaze
#

Compiling to nif/wasm sounds much more complicated than just having an extra package? And It's completely normal for the backend to have different packages from the frontend.

scarlet nexus
#

You could theoretically do that but the amount of code you would share is less than you think, and the glue code for each platform is more than you think.

uncut shore
#

Roger! Thank you! Are there docs on best practices for building projects that target both backends?

uncut shore
#

My use case is a little more complicated than my original post implied because I was not trying to distract from the question -- but potentially I created an XY problem. Basically, I'm working on a peer to peer network where each node needs to have most of the components of the backend, but those living on user facing devices also need to serve a front end. On mobile devices, I'd need the server to run on an embedded deno runtime. But if it's running on a dedicated server, I'd like to leverage Erlang.

As a proof of concept, I plan to just work on JS, but I don't want to end up boxing myself in with poor design decisions in a new language.

#

I'm coming from a Julia background, so I'm used to functional style, but I'm also used to multiple dispatch and a bunch of other modes of expression that Gleam doesn't include for the sake of simplicity. I really appreciate the design choices, but I'm having trouble slotting my mental model into Gleam's 😅

serene sun
#

It’s bled over into the other languages I use too (for the better)

#

I think just embracing the fact that you won’t get everything right the first time and will probably have to refactor several times over before you get the ideal design you want will help

#

For my current gleam project, I’ve probably refactored 80% of the code at least like 4 times

uncut shore
#

Totally! Moving from Python to Julia totally restructured the way I think about modeling a program (function+type). And it's radically changed the way I write Python.

I know sum types and immutability will also reshape my mental model again.

unique grotto