Skip to content

Commit e9f20c1

Browse files
committed
Make rustfmt happy
Signed-off-by: Uli Schlachter <[email protected]>
1 parent a1f22a5 commit e9f20c1

File tree

3 files changed

+192
-45
lines changed

3 files changed

+192
-45
lines changed

examples/xeyes.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use x11rb::errors::{ConnectionError, ReplyOrIdError};
77
use x11rb::protocol::shape::{self, ConnectionExt as _};
88
use x11rb::protocol::xproto::*;
99
use x11rb::protocol::Event;
10-
use x11rb::wrapper::{ConnectionExt as _,};
10+
use x11rb::wrapper::ConnectionExt as _;
1111
use x11rb::COPY_DEPTH_FROM_PARENT;
1212

1313
const PUPIL_SIZE: i16 = 50;
@@ -175,7 +175,13 @@ fn shape_window<C: Connection>(
175175
// Draw the eyes as "not transparent"
176176
let values = ChangeGCAux::new().foreground(1);
177177
conn.change_gc(gc.gcontext(), &values)?;
178-
draw_eyes(conn, pixmap.pixmap(), gc.gcontext(), gc.gcontext(), window_size)?;
178+
draw_eyes(
179+
conn,
180+
pixmap.pixmap(),
181+
gc.gcontext(),
182+
gc.gcontext(),
183+
window_size,
184+
)?;
179185

180186
// Set the shape of the window
181187
conn.shape_mask(shape::SO::SET, shape::SK::BOUNDING, win_id, 0, 0, &pixmap)?;

generator/src/generator/namespace/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
6767
}
6868
}
6969

70-
fn generate(&self, out: &mut Output, enum_cases: &mut EnumCases, resource_info: &[super::ResourceInfo<'_>]) {
70+
fn generate(
71+
&self,
72+
out: &mut Output,
73+
enum_cases: &mut EnumCases,
74+
resource_info: &[super::ResourceInfo<'_>],
75+
) {
7176
super::write_code_header(out);
7277
header::write_header(out, &self.ns);
7378

generator/src/generator/namespace/resource_wrapper.rs

+178-42
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::rc::Rc;
22

33
use xcbgen::defs as xcbdefs;
44

5+
use super::super::{camel_case_to_lower_snake, CreateInfo, ResourceInfo};
56
use super::{
67
gather_deducible_fields, to_rust_type_name, to_rust_variable_name, NamespaceGenerator, Output,
78
};
8-
use super::super::{camel_case_to_lower_snake, CreateInfo, ResourceInfo};
99

1010
pub(super) fn generate(
1111
generator: &NamespaceGenerator<'_, '_>,
@@ -16,34 +16,74 @@ pub(super) fn generate(
1616
let free_function = camel_case_to_lower_snake(&info.free_request);
1717
let wrapper = format!("{}Wrapper", info.resource_name);
1818
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+
);
2024
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+
);
2230
outln!(out, "///");
2331
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+
);
2536
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+
);
2743
outln!(out, "");
2844
outln!(out, "impl<'c, C: RequestConnection> {}<'c, C>", wrapper);
2945
outln!(out, "{{");
3046
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+
);
3357
outln!(out.indent(), "{}(conn, id)", wrapper);
3458
outln!(out, "}}");
3559
outln!(out, "");
3660

3761
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+
);
3968
outln!(out.indent(), "self.1");
4069
outln!(out, "}}");
4170
outln!(out, "");
4271

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+
);
4476
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+
);
4787
outln!(out.indent(), "let id = self.1;");
4888
outln!(out.indent(), "std::mem::forget(self);");
4989
outln!(out.indent(), "id");
@@ -57,21 +97,37 @@ pub(super) fn generate(
5797
out.indented(|out| {
5898
for create_request in info.create_requests.iter() {
5999
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+
);
61108
}
62109
}
63110
});
64111
outln!(out, "}}");
65112
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+
);
67119
out.indented(|out| {
68120
outln!(out, "fn from(from: &{}<'_, C>) -> Self {{", wrapper);
69121
outln!(out.indent(), "from.1");
70122
outln!(out, "}}");
71123
});
72124
outln!(out, "}}");
73125
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+
);
75131
out.indented(|out| {
76132
outln!(out, "fn drop(&mut self) {{");
77133
outln!(out.indent(), "let _ = (self.0).{}(self.1);", free_function);
@@ -89,14 +145,19 @@ fn generate_creator(
89145
lower_name: &str,
90146
) {
91147
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+
);
100161
let request_fields = request_def.fields.borrow();
101162
let deducible_fields = gather_deducible_fields(&*request_fields);
102163

@@ -153,19 +214,24 @@ fn generate_creator(
153214
xcbdefs::FieldDef::List(list_field) => {
154215
let field_name = to_rust_variable_name(&list_field.name);
155216
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);
157219
let field_type = if let Some(list_len) = list_field.length() {
158220
format!("&[{}; {}]", element_type, list_len)
159221
} else {
160222
format!("&[{}]", element_type)
161223
};
162224
(field_name, field_type)
163225
}
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+
)
167230
};
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+
{
169235
function_args.push_str(&format!(", {}: {}", rust_field_name, rust_field_type));
170236

171237
forward_args_without_resource.push(rust_field_name.clone());
@@ -181,34 +247,104 @@ fn generate_creator(
181247
};
182248

183249
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+
);
185255
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+
);
189272
outln!(out, "/// [{}].", function_name);
190273
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+
);
193286
emit_where(out, &wheres);
194287
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+
);
198305
outln!(out, "}}");
199306
outln!(out, "");
200307

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+
);
202313
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+
);
205326
outln!(out, "/// it in `Drop`.");
206327
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+
);
209340
emit_where(out, &wheres);
210341
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+
);
212348
outln!(out, "}}");
213349
}
214350

0 commit comments

Comments
 (0)