Stream: beginners

Topic: Help for creatng glue for platform with elm-like API Rust


view this post on Zulip Felix Andreas (Mar 19 2024 at 18:36):

Hi! Roc looks great so far!

Out of curiosity a colleague and me are trying to implement a simple platform with a API similar to the Elm Architecture.

# main.roc
platform "test-platform"
    requires {} { program : {
        view : Str -> Task Str [],
        update : Str -> Task Str [],
    } }
    exposes []
    packages {}
    imports [Task.{ Task }]
    provides [mainForHost]

mainForHost : {
    view : Str -> Task Str [],
    update : Str -> Task Str [],
}
mainForHost = {
    view: \x -> program.view x,
    update: \x -> program.update x,
}

Unfortunately the roc glue command seems to not to support this use case yet:

  1. It generates TODOs: \TODO: SingleTagStruct with closures U1 C17_4
  2. It generates symbols not present in the application:

      roc__mainForHost_4_caller, was not defined by the app.
    
  3. It tries to derive Clone for the {view, update} Record but not for the subtypes it holds. We tried to just remove the clone (at least Rust didn't complain ...)

So we tried to manually write the glue code: We got it working for a platform like this:

mainForHost: Str -> Task Str []

and this:

mainForHost : {
    view : Str -> Str,
    update : Str -> Str,
}

But, we tried mulitple hours to write the glue for the above mentioned platform and always hit a segfaults or double free errors when trying, which we weren't able to fix. We found https://github.com/lukewilliamboswell/roc-wasm4, which had similar platform API, but it is written in Zig.

Does anybody has written a platform with a similar API in Rust yet? Or, any tips how to debug the glue code? Thanks in advance!

view this post on Zulip Felix Andreas (Mar 19 2024 at 19:42):

In case somebody else hits the same problem: I think this is what we need: https://github.com/lukewilliamboswell/roc-gui/blob/main/platform/src/roc.rs

We will try again tomorrow


Last updated: Jul 05 2025 at 12:14 UTC