| Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 1 | # Rust Thrift library |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This crate implements the components required to build a working Thrift server |
| 6 | and 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 | |
| 14 | The modules are layered as shown. The `generated` layer is code generated by the |
| 15 | Thrift compiler's Rust plugin. It uses the components defined in this crate to |
| 16 | serialize and deserialize types and implement RPC. Users interact with these |
| 17 | types 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 | |
| 33 | Add `thrift = "x.y.z"` to your `Cargo.toml`, where `x.y.z` is the version of the |
| 34 | Thrift compiler you're using. |
| 35 | |
| 36 | ## API Documentation |
| 37 | |
| 38 | Full [Rustdoc](https://docs.rs/thrift/) |
| 39 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 40 | ## Compatibility |
| 41 | |
| 42 | The Rust library and auto-generated code targets Rust versions 1.28+. |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 43 | It does not currently use any Rust 2021 features. |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 44 | |
| 45 | ### Breaking Changes |
| 46 | |
| 47 | Breaking changes are minimized. When they are made they will be outlined below with transition guidelines. |
| 48 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 49 | ##### Thrift 0.15.0 |
| 50 | |
| Allen George | a194276 | 2021-03-06 17:39:02 -0500 | [diff] [blame] | 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 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 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. |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 139 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 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: |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 153 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 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: |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 165 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 166 | ```rust |
| 167 | // NEW AUTO-GENERATED RUST |
| 168 | pub struct Operation(pub i32); |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 169 | |
| Allen George | 2e90ef5 | 2021-03-01 14:47:04 -0500 | [diff] [blame] | 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 | |
| Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 178 | ##### Thrift 0.14.0 |
| 179 | |
| Jiayu Liu | fbfa52c | 2023-11-07 13:47:24 +0800 | [diff] [blame] | 180 | * **[THRIFT-5158]** - Rust library and generator now support Rust 2021 only. Required rust 1.65.0 or higher |
| Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 181 | |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 182 | The Rust `thrift` library was updated to Rust 2021 via `cargo fix --edition`. |
| Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 183 | All test code in the repo was updated as well. The code generator was also updated |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 184 | to support Rust 2021 only. |
| Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 185 | |
| Danny Browning | 181d900 | 2019-04-15 09:50:24 -0600 | [diff] [blame] | 186 | ##### Thrift 0.13.0 |
| James E. King III | 057bebc | 2019-05-26 14:59:04 -0400 | [diff] [blame] | 187 | |
| Danny Browning | 181d900 | 2019-04-15 09:50:24 -0600 | [diff] [blame] | 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, |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 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 |
| Danny Browning | 181d900 | 2019-04-15 09:50:24 -0600 | [diff] [blame] | 193 | possible dependency mismatches. |
| 194 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 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 | ``` |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 211 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 212 | used to generate: |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 213 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 214 | ```rust |
| 215 | // OLD AUTO-GENERATED RUST |
| 216 | pub enum Operation { |
| 217 | ADD, |
| 218 | SUBTRACT, |
| 219 | MULTIPLY, |
| 220 | DIVIDE, |
| 221 | } |
| 222 | ``` |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 223 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 224 | It *now* generates: |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 225 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 226 | ```rust |
| 227 | // NEW AUTO-GENERATED RUST |
| 228 | pub enum Operation { |
| 229 | Add, |
| 230 | Subtract, |
| 231 | Multiply, |
| 232 | Divide, |
| 233 | } |
| 234 | ``` |
| Jiayu Liu | aa85593 | 2022-06-26 05:00:25 +0200 | [diff] [blame] | 235 | |
| GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 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 | |
| Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 239 | ## Contributing |
| 240 | |
| 241 | Bug reports and PRs are always welcome! Please see the |
| 242 | [Thrift website](https://thrift.apache.org/) for more details. |
| 243 | |
| 244 | Thrift 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 | |
| 252 | All library code, test code and auto-generated code compiles and passes clippy |
| 253 | without 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 |