@@ -2,10 +2,10 @@ use std::rc::Rc;
2
2
3
3
use xcbgen:: defs as xcbdefs;
4
4
5
+ use super :: super :: { camel_case_to_lower_snake, CreateInfo , ResourceInfo } ;
5
6
use super :: {
6
7
gather_deducible_fields, to_rust_type_name, to_rust_variable_name, NamespaceGenerator , Output ,
7
8
} ;
8
- use super :: super :: { camel_case_to_lower_snake, CreateInfo , ResourceInfo } ;
9
9
10
10
pub ( super ) fn generate (
11
11
generator : & NamespaceGenerator < ' _ , ' _ > ,
@@ -16,34 +16,74 @@ pub(super) fn generate(
16
16
let free_function = camel_case_to_lower_snake ( & info. free_request ) ;
17
17
let wrapper = format ! ( "{}Wrapper" , info. resource_name) ;
18
18
outln ! ( out, "" ) ;
19
- outln ! ( out, "/// A RAII-like wrapper around a [{}]." , info. resource_name) ;
19
+ outln ! (
20
+ out,
21
+ "/// A RAII-like wrapper around a [{}]." ,
22
+ info. resource_name,
23
+ ) ;
20
24
outln ! ( out, "///" ) ;
21
- outln ! ( out, "/// Instances of this struct represent a {} that is freed in `Drop`." , info. resource_name) ;
25
+ outln ! (
26
+ out,
27
+ "/// Instances of this struct represent a {} that is freed in `Drop`." ,
28
+ info. resource_name,
29
+ ) ;
22
30
outln ! ( out, "///" ) ;
23
31
outln ! ( out, "/// Any errors during `Drop` are silently ignored. Most likely an error here means that your" ) ;
24
- outln ! ( out, "/// X11 connection is broken and later requests will also fail." ) ;
32
+ outln ! (
33
+ out,
34
+ "/// X11 connection is broken and later requests will also fail." ,
35
+ ) ;
25
36
outln ! ( out, "#[derive(Debug)]" ) ;
26
- outln ! ( out, "pub struct {wrapper}<'c, C: RequestConnection>(&'c C, {name});" , name = info. resource_name, wrapper = wrapper) ;
37
+ outln ! (
38
+ out,
39
+ "pub struct {wrapper}<'c, C: RequestConnection>(&'c C, {name});" ,
40
+ name = info. resource_name,
41
+ wrapper = wrapper,
42
+ ) ;
27
43
outln ! ( out, "" ) ;
28
44
outln ! ( out, "impl<'c, C: RequestConnection> {}<'c, C>" , wrapper) ;
29
45
outln ! ( out, "{{" ) ;
30
46
out. indented ( |out| {
31
- outln ! ( out, "/// Assume ownership of the given resource and destroy it in `Drop`." ) ;
32
- outln ! ( out, "pub fn for_{}(conn: &'c C, id: {}) -> Self {{" , lower_name, info. resource_name) ;
47
+ outln ! (
48
+ out,
49
+ "/// Assume ownership of the given resource and destroy it in `Drop`." ,
50
+ ) ;
51
+ outln ! (
52
+ out,
53
+ "pub fn for_{}(conn: &'c C, id: {}) -> Self {{" ,
54
+ lower_name,
55
+ info. resource_name,
56
+ ) ;
33
57
outln ! ( out. indent( ) , "{}(conn, id)" , wrapper) ;
34
58
outln ! ( out, "}}" ) ;
35
59
outln ! ( out, "" ) ;
36
60
37
61
outln ! ( out, "/// Get the XID of the wrapped resource" ) ;
38
- outln ! ( out, "pub fn {}(&self) -> {} {{" , lower_name, info. resource_name) ;
62
+ outln ! (
63
+ out,
64
+ "pub fn {}(&self) -> {} {{" ,
65
+ lower_name,
66
+ info. resource_name,
67
+ ) ;
39
68
outln ! ( out. indent( ) , "self.1" ) ;
40
69
outln ! ( out, "}}" ) ;
41
70
outln ! ( out, "" ) ;
42
71
43
- outln ! ( out, "/// Assume ownership of the XID of the wrapped resource" ) ;
72
+ outln ! (
73
+ out,
74
+ "/// Assume ownership of the XID of the wrapped resource" ,
75
+ ) ;
44
76
outln ! ( out, "///" ) ;
45
- outln ! ( out, "/// This function destroys this wrapper without freeing the underlying resource." ) ;
46
- outln ! ( out, "pub fn into_{}(self) -> {} {{" , lower_name, info. resource_name) ;
77
+ outln ! (
78
+ out,
79
+ "/// This function destroys this wrapper without freeing the underlying resource." ,
80
+ ) ;
81
+ outln ! (
82
+ out,
83
+ "pub fn into_{}(self) -> {} {{" ,
84
+ lower_name,
85
+ info. resource_name,
86
+ ) ;
47
87
outln ! ( out. indent( ) , "let id = self.1;" ) ;
48
88
outln ! ( out. indent( ) , "std::mem::forget(self);" ) ;
49
89
outln ! ( out. indent( ) , "id" ) ;
@@ -57,21 +97,37 @@ pub(super) fn generate(
57
97
out. indented ( |out| {
58
98
for create_request in info. create_requests . iter ( ) {
59
99
if let Some ( request) = create_request {
60
- generate_creator ( generator, out, request, info. resource_name , & wrapper, & lower_name) ;
100
+ generate_creator (
101
+ generator,
102
+ out,
103
+ request,
104
+ info. resource_name ,
105
+ & wrapper,
106
+ & lower_name,
107
+ ) ;
61
108
}
62
109
}
63
110
} ) ;
64
111
outln ! ( out, "}}" ) ;
65
112
outln ! ( out, "" ) ;
66
- outln ! ( out, "impl<C: RequestConnection> From<&{wrapper}<'_, C>> for {name} {{" , name = info. resource_name, wrapper = wrapper) ;
113
+ outln ! (
114
+ out,
115
+ "impl<C: RequestConnection> From<&{wrapper}<'_, C>> for {name} {{" ,
116
+ name = info. resource_name,
117
+ wrapper = wrapper,
118
+ ) ;
67
119
out. indented ( |out| {
68
120
outln ! ( out, "fn from(from: &{}<'_, C>) -> Self {{" , wrapper) ;
69
121
outln ! ( out. indent( ) , "from.1" ) ;
70
122
outln ! ( out, "}}" ) ;
71
123
} ) ;
72
124
outln ! ( out, "}}" ) ;
73
125
outln ! ( out, "" ) ;
74
- outln ! ( out, "impl<C: RequestConnection> Drop for {}<'_, C> {{" , wrapper) ;
126
+ outln ! (
127
+ out,
128
+ "impl<C: RequestConnection> Drop for {}<'_, C> {{" ,
129
+ wrapper,
130
+ ) ;
75
131
out. indented ( |out| {
76
132
outln ! ( out, "fn drop(&mut self) {{" ) ;
77
133
outln ! ( out. indent( ) , "let _ = (self.0).{}(self.1);" , free_function) ;
@@ -89,14 +145,19 @@ fn generate_creator(
89
145
lower_name : & str ,
90
146
) {
91
147
let request_name = request_info. request_name ;
92
- let request_def = Rc :: clone ( generator
93
- . ns
94
- . request_defs
95
- . borrow ( )
96
- . get ( request_name)
97
- . unwrap_or_else ( || {
98
- panic ! ( "Did not find request {} in namespace {}" , request_name, generator. ns. header) ;
99
- } ) ) ;
148
+ let request_def = Rc :: clone (
149
+ generator
150
+ . ns
151
+ . request_defs
152
+ . borrow ( )
153
+ . get ( request_name)
154
+ . unwrap_or_else ( || {
155
+ panic ! (
156
+ "Did not find request {} in namespace {}" ,
157
+ request_name, generator. ns. header,
158
+ ) ;
159
+ } ) ,
160
+ ) ;
100
161
let request_fields = request_def. fields . borrow ( ) ;
101
162
let deducible_fields = gather_deducible_fields ( & * request_fields) ;
102
163
@@ -153,19 +214,24 @@ fn generate_creator(
153
214
xcbdefs:: FieldDef :: List ( list_field) => {
154
215
let field_name = to_rust_variable_name ( & list_field. name ) ;
155
216
assert ! ( generator. rust_value_type_is_u8( & list_field. element_type) ) ;
156
- let element_type = generator. field_value_type_to_rust_type ( & list_field. element_type ) ;
217
+ let element_type =
218
+ generator. field_value_type_to_rust_type ( & list_field. element_type ) ;
157
219
let field_type = if let Some ( list_len) = list_field. length ( ) {
158
220
format ! ( "&[{}; {}]" , element_type, list_len)
159
221
} else {
160
222
format ! ( "&[{}]" , element_type)
161
223
} ;
162
224
( field_name, field_type)
163
225
}
164
- xcbdefs:: FieldDef :: Switch ( switch_field) => {
165
- ( to_rust_variable_name ( & switch_field. name ) , format ! ( "&{}Aux" , to_rust_type_name( request_name) ) )
166
- }
226
+ xcbdefs:: FieldDef :: Switch ( switch_field) => (
227
+ to_rust_variable_name ( & switch_field. name ) ,
228
+ format ! ( "&{}Aux" , to_rust_type_name( request_name) ) ,
229
+ )
167
230
} ;
168
- if !request_info. created_argument . eq_ignore_ascii_case ( & rust_field_name) {
231
+ if !request_info
232
+ . created_argument
233
+ . eq_ignore_ascii_case ( & rust_field_name)
234
+ {
169
235
function_args. push_str ( & format ! ( ", {}: {}" , rust_field_name, rust_field_type) ) ;
170
236
171
237
forward_args_without_resource. push ( rust_field_name. clone ( ) ) ;
@@ -181,34 +247,104 @@ fn generate_creator(
181
247
} ;
182
248
183
249
outln ! ( out, "" ) ;
184
- outln ! ( out, "/// Create a new {name} and return a {name} wrapper and a cookie." , name = resource_name) ;
250
+ outln ! (
251
+ out,
252
+ "/// Create a new {name} and return a {name} wrapper and a cookie." ,
253
+ name = resource_name,
254
+ ) ;
185
255
outln ! ( out, "///" ) ;
186
- outln ! ( out, "/// This is a thin wrapper around [{}] that allocates an id for the {}." , function_name, resource_name) ;
187
- outln ! ( out, "/// This function returns the resulting `{}` that owns the created {} and frees" , wrapper_name, resource_name) ;
188
- outln ! ( out, "/// it in `Drop`. This also returns a `VoidCookie` that comes from the call to" ) ;
256
+ outln ! (
257
+ out,
258
+ "/// This is a thin wrapper around [{}] that allocates an id for the {}." ,
259
+ function_name,
260
+ resource_name,
261
+ ) ;
262
+ outln ! (
263
+ out,
264
+ "/// This function returns the resulting `{}` that owns the created {} and frees" ,
265
+ wrapper_name,
266
+ resource_name,
267
+ ) ;
268
+ outln ! (
269
+ out,
270
+ "/// it in `Drop`. This also returns a `VoidCookie` that comes from the call to" ,
271
+ ) ;
189
272
outln ! ( out, "/// [{}]." , function_name) ;
190
273
outln ! ( out, "///" ) ;
191
- outln ! ( out, "/// Errors can come from the call to [X11Connection::generate_id] or [{}]." , function_name) ;
192
- outln ! ( out, "pub fn {}_and_get_cookie{}({}) -> Result<(Self, VoidCookie<'c, C>), ReplyOrIdError>" , function_name, generics_decl, function_args) ;
274
+ outln ! (
275
+ out,
276
+ "/// Errors can come from the call to [X11Connection::generate_id] or [{}]." ,
277
+ function_name,
278
+ ) ;
279
+ outln ! (
280
+ out,
281
+ "pub fn {}_and_get_cookie{}({}) -> Result<(Self, VoidCookie<'c, C>), ReplyOrIdError>" ,
282
+ function_name,
283
+ generics_decl,
284
+ function_args,
285
+ ) ;
193
286
emit_where ( out, & wheres) ;
194
287
outln ! ( out, "{{" ) ;
195
- outln ! ( out. indent( ) , "let {} = conn.generate_id()?;" , request_info. created_argument) ;
196
- outln ! ( out. indent( ) , "let cookie = conn.{}({})?;" , function_name, forward_args_with_resource. join( ", " ) ) ;
197
- outln ! ( out. indent( ) , "Ok((Self::for_{}(conn, {}), cookie))" , lower_name, request_info. created_argument) ;
288
+ outln ! (
289
+ out. indent( ) ,
290
+ "let {} = conn.generate_id()?;" ,
291
+ request_info. created_argument,
292
+ ) ;
293
+ outln ! (
294
+ out. indent( ) ,
295
+ "let cookie = conn.{}({})?;" ,
296
+ function_name,
297
+ forward_args_with_resource. join( ", " ) ,
298
+ ) ;
299
+ outln ! (
300
+ out. indent( ) ,
301
+ "Ok((Self::for_{}(conn, {}), cookie))" ,
302
+ lower_name,
303
+ request_info. created_argument,
304
+ ) ;
198
305
outln ! ( out, "}}" ) ;
199
306
outln ! ( out, "" ) ;
200
307
201
- outln ! ( out, "/// Create a new {name} and return a {name} wrapper" , name = resource_name) ;
308
+ outln ! (
309
+ out,
310
+ "/// Create a new {name} and return a {name} wrapper" ,
311
+ name = resource_name,
312
+ ) ;
202
313
outln ! ( out, "///" ) ;
203
- outln ! ( out, "/// This is a thin wrapper around [{}] that allocates an id for the {}." , function_name, resource_name) ;
204
- outln ! ( out, "/// This function returns the resulting `{}` that owns the created {} and frees" , wrapper_name, resource_name) ;
314
+ outln ! (
315
+ out,
316
+ "/// This is a thin wrapper around [{}] that allocates an id for the {}." ,
317
+ function_name,
318
+ resource_name,
319
+ ) ;
320
+ outln ! (
321
+ out,
322
+ "/// This function returns the resulting `{}` that owns the created {} and frees" ,
323
+ wrapper_name,
324
+ resource_name,
325
+ ) ;
205
326
outln ! ( out, "/// it in `Drop`." ) ;
206
327
outln ! ( out, "///" ) ;
207
- outln ! ( out, "/// Errors can come from the call to [X11Connection::generate_id] or [{}]." , function_name) ;
208
- outln ! ( out, "pub fn {}{}({}) -> Result<Self, ReplyOrIdError>" , function_name, generics_decl, function_args) ;
328
+ outln ! (
329
+ out,
330
+ "/// Errors can come from the call to [X11Connection::generate_id] or [{}]." ,
331
+ function_name,
332
+ ) ;
333
+ outln ! (
334
+ out,
335
+ "pub fn {}{}({}) -> Result<Self, ReplyOrIdError>" ,
336
+ function_name,
337
+ generics_decl,
338
+ function_args,
339
+ ) ;
209
340
emit_where ( out, & wheres) ;
210
341
outln ! ( out, "{{" ) ;
211
- outln ! ( out. indent( ) , "Ok(Self::{}_and_get_cookie(conn, {})?.0)" , function_name, forward_args_without_resource. join( ", " ) ) ;
342
+ outln ! (
343
+ out. indent( ) ,
344
+ "Ok(Self::{}_and_get_cookie(conn, {})?.0)" ,
345
+ function_name,
346
+ forward_args_without_resource. join( ", " ) ,
347
+ ) ;
212
348
outln ! ( out, "}}" ) ;
213
349
}
214
350
0 commit comments