fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.86k stars 296 forks source link

How to create/use Rust bindings? #3479

Open zakaluka opened 1 year ago

zakaluka commented 1 year ago

Description

Is there a guide/instructions to create F# bindings to Rust crates? I could not find any information.

Alternately, are any crates "baked into" the F# to Rust conversion via Fable? E.g. async-std/tokio for async operations, yew/seed for web framework, etc.?

Repro code

N/A

Expected and actual results

Expected that either there is a way to create bindings or the frameworks are already baked in.

ncave commented 1 year ago

@zakaluka Thank you for your interest in Fable and for contributing an issue! To clarify, the Fable-Rust support is still in alpha phase and implementation may still change.

Creating bindings for Rust is currently a bit tricky cause of the need to properly translate the inputs and output types back and forth between Rust and F# Fable-Rust generated code. We don't have an example of integrating with larger Rust frameworks yet, but here are some simple examples of bindings if you need to.

It's probably easier to separate your F# code as a library or module and just consume it from Rust. We tried not to integrate any large Rust frameworks to try to keep dependencies to a minimum, but some F# features are implemented with Rust packages behind feature flags that you can opt out of, if you don't need that specific feature.

We're adding some function attributes in the next Fable release, but it's not available yet in the current release.

Please feel free to share any improvement ideas or suggestions you might have, contributions are always welcome and no contribution is too small.

sachinshahredmane commented 1 year ago

Thank you @ncave. This library is of great interest to me because I feel like it brings together the succinctness of F# with the performance of Rust. I have been closely following the status - and it looks like many of the basics are implemented.

I will try out a small POC to see how it works and will post here if I have more questions. Thank you for all your hard work on this!

SichangHe commented 1 month ago

Hey @ncave, interesting development. I skimmed over the examples you provided but did not quite get how to use this feature, perhaps more documentation should be considered just for people to try it out.

Hey @sachinshahredmane, could you share your POC?

ncave commented 1 month ago

@SichangHe

Here is one example that uses Fable-to-Rust.