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
//! Rust code generator for safe, fixed size, evolving records.
//!
//! ## Objectives
//!
//! The objectives of this crate are:
//!
//! * define fixed size Rust structures to represent records of data
//! * define strongly typed accessors to data
//! * allow record evolution: data replacement by other data of different type
//! * optimize memory copies when records are evolving
//! * entirely safe memory management, even on panic
//!
//! Whether these objectives are met or not is still to be proven.
//!
//! ## Why and how
//!
//! ### Fixed size
//!
//! Having fixed size records aims at optimizing allocations: it is easy to manage memory when millions of objects have all the same size.
//!
//! This is achieved by leveraging the const generic Rust feature: the size of records is statically known.
//!
//! ### Strong typing
//!
//! Strong typing is a very interesting aspect of the Rust language. The only thing a developer has to do to have strong typing is to use types. _Truc_ is made to allow the use of as many types as possible, even user-defined types. There may be restrictions, especially on lifetimes, but any owned type can be used.
//!
//! ### Record evolution
//!
//! It is often nice to make a record evolve, keeping only one invariant: its size.
//!
//! This is achieved by defining record variants and ways to:
//!
//! * destructure the data
//! * convert from one variant to the next one
//! * convert a list of records of one variant to a list of records of another variant
//!
//! There is no direct way to convert from one variant to any arbitrary other variant (because use cases have to be defined), but the language allows almost anything provided it compiles.
//!
//! ### Memory copies
//!
//! Memory copies are expensive, one should try to avoid them as much as possible.
//!
//! A datum in a record is always stored at the same location in that record. If it's not at the same location then it is not the same datum. With the help of the compiler, memory copies should be optimized.
//!
//! To be proven:
//!
//! * Can the compiler optimize the conversion from one variant to another? That sounds optimistic, but one can be surprised by the level of optimization of LLVM.
//! * Does it work on both the heap and the stack?
//!
//! ### Safe memory management
//!
//! Code generated by _Truc_ is made to be entirely safe. It is built on top of some heavily `unsafe` calls but the API only exposes safe functions:
//!
//! * the types size and alignment is respected (otherwise the code would panic)
//! * everything held by a record is safely managed: dropping a record drops the data it holds
//! * the vector conversion is safe even if the conversion panics in the middle of the loop: old data and new data is dropped according to the type system rules, no value is missed
//!
//! ## Additional cool features
//!
//! * cross-compilation enabled
//!
//! ## Getting started
//!
//! ### Project organization
//!
//! Usually a project is organized this way:
//!
//! * generation of the record definitions in `build.rs`
//! * import of the generated definitions in a module of the project
//! * project implementation
//!
//! Example `Cargo.toml`:
//!
//! ```toml
//! [package]
//! name = "readme"
//! version = "0.1.0"
//! edition = "2021"
//!
//! [dependencies]
//! static_assertions = "1"
//! truc_runtime = { git = "https://github.com/arnodb/truc.git" }
//!
//! [build-dependencies]
//! truc = { git = "https://github.com/arnodb/truc.git" }
//! ```
//!
//! ### Record definitions
//!
//! First of all you need a type resolver. If you are not cross-compiling then
//! [HostTypeResolver](crate::record::type_resolver::HostTypeResolver) will work in most cases.
//!
//! Then the definitions are built with
//! [NativeRecordDefinitionBuilder](crate::record::definition::builder::native::NativeRecordDefinitionBuilder).
//!
//! Once you have your definitions set up, you just need to generate the Rust definitions to an output file.
//!
//! Example `build.rs`:
//!
//! ```rust,no_run
//! use std::{env, fs::File, io::Write, path::PathBuf};
//!
//! use truc::{
//! generator::{config::GeneratorConfig, generate},
//! record::{
//! definition::builder::native::NativeRecordDefinitionBuilder,
//! type_resolver::HostTypeResolver,
//! },
//! };
//!
//! fn main() {
//! let mut definition = NativeRecordDefinitionBuilder::new(&HostTypeResolver);
//!
//! // First variant with an integer
//! let integer_id = definition.add_datum_allow_uninit::<usize, _>("integer").unwrap();
//! definition.close_record_variant();
//!
//! // Second variant with a string
//! let string_id = definition.add_datum::<String, _>("string").unwrap();
//! definition.remove_datum(integer_id).unwrap();
//! definition.close_record_variant();
//!
//! // Remove the integer and replace it with another
//! definition.add_datum_allow_uninit::<isize, _>("signed_integer").unwrap();
//! definition.remove_datum(string_id).unwrap();
//! definition.close_record_variant();
//!
//! // Build
//! let definition = definition.build();
//!
//! // Generate Rust definitions
//! let out_dir = env::var("OUT_DIR").expect("OUT_DIR");
//! let out_dir_path = PathBuf::from(out_dir);
//! let mut file = File::create(out_dir_path.join("readme_truc.rs")).unwrap();
//! write!(
//! file,
//! "{}",
//! generate(&definition, &GeneratorConfig::default())
//! )
//! .unwrap();
//! }
//! ```
//!
//! ### Project implementation
//!
//! ```text
//! #[macro_use]
//! extern crate static_assertions;
//!
//! #[allow(dead_code)]
//! #[allow(clippy::borrowed_box)]
//! #[allow(clippy::module_inception)]
//! mod truc {
//! include!(concat!(env!("OUT_DIR"), "/readme_truc.rs"));
//! }
//!
//! fn main() {
//! use crate::truc::*;
//!
//! for record_2 in (0..42)
//! .into_iter()
//! .map(|integer| Record0::new(UnpackedRecord0 { integer }))
//! .map(|mut record_0| {
//! (*record_0.integer_mut()) *= 2;
//! record_0
//! })
//! .map(|record_0| {
//! let string = record_0.integer().to_string();
//! Record1::from((record_0, UnpackedRecordIn1 { string }))
//! })
//! .map(|record_1| {
//! let UnpackedRecord1 { string } = record_1.unpack();
//! Record2::new(UnpackedRecord2 {
//! signed_integer: string.parse().unwrap(),
//! })
//! })
//! {
//! println!("{}", record_2.signed_integer());
//! }
//! }
//! ```
extern crate assert_matches;
extern crate derive_more;
extern crate derive_new;
extern crate quote;