Rust2Go is a project that provides users with a simple and efficient way to call Golang from Rust with native async support.
rust2go-cli --src src/user.rs --dst go/gen.go
build.rs
for you project.For detailed example, please checkout the example projects.
Detailed design details can be found in this article: Design and Implementation of a Rust-Go FFI Framework.
In order to achieve the ultimate performance, this project is not based on communication, but on FFI to pass specially encoded data. In order to reduce memory operations to a minimum, data that satisfies a specific memory layout is passed directly by reference rather than copied.
For example, Vec<u8>
and String
is represented as a pointer and a length. However, structs like Vec<String>
or Vec<Vec<u8>>
require intermediate representation. In order to reduce the number of memory allocations to one, I use a precomputed size buffer to store these intermediate structures.
On the Golang side, the data it receives is referenced from Rust. The Rust side will do its best to ensure the validity of this data during the call. So the Golang side can implement the handler arbitrarily, but manually deep copy when leaking data outside the function life cycle.
On the Rust side, it is needed to ensure that the slot pointer of the callback ffi operation, and the user parameters are valid when the future drops. This is archieved by implementing an atomic slot structure and providing a [drop_safe]
attribute to require user passing parameters with ownership.
--go118
This project is inspired by fcplug.