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
use std::io::Write;
use wire_format;
pub struct CodeWriter<'a> {
writer: &'a mut (Write + 'a),
indent: String,
}
impl<'a> CodeWriter<'a> {
pub fn new(writer: &'a mut Write) -> CodeWriter<'a> {
CodeWriter {
writer: writer,
indent: "".to_string(),
}
}
pub fn write_line<S : AsRef<str>>(&mut self, line: S) {
(if line.as_ref().is_empty() {
self.writer.write_all("\n".as_bytes())
} else {
let s: String = [self.indent.as_ref(), line.as_ref(), "\n"].concat();
self.writer.write_all(s.as_bytes())
}).unwrap();
}
pub fn write_generated(&mut self) {
self.write_line("// This file is generated. Do not edit");
self.write_line("// @generated");
self.write_line("");
self.comment("https://github.com/Manishearth/rust-clippy/issues/702");
self.write_line("#![allow(unknown_lints)]");
self.write_line("#![allow(clippy)]");
self.write_line("");
self.write_line("#![cfg_attr(rustfmt, rustfmt_skip)]");
self.write_line("");
self.write_line("#![allow(box_pointers)]");
self.write_line("#![allow(dead_code)]");
self.write_line("#![allow(non_camel_case_types)]");
self.write_line("#![allow(non_snake_case)]");
self.write_line("#![allow(non_upper_case_globals)]");
self.write_line("#![allow(trivial_casts)]");
self.write_line("#![allow(unsafe_code)]");
self.write_line("#![allow(unused_imports)]");
self.write_line("#![allow(unused_results)]");
}
pub fn todo(&mut self, message: &str) {
self.write_line(format!("panic!(\"TODO: {}\");", message));
}
pub fn indented<F>(&mut self, cb: F)
where F : Fn(&mut CodeWriter)
{
cb(&mut CodeWriter {
writer: self.writer,
indent: format!("{} ", self.indent),
});
}
#[allow(dead_code)]
pub fn commented<F>(&mut self, cb: F)
where F : Fn(&mut CodeWriter)
{
cb(&mut CodeWriter {
writer: self.writer,
indent: format!("// {}", self.indent),
});
}
pub fn lazy_static<S1 : AsRef<str>, S2 : AsRef<str>>(&mut self, name: S1, ty: S2) {
self.stmt_block(format!("static mut {}: ::protobuf::lazy::Lazy<{}> = ::protobuf::lazy::Lazy", name.as_ref(), ty.as_ref()), |w| {
w.field_entry("lock", "::protobuf::lazy::ONCE_INIT");
w.field_entry("ptr", format!("0 as *const {}", ty.as_ref()));
});
}
pub fn lazy_static_decl_get<S1 : AsRef<str>, S2 : AsRef<str>, F>(&mut self, name: S1, ty: S2, init: F)
where F : Fn(&mut CodeWriter)
{
self.lazy_static(name.as_ref(), ty);
self.unsafe_expr(|w| {
w.write_line(format!("{}.get(|| {{", name.as_ref()));
w.indented(|w| init(w));
w.write_line(format!("}})"));
});
}
pub fn block<S1 : AsRef<str>, S2 : AsRef<str>, F>(&mut self, first_line: S1, last_line: S2, cb: F)
where F : Fn(&mut CodeWriter)
{
self.write_line(first_line.as_ref());
self.indented(cb);
self.write_line(last_line.as_ref());
}
pub fn expr_block<S : AsRef<str>, F>(&mut self, prefix: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.block(format!("{} {{", prefix.as_ref()), "}", cb);
}
pub fn stmt_block<S : AsRef<str>, F>(&mut self, prefix: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.block(format!("{} {{", prefix.as_ref()), "};", cb);
}
pub fn unsafe_expr<F>(&mut self, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block("unsafe", cb);
}
pub fn impl_self_block<S : AsRef<str>, F>(&mut self, name: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("impl {}", name.as_ref()), cb);
}
pub fn impl_for_block<S1 : AsRef<str>, S2 : AsRef<str>, F>(&mut self, tr: S1, ty: S2, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("impl {} for {}", tr.as_ref(), ty.as_ref()), cb);
}
pub fn unsafe_impl(&mut self, what: &str, for_what: &str) {
self.write_line(format!("unsafe impl {} for {} {{}}", what, for_what));
}
pub fn pub_struct<S : AsRef<str>, F>(&mut self, name: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("pub struct {}", name.as_ref()), cb);
}
pub fn def_struct<S : AsRef<str>, F>(&mut self, name: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("struct {}", name.as_ref()), cb);
}
pub fn pub_enum<F>(&mut self, name: &str, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("pub enum {}", name), cb);
}
pub fn pub_trait<F>(&mut self, name: &str, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("pub trait {}", name), cb);
}
pub fn field_entry<S1 : AsRef<str>, S2 : AsRef<str>>(&mut self, name: S1, value: S2) {
self.write_line(format!("{}: {},", name.as_ref(), value.as_ref()));
}
pub fn field_decl<S : AsRef<str>>(&mut self, name: S, field_type: &str) {
self.field_entry(name, field_type);
}
pub fn derive(&mut self, derive: &[&str]) {
let v: Vec<String> = derive.iter().map(|&s| s.to_string()).collect();
self.write_line(format!("#[derive({})]", v.join(",")));
}
pub fn allow(&mut self, what: &[&str]) {
let v: Vec<String> = what.iter().map(|&s| s.to_string()).collect();
self.write_line(format!("#[allow({})]", v.join(",")));
}
pub fn comment(&mut self, comment: &str) {
if comment.is_empty() {
self.write_line("//");
} else {
self.write_line(format!("// {}", comment));
}
}
pub fn fn_def(&mut self, sig: &str) {
self.write_line(format!("fn {};", sig));
}
pub fn pub_fn<S : AsRef<str>, F>(&mut self, sig: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("pub fn {}", sig.as_ref()), cb);
}
pub fn def_fn<S : AsRef<str>, F>(&mut self, sig: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("fn {}", sig.as_ref()), cb);
}
pub fn while_block<S : AsRef<str>, F>(&mut self, cond: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("while {}", cond.as_ref()), cb);
}
pub fn if_stmt<S : AsRef<str>, F>(&mut self, cond: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.stmt_block(format!("if {}", cond.as_ref()), cb);
}
pub fn if_else_stmt<S : AsRef<str>, F>(&mut self, cond: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.write_line(format!("if {} {{", cond.as_ref()));
self.write_line("} else {");
self.indented(cb);
self.write_line("}");
}
pub fn if_let_stmt<F>(&mut self, decl: &str, expr: &str, cb: F)
where F : Fn(&mut CodeWriter)
{
self.if_stmt(format!("let {} = {}", decl, expr), cb);
}
pub fn if_let_else_stmt<F>(&mut self, decl: &str, expr: &str, cb: F)
where F : Fn(&mut CodeWriter)
{
self.if_else_stmt(format!("let {} = {}", decl, expr), cb);
}
pub fn for_stmt<S1 : AsRef<str>, S2 : AsRef<str>, F>(&mut self, over: S1, varn: S2, cb: F)
where F : Fn(&mut CodeWriter)
{
self.stmt_block(format!("for {} in {}", varn.as_ref(), over.as_ref()), cb)
}
pub fn match_block<S : AsRef<str>, F>(&mut self, value: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.stmt_block(format!("match {}", value.as_ref()), cb);
}
pub fn match_expr<S : AsRef<str>, F>(&mut self, value: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.expr_block(format!("match {}", value.as_ref()), cb);
}
pub fn case_block<S : AsRef<str>, F>(&mut self, cond: S, cb: F)
where F : Fn(&mut CodeWriter)
{
self.block(format!("{} => {{", cond.as_ref()), "},", cb);
}
pub fn case_expr<S1 : AsRef<str>, S2 : AsRef<str>>(&mut self, cond: S1, body: S2) {
self.write_line(format!("{} => {},", cond.as_ref(), body.as_ref()));
}
pub fn error_unexpected_wire_type(&mut self, wire_type: &str) {
self.write_line(format!(
"return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type({}));",
wire_type));
}
pub fn assert_wire_type(&mut self, wire_type: wire_format::WireType) {
self.if_stmt(format!("wire_type != ::protobuf::wire_format::{:?}", wire_type), |w| {
w.error_unexpected_wire_type("wire_type");
});
}
}