README.md
1# Rust Thrift library
2
3## Overview
4
5This crate implements the components required to build a working Thrift server
6and client. It is divided into the following modules:
7
8 1. errors
9 2. protocol
10 3. transport
11 4. server
12 5. autogen
13
14The modules are layered as shown. The `generated` layer is code generated by the
15Thrift compiler's Rust plugin. It uses the components defined in this crate to
16serialize and deserialize types and implement RPC. Users interact with these
17types and services by writing their own code on top.
18
19 ```text
20 +-----------+
21 | app dev |
22 +-----------+
23 | generated | <-> errors/results
24 +-----------+
25 | protocol |
26 +-----------+
27 | transport |
28 +-----------+
29 ```
30
31## Using this crate
32
33Add `thrift = "x.y.z"` to your `Cargo.toml`, where `x.y.z` is the version of the
34Thrift compiler you're using.
35
36## API Documentation
37
38Full [Rustdoc](https://docs.rs/thrift/)
39
40## Compatibility
41
42The Rust library and auto-generated code targets Rust versions 1.28+.
43It does not currently use any Rust 2021 features.
44
45### Breaking Changes
46
47Breaking changes are minimized. When they are made they will be outlined below with transition guidelines.
48
49##### Thrift 0.15.0
50
51* **[THRIFT-5360]** - No longer define OR generate `description()` methods for `Error` types.
52
53 `Error.description()` was soft-deprecated in 1.27, and deprecated as of 1.41.
54 Library error types also do not implement `Error.description()`. Also, as a result of this change
55 the generated Rust representation of an Error no longer implements the `Error.description()` method.
56 Instead, it generates a `Display` impl with the same information.
57
58 For example:
59
60 ```thrift
61 exception Xception {
62 1: i32 errorCode,
63 2: string message
64 }
65 ```
66
67 used to generate:
68
69 ```rust
70 use std::error::Error;
71 use std::fmt;
72 use std::fmt::{Display, Formatter};
73
74 // auto-generated by the Thrift compiler
75 #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
76 pub struct Xception {
77 pub error_code: Option<i32>,
78 pub message: Option<String>,
79 }
80
81 // auto-generated by the Thrift compiler
82 impl Error for Xception {
83 fn description(&self) -> &str {
84 "remote service threw Xception"
85 }
86 }
87
88 // auto-generated by the Thrift compiler
89 impl From<Xception> for thrift::Error {
90 fn from(e: Xception) -> Self {
91 thrift::Error::User(Box::new(e))
92 }
93 }
94
95 // auto-generated by the Thrift compiler
96 impl Display for Xception {
97 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
98 self.description().format(f)
99 }
100 }
101 ```
102
103 It *now* generates:
104
105 ```rust
106 use std::error::Error;
107 use std::fmt;
108 use std::fmt::{Display, Formatter};
109
110 // auto-generated by the Thrift compiler
111 #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
112 pub struct Xception {
113 pub error_code: Option<i32>,
114 pub message: Option<String>,
115 }
116
117 // auto-generated by the Thrift compiler
118 impl Error for Xception { }
119
120 // auto-generated by the Thrift compiler
121 impl From<Xception> for thrift::Error {
122 fn from(e: Xception) -> Self {
123 thrift::Error::User(Box::new(e))
124 }
125 }
126
127 // auto-generated by the Thrift compiler
128 impl Display for Xception {
129 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
130 write!(f, "remote service threw Xception")
131 }
132 }
133 ```
134
135* **[THRIFT-5314]** - Generate enums implementations that are forward compatible (i.e. don't error on unknown values)
136
137 As a result of this change the Rust representation of an enum changes from a standard
138 Rust enum into a newtype struct with associated constants.
139
140 For example:
141
142 ```thrift
143 // THRIFT
144 enum Operation {
145 ADD,
146 SUBTRACT,
147 MULTIPLY,
148 DIVIDE,
149 }
150 ```
151
152 used to generate:
153
154 ```rust
155 // OLD AUTO-GENERATED RUST
156 pub enum Operation {
157 Add,
158 Subtract,
159 Multiply,
160 Divide,
161 }
162 ```
163
164 It *now* generates:
165
166 ```rust
167 // NEW AUTO-GENERATED RUST
168 pub struct Operation(pub i32);
169
170 impl Operation {
171 pub const ADD: Operation = Operation(0);
172 pub const SUBTRACT: Operation = Operation(1);
173 pub const MULTIPLY: Operation = Operation(2);
174 pub const DIVIDE: Operation = Operation(3);
175 }
176 ```
177
178##### Thrift 0.14.0
179
180* **[THRIFT-5158]** - Rust library and generator now support Rust 2021 only. Required rust 1.61.0 or higher
181
182 The Rust `thrift` library was updated to Rust 2021 via `cargo fix --edition`.
183 All test code in the repo was updated as well. The code generator was also updated
184 to support Rust 2021 only.
185
186##### Thrift 0.13.0
187
188* **[THRIFT-4536]** - Use TryFrom from std, required rust 1.34.0 or higher
189
190 Previously TryFrom was from try_from crate, it is now from the std library,
191 but this functionality is only available in rust 1.34.0. Additionally,
192 ordered-float is now re-exported under the thrift module to reduce
193 possible dependency mismatches.
194
195##### Thrift 0.12.0
196
197* **[THRIFT-4529]** - Rust enum variants are now camel-cased instead of uppercased to conform to Rust naming conventions
198
199 Previously, enum variants were uppercased in the auto-generated code.
200 For example, the following thrift enum:
201
202 ```thrift
203 // THRIFT
204 enum Operation {
205 ADD,
206 SUBTRACT,
207 MULTIPLY,
208 DIVIDE,
209 }
210 ```
211
212 used to generate:
213
214 ```rust
215 // OLD AUTO-GENERATED RUST
216 pub enum Operation {
217 ADD,
218 SUBTRACT,
219 MULTIPLY,
220 DIVIDE,
221 }
222 ```
223
224 It *now* generates:
225
226 ```rust
227 // NEW AUTO-GENERATED RUST
228 pub enum Operation {
229 Add,
230 Subtract,
231 Multiply,
232 Divide,
233 }
234 ```
235
236 You will have to change all enum variants in your code to use camel-cased names.
237 This should be a search and replace.
238
239## Contributing
240
241Bug reports and PRs are always welcome! Please see the
242[Thrift website](https://thrift.apache.org/) for more details.
243
244Thrift Rust support requires code in several directories:
245
246* `compiler/cpp/src/thrift/generate/t_rs_generator.cc`: binding code generator
247* `lib/rs`: runtime library
248* `lib/rs/test`: supplemental tests
249* `tutorial/rs`: tutorial client and server
250* `test/rs`: cross-language test client and server
251
252All library code, test code and auto-generated code compiles and passes clippy
253without warnings. All new code must do the same! When making changes ensure that:
254
255* `rustc` does does output any warnings
256* `clippy` with default settings does not output any warnings (includes auto-generated code)
257* `cargo test` is successful
258* `make precross` and `make check` are successful
259* `tutorial/bin/tutorial_client` and `tutorial/bin/tutorial_server` communicate
260