1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
# Rust Language Bindings for Thrift
## Getting Started
1. Get the [Thrift compiler](https://thrift.apache.org).
2. Add the thrift crate to your `Cargo.toml`.
```toml
thrift = "x.y.z" # x.y.z is the version of the thrift compiler
```
3. Generate Rust sources for your IDL (for example, `Tutorial.thrift`).
```shell
thrift -out my_rust_program/src --gen rs -r Tutorial.thrift
```
4. Use the generated source in your code.
```rust
// generated Rust module from Thrift IDL
mod tutorial;
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol};
use thrift::protocol::{TInputProtocol, TOutputProtocol};
use thrift::transport::{TFramedReadTransport, TFramedWriteTransport};
use thrift::transport::{TIoChannel, TTcpChannel};
use tutorial::{CalculatorSyncClient, TCalculatorSyncClient};
use tutorial::{Operation, Work};
fn main() {
match run() {
Ok(()) => println!("client ran successfully"),
Err(e) => {
println!("client failed with {:?}", e);
std::process::exit(1);
}
}
}
fn run() -> thrift::Result<()> {
//
// build client
//
println!("connect to server on 127.0.0.1:9090");
let mut c = TTcpChannel::new();
c.open("127.0.0.1:9090")?;
let (i_chan, o_chan) = c.split()?;
let i_prot = TCompactInputProtocol::new(
TFramedReadTransport::new(i_chan)
);
let o_prot = TCompactOutputProtocol::new(
TFramedWriteTransport::new(o_chan)
);
let mut client = CalculatorSyncClient::new(i_prot, o_prot);
//
// alright! - let's make some calls
//
// two-way, void return
client.ping()?;
// two-way with some return
let res = client.calculate(
72,
Work::new(7, 8, Operation::Multiply, None)
)?;
println!("multiplied 7 and 8, got {}", res);
// two-way and returns a Thrift-defined exception
let res = client.calculate(
77,
Work::new(2, 0, Operation::Divide, None)
);
match res {
Ok(v) => panic!("shouldn't have succeeded with result {}", v),
Err(e) => println!("divide by zero failed with {:?}", e),
}
// one-way
client.zip()?;
// done!
Ok(())
}
```
## Code Generation
### Thrift Files and Generated Modules
The Thrift code generator takes each Thrift file and generates a Rust module
with the same name snake-cased. For example, running the compiler on
`ThriftTest.thrift` creates `thrift_test.rs`. To use these generated files add
`mod ...` and `use ...` declarations to your `lib.rs` or `main.rs` - one for
each generated file.
### Results and Errors
The Thrift runtime library defines a `thrift::Result` and a `thrift::Error` type,
both of which are used throughout the runtime library and in all generated code.
Conversions are defined from `std::io::Error`, `str` and `String` into
`thrift::Error`.
### Thrift Type and their Rust Equivalents
Thrift defines a number of types, each of which is translated into its Rust
equivalent by the code generator.
* Primitives (bool, i8, i16, i32, i64, double, string, binary)
* Typedefs
* Enums
* Containers
* Structs
* Unions
* Exceptions
* Services
* Constants (primitives, containers, structs)
In addition, unless otherwise noted, thrift includes are translated into
`use ...` statements in the generated code, and all declarations, parameters,
traits and types in the generated code are namespaced appropriately.
The following subsections cover each type and their generated Rust equivalent.
### Primitives
Thrift primitives have straightforward Rust equivalents.
* bool: `bool`
* i8: `i8`
* i16: `i16`
* i32: `i32`
* i64: `i64`
* double: `OrderedFloat<f64>`
* string: `String`
* binary: `Vec<u8>`
### Typedefs
A typedef is translated to a `pub type` declaration.
```thrift
typedef i64 UserId
typedef map<string, UserId> MapType
```
```rust
pub type UserId = i64;
pub type MapType = BTreeMap<String, Bonk>;
```
### Enums
A Thrift enum is represented as a Rust enum, and each variant is transcribed 1:1.
```thrift
enum Numberz
{
ONE = 1,
TWO,
THREE,
FIVE = 5,
SIX,
EIGHT = 8
}
```
```rust
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Numberz {
ONE = 1,
TWO = 2,
THREE = 3,
FIVE = 5,
SIX = 6,
EIGHT = 8,
}
impl TryFrom<i32> for Numberz {
// ...
}
```
### Containers
Thrift has three container types: list, set and map. They are translated into
Rust `Vec`, `BTreeSet` and `BTreeMap` respectively. Any Thrift type (this
includes structs, enums and typedefs) can be a list/set element or a map
key/value.
#### List
```thrift
list <i32> numbers
```
```rust
numbers: Vec<i32>
```
#### Set
```thrift
set <i32> numbers
```
```rust
numbers: BTreeSet<i32>
```
#### Map
```thrift
map <string, i32> numbers
```
```rust
numbers: BTreeMap<String, i32>
```
### Structs
A Thrift struct is represented as a Rust struct, and each field transcribed 1:1.
```thrift
struct CrazyNesting {
1: string string_field,
2: optional set<Insanity> set_field,
3: required list<
map<set<i32>, map<i32,set<list<map<Insanity,string>>>>>
>
4: binary binary_field
}
```
```rust
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct CrazyNesting {
pub string_field: Option<String>,
pub set_field: Option<BTreeSet<Insanity>>,
pub list_field: Vec<
BTreeMap<
BTreeSet<i32>,
BTreeMap<i32, BTreeSet<Vec<BTreeMap<Insanity, String>>>>
>
>,
pub binary_field: Option<Vec<u8>>,
}
impl CrazyNesting {
pub fn read_from_in_protocol(i_prot: &mut TInputProtocol)
->
thrift::Result<CrazyNesting> {
// ...
}
pub fn write_to_out_protocol(&self, o_prot: &mut TOutputProtocol)
->
thrift::Result<()> {
// ...
}
}
```
##### Optionality
Thrift has 3 "optionality" types:
1. Required
2. Optional
3. Default
The Rust code generator encodes *Required* fields as the bare type itself, while
*Optional* and *Default* fields are encoded as `Option<TypeName>`.
```thrift
struct Foo {
1: required string bar // 1. required
2: optional string baz // 2. optional
3: string qux // 3. default
}
```
```rust
pub struct Foo {
bar: String, // 1. required
baz: Option<String>, // 2. optional
qux: Option<String>, // 3. default
}
```
## Known Issues
* Struct constants are not supported
* Map, list and set constants require a const holder struct
|