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
//! [Cap'n Proto] encoding for [`decimix`]'s [`Dec19`] and [`UDec19`].
//!
//! [Cap'n Proto]: https://capnproto.org
//!
//! This crate ships a schema, `decimix.capnp`, with a type for each of the
//! two decimals, and the Rust code to read and write them. It needs the
//! `newtype` branch of capnproto-rust, which adds schema-level `type`
//! declarations.
//!
//! # Using it in your schema
//!
//! Import `decimix.capnp` and use `Decimix.Dec19` or `Decimix.UDec19` as a
//! field's type. Each value takes two 64-bit words, so the field takes two
//! ordinals, one per word:
//!
//! ```capnp
//! using Decimix = import "/decimix.capnp";
//!
//! struct Order {
//! id @0 :UInt32;
//! price @[1-2] :Decimix.Dec19;
//! qty @[3-4] :Decimix.UDec19;
//! }
//! ```
//!
//! Add `capnp-decimix` to both `[dependencies]` and `[build-dependencies]`.
//! In your `build.rs`, add [`import_path`] (the directory holding
//! `decimix.capnp`) to the import path, and tell the code generator, with
//! [`SCHEMA_ID`], that this crate provides that schema's generated code, so
//! yours refers to it rather than expecting its own copy:
//!
//! ```ignore
//! capnpc::CompilerCommand::new()
//! .import_path(capnp_decimix::import_path())
//! .crate_provides("capnp_decimix", [capnp_decimix::SCHEMA_ID])
//! .file("order.capnp")
//! .run()
//! .unwrap();
//! ```
//!
//! If your build system can't use [`import_path`] (see its caveat), copy
//! `decimix.capnp` into your project instead, as is usual for Cap'n Proto
//! schemas, and keep the [`SCHEMA_ID`] line.
//!
//! # Reading and writing
//!
//! With the [`prelude`] in scope, a decimal field's reader has `get_dec19`
//! (or `get_udec19`) and its builder has `set_dec19` (or `set_udec19`):
//!
//! ```
//! # use capnp_decimix_tests::basic_capnp;
//! use capnp_decimix::prelude::*;
//! use decimix::{dec, udec};
//!
//! # fn main() {
//! let mut message = capnp::message::Builder::new_default();
//! let mut order = message.init_root::<basic_capnp::order::Builder<'_>>();
//! order.set_id(7);
//! order.reborrow().get_price().set_dec19(dec!(-101.25));
//! order.reborrow().get_qty().set_udec19(udec!(3));
//!
//! let order = order.into_reader();
//! assert_eq!(order.get_price().get_dec19(), dec!(-101.25));
//! assert_eq!(order.get_qty().get_udec19(), udec!(3));
//! # }
//! ```
//!
//! These work on every field of these types, in any struct, and on the
//! type-erased `as_any()` readers and builders. The same conversions are
//! available as free functions in [`dec19`] and [`udec19`].
//!
//! A field that isn't in the message, e.g. one written by a program using an
//! older schema without it, reads as zero (Cap'n Proto's default). So adding
//! a decimal field to a struct is backward compatible, but a reader can't
//! tell zero from "not sent".
//!
//! # The wire format
//!
//! A value is its stored integer: the value in steps of 10⁻¹⁹, i.e. the
//! value × 10,000,000,000,000,000,000 (see [`Dec19::to_raw`]). That 128-bit
//! integer is split into two `UInt64` fields: `lo` holds the low 64 bits and
//! `hi` the high 64 bits. For `Dec19` the integer is two's complement, so the
//! top bit of `hi` is the sign. For example:
//!
//! | Value | `hi` | `lo` |
//! |---------------------|-------------------------|-------------------------|
//! | 1 | `0` | `0x8AC7_2304_89E8_0000` |
//! | −1 | `0xFFFF_FFFF_FFFF_FFFF` | `0x7538_DCFB_7618_0000` |
//! | −0.000…01 (1 step) | `0xFFFF_FFFF_FFFF_FFFF` | `0xFFFF_FFFF_FFFF_FFFF` |
//!
//! (1 is stored as 10¹⁹ = `0x8AC7_2304_89E8_0000`, which fits in `lo`; −1 is
//! stored as −10¹⁹, whose low word is 2⁶⁴ − 10¹⁹.)
//!
//! Every pair of words is a valid value, so decoding can't fail, and every
//! value has exactly one encoding.
//!
//! Cap'n Proto stores each `UInt64` little-endian whatever the host, and
//! this crate splits and joins the words arithmetically rather than by
//! reinterpreting memory, so the format is the same on big- and
//! little-endian hosts. Laid out in a struct, the two words are a
//! little-endian 128-bit integer if `lo`'s word comes first, as it does for
//! consecutive ordinals like `@[1-2]`; don't rely on that, read the fields.
//!
//! # Why two words rather than `Data`
//!
//! Cap'n Proto has no 128-bit integer. The alternative is a 16-byte `Data`
//! field holding [`Dec19::to_le_bytes`], but `Data` is stored behind a
//! pointer (an extra indirection and 8 more bytes per value), its length has
//! to be checked at run time, and a type alias for it is still just `Data`
//! in the generated code, so a decimal can't be told apart from any other
//! bytes. A `type` group is stored inline in the struct, and the generated
//! code gives each one its own `Reader` and `Builder` traits, which this
//! crate implements its conversions on.
pub use ;
/// The directory holding `decimix.capnp`, for a `build.rs` to pass to
/// `capnpc::CompilerCommand::import_path`. Schemas then import it as
/// `"/decimix.capnp"`.
///
/// This is where cargo unpacked this crate's source (the registry, git or
/// path checkout), recorded when the crate was compiled. Build scripts run on
/// the machine that compiled it, so the path exists for them under plain
/// cargo. A build system that compiles in one place and builds in another
/// (e.g. a shared cache of compiled crates, or a sandbox that is removed
/// afterwards) can leave a stale path; copy the schema instead there.
/// The file ID of `decimix.capnp` (its `@0x...` line), for a `build.rs` to
/// pass to `capnpc::CompilerCommand::crate_provides` along with the crate
/// name `"capnp_decimix"`.
pub const SCHEMA_ID: u64 = 0xf534_c073_fd15_b2d4;
/// The code generated from `decimix.capnp`: a `Reader` and `Builder` trait
/// for each type, implemented by every field of that type.
///
/// You rarely need these directly: the [`prelude`] methods are implemented
/// on them.
/// Methods for reading and writing decimal fields: `use
/// capnp_decimix::prelude::*`.
///
/// The traits here are implemented for every reader and builder of a
/// `Decimix.Dec19` or `Decimix.UDec19` field, so you don't name them; they
/// just need to be in scope.
/// [`Dec19`] to and from a `Decimix.Dec19` field.
///
/// The [`prelude`] methods call these.
/// [`UDec19`] to and from a `Decimix.UDec19` field.
///
/// The [`prelude`] methods call these.