Can a Roc package also include platform code to share implementation details?
I was thinking through how to share code between platforms easily. I would like to use Rust code from cli in the tui platform I have been messing around with. Is there a good way to do this? This had me thinking, how will packages share side-effects across platforms in the future? Can we/should we use roc glue
to assist with building a Rust crate which provides package specific implementation details?
This also feels similar to a recent video I watched with the development of WASM. Is there a Roc equivalent to the component model similar to that being developed for WASM? Does the current Roc platform/package specific integration concept align with this model of software development?
For example; imagine I want to write a library for generating markdown documents. It has a simple API maybe something like this;
interface Markdown
exposes [Elem, writeToFile]
imports []
Elem : [
Heading1 Str,
Heading2 Str,
Text Str,
]
writeToFile : Str (List Elem) -> Effect {}
I would like to generate Rust bindings for this using glue. Then implement the writeToFile
side-effect and potentially other processing of the data. For example I might just use std::fs::File::write_all()
for example. Then I import my Rust library into tui-platform and integrate with my platform, taking advantage of the new features that I didn’t have to write from scratch. I might vendor the Roc interface module so now any apps developed with my platform can target that and have a simple easy to use experience.
The above idea needs a lot of work. I can see a lot of rough edges, but I thought I would pose the question anyway to see if there is any prior research or design around this, or advice that will save me some headaches...
I would like to use Rust code from cli in the tui platform I have been messing around with.
I think publishing a rust lib on crates.io (or github to get started) is the way to go here.
it's a good question!
we've talked about this a lot in the past, and the short answer is that the current plan is to - very intentionally - not introduce any language-level changes to Roc for use cases like this
it's already possible to share host code using the host language's code-sharing capabilities (e.g. in Rust's case, splitting out a crate of shared logic and then importing it in both platforms)
and then the corresponding Roc code can just be copy/pasted
I think we should start with this system, because it's the simplest from a language perspective
it's possible that the benefits of a more complex system would outweigh the cost of complexity, but in this particular case I'm very skeptical of that
so I want to try sticking to the current set of tools for sharing code between platforms, and watch how the ecosystem evolves!
btw rust allows dependencies with a github link, so you don't even need to publish officially to use rust code in multiple projects
Last updated: Jul 06 2025 at 12:14 UTC