May 7, 2017 by Allen George
Thrift and Rust
I maintain the Rust Thrift language generator. In this post I’ll briefly describe how to write a simple Thrift client and server in Rust.
First, you’ll need the Thrift compiler. thrift 0.11 should support Rust, but till then you’ll need a development version of the compiler. Please see the Thrift website for documentation on setting it up.
Thrift works by converting an RPC IDL into target code that can be used in your language’s client/server to send and process RPC calls. There are a few steps:
- Describe your IDL in a
- Invoke the Thrift compiler to convert the IDL into executable code in the target language
- Link the generated code as well as the associated “runtime binding library” into your client/server implementation
In our case the “runtime binding library” is called
thrift and it’s available as a pre-release crate on crates.io. Once an official version of the thrift compiler is released, I’ll release a crate with a matching version on crates.io.
Let’s apply this to Rust:
example +- Cargo.toml +- simple_service.thrift // Thrift IDL +- src +- simple_service.rs // generated by the Thrift compiler +- lib.rs +- bin +- client.rs +- server.rs
Thrift IDL (simple_service.thrift)
Invoke Compiler (generates simple_service.rs)
thrift --out src --gen rs simple_service.thrift
allen@ddde92e9cfcf:/thrift/src/test/rs-example# ./target/debug/client Hello Allen! client ran successfully
Hopefully this gives you an idea of what it’s like to write a Thrift client and server in Rust. I’ll be continuing to work on the Thrift compiler plugin: adding derives or trait implementations where they make sense, improving ergonomics, incorporating more Thrift features – in general, making the use of Thrift in Rust a more pleasant experience. If you encounter any problems file a bug or PM me on Reddit (I’m usually around on the Rust subreddit).