Thrift and Rust

T

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:

  1. Describe your IDL in a .thrift file
  2. Invoke the Thrift compiler to convert the IDL into executable code in the target language
  3. Link the generated code as well as the associated “runtime binding library” into your client/server implementation
  4. You’re…done!

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.

Example

Let’s apply this to Rust:

Directory Structure

example
+- Cargo.toml
+- simple_service.thrift // Thrift IDL
+- src
   +- simple_service.rs  // generated by the Thrift compiler
   +- lib.rs
   +- bin
      +- client.rs
      +- server.rs

Cargo.toml

Thrift IDL (simple_service.thrift)

Invoke Compiler (generates simple_service.rs)

thrift --out src --gen rs simple_service.thrift

lib.rs

bin/client.rs

bin/server.rs

Output

allen@ddde92e9cfcf:/thrift/src/test/rs-example# ./target/debug/client
Hello Allen!
client ran successfully

Closing Notes

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).

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.