Skip to content

Commit 66e78af

Browse files
vimpunkSimonSapin
authored andcommitted
Run rustfmt on everything
1 parent c754931 commit 66e78af

17 files changed

+1247
-787
lines changed

build.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ mod codegen {
3333
println!("cargo:rerun-if-changed={}", input.display());
3434

3535
// We have stack overflows on Servo's CI.
36-
let handle = Builder::new().stack_size(128 * 1024 * 1024).spawn(move || {
37-
match_byte::expand(&input, &output);
38-
}).unwrap();
36+
let handle = Builder::new()
37+
.stack_size(128 * 1024 * 1024)
38+
.spawn(move || {
39+
match_byte::expand(&input, &output);
40+
})
41+
.unwrap();
3942

4043
handle.join().unwrap();
4144
}

build/match_byte.rs

+28-21
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,27 @@ use syn::parse::{Parse, ParseStream, Result};
1212

1313
use proc_macro2::{Span, TokenStream};
1414

15-
struct MatchByteParser {
16-
}
15+
struct MatchByteParser {}
1716

1817
pub fn expand(from: &Path, to: &Path) {
1918
let mut source = String::new();
20-
File::open(from).unwrap().read_to_string(&mut source).unwrap();
19+
File::open(from)
20+
.unwrap()
21+
.read_to_string(&mut source)
22+
.unwrap();
2123
let ast = syn::parse_file(&source).expect("Parsing rules.rs module");
2224
let mut m = MatchByteParser {};
2325
let ast = m.fold_file(ast);
2426

25-
let code = ast.into_token_stream().to_string().replace("{ ", "{\n").replace(" }", "\n}");
26-
File::create(to).unwrap().write_all(code.as_bytes()).unwrap();
27+
let code = ast
28+
.into_token_stream()
29+
.to_string()
30+
.replace("{ ", "{\n")
31+
.replace(" }", "\n}");
32+
File::create(to)
33+
.unwrap()
34+
.write_all(code.as_bytes())
35+
.unwrap();
2736
}
2837

2938
struct MatchByte {
@@ -45,7 +54,7 @@ impl Parse for MatchByte {
4554
arms.push(input.call(syn::Arm::parse)?);
4655
}
4756
arms
48-
}
57+
},
4958
})
5059
}
5160
}
@@ -55,16 +64,14 @@ fn get_byte_from_expr_lit(expr: &Box<syn::Expr>) -> u8 {
5564
syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
5665
if let syn::Lit::Byte(ref byte) = *lit {
5766
byte.value()
58-
}
59-
else {
67+
} else {
6068
panic!("Found a pattern that wasn't a byte")
6169
}
62-
},
70+
}
6371
_ => unreachable!(),
6472
}
6573
}
6674

67-
6875
/// Expand a TokenStream corresponding to the `match_byte` macro.
6976
///
7077
/// ## Example
@@ -93,12 +100,12 @@ fn expand_match_byte(body: &TokenStream) -> syn::Expr {
93100

94101
for pat in &arm.pats {
95102
match pat {
96-
&syn::Pat::Lit(syn::PatLit{ref expr}) => {
103+
&syn::Pat::Lit(syn::PatLit { ref expr }) => {
97104
let value = get_byte_from_expr_lit(expr);
98105
if table[value as usize] == 0 {
99106
table[value as usize] = case_id as u8;
100107
}
101-
},
108+
}
102109
&syn::Pat::Range(syn::PatRange { ref lo, ref hi, .. }) => {
103110
let lo = get_byte_from_expr_lit(lo);
104111
let hi = get_byte_from_expr_lit(hi);
@@ -110,14 +117,14 @@ fn expand_match_byte(body: &TokenStream) -> syn::Expr {
110117
if table[hi as usize] == 0 {
111118
table[hi as usize] = case_id as u8;
112119
}
113-
},
120+
}
114121
&syn::Pat::Wild(_) => {
115122
for byte in table.iter_mut() {
116123
if *byte == 0 {
117124
*byte = case_id as u8;
118125
}
119126
}
120-
},
127+
}
121128
&syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
122129
assert_eq!(wildcard, None);
123130
wildcard = Some(ident);
@@ -126,7 +133,7 @@ fn expand_match_byte(body: &TokenStream) -> syn::Expr {
126133
*byte = case_id as u8;
127134
}
128135
}
129-
},
136+
}
130137
_ => {
131138
panic!("Unexpected pattern: {:?}. Buggy code ?", pat);
132139
}
@@ -159,11 +166,11 @@ fn expand_match_byte(body: &TokenStream) -> syn::Expr {
159166
impl Fold for MatchByteParser {
160167
fn fold_stmt(&mut self, stmt: syn::Stmt) -> syn::Stmt {
161168
match stmt {
162-
syn::Stmt::Item(syn::Item::Macro(syn::ItemMacro{ ref mac, .. })) => {
169+
syn::Stmt::Item(syn::Item::Macro(syn::ItemMacro { ref mac, .. })) => {
163170
if mac.path == parse_quote!(match_byte) {
164-
return syn::fold::fold_stmt(self, syn::Stmt::Expr(expand_match_byte(&mac.tts)))
171+
return syn::fold::fold_stmt(self, syn::Stmt::Expr(expand_match_byte(&mac.tts)));
165172
}
166-
},
173+
}
167174
_ => {}
168175
}
169176

@@ -172,11 +179,11 @@ impl Fold for MatchByteParser {
172179

173180
fn fold_expr(&mut self, expr: syn::Expr) -> syn::Expr {
174181
match expr {
175-
syn::Expr::Macro(syn::ExprMacro{ ref mac, .. }) => {
182+
syn::Expr::Macro(syn::ExprMacro { ref mac, .. }) => {
176183
if mac.path == parse_quote!(match_byte) {
177-
return syn::fold::fold_expr(self, expand_match_byte(&mac.tts))
184+
return syn::fold::fold_expr(self, expand_match_byte(&mac.tts));
178185
}
179-
},
186+
}
180187
_ => {}
181188
}
182189

macros/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
#[macro_use] extern crate procedural_masquerade;
5+
#[macro_use]
6+
extern crate procedural_masquerade;
67
extern crate phf_codegen;
78
extern crate proc_macro;
89
extern crate proc_macro2;
9-
#[macro_use] extern crate quote;
10+
#[macro_use]
11+
extern crate quote;
1012
extern crate syn;
1113

12-
#[allow(unused_imports)] use std::ascii::AsciiExt;
14+
use proc_macro2::{TokenStream, TokenTree};
1315
use quote::TokenStreamExt;
16+
#[allow(unused_imports)]
17+
use std::ascii::AsciiExt;
1418
use std::iter;
15-
use proc_macro2::{TokenStream, TokenTree};
1619

1720
define_proc_macros! {
1821
/// Input: the arms of a `match` expression.
@@ -88,12 +91,13 @@ define_proc_macros! {
8891
}
8992
}
9093

91-
fn max_len<I: Iterator<Item=usize>>(lengths: I) -> String {
94+
fn max_len<I: Iterator<Item = usize>>(lengths: I) -> String {
9295
let max_length = lengths.max().expect("expected at least one string");
9396
quote!( const MAX_LENGTH: usize = #max_length; ).to_string()
9497
}
9598

9699
fn string_literal(token: &TokenTree) -> String {
97-
let lit: syn::LitStr = syn::parse2(iter::once(token.clone()).collect()).expect(&format!("expected string literal, got {:?}", token));
100+
let lit: syn::LitStr = syn::parse2(iter::once(token.clone()).collect())
101+
.expect(&format!("expected string literal, got {:?}", token));
98102
lit.value()
99103
}

procedural-masquerade/lib.rs

+51-34
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,35 @@ macro_rules! define_proc_macros {
198198
pub fn _extract_input(derive_input: &str) -> &str {
199199
let mut input = derive_input;
200200

201-
for expected in &["#[allow(unused)]", "enum", "ProceduralMasqueradeDummyType", "{",
202-
"Input", "=", "(0,", "stringify!", "("] {
201+
for expected in &[
202+
"#[allow(unused)]",
203+
"enum",
204+
"ProceduralMasqueradeDummyType",
205+
"{",
206+
"Input",
207+
"=",
208+
"(0,",
209+
"stringify!",
210+
"(",
211+
] {
203212
input = input.trim_start();
204-
assert!(input.starts_with(expected),
205-
"expected prefix {:?} not found in {:?}", expected, derive_input);
213+
assert!(
214+
input.starts_with(expected),
215+
"expected prefix {:?} not found in {:?}",
216+
expected,
217+
derive_input
218+
);
206219
input = &input[expected.len()..];
207220
}
208221

209222
for expected in [")", ").0,", "}"].iter().rev() {
210223
input = input.trim_end();
211-
assert!(input.ends_with(expected),
212-
"expected suffix {:?} not found in {:?}", expected, derive_input);
224+
assert!(
225+
input.ends_with(expected),
226+
"expected suffix {:?} not found in {:?}",
227+
expected,
228+
derive_input
229+
);
213230
let end = input.len() - expected.len();
214231
input = &input[..end];
215232
}
@@ -227,33 +244,33 @@ macro_rules! define_invoke_proc_macro {
227244
#[doc(hidden)]
228245
#[macro_export]
229246
macro_rules! $macro_name {
230-
($proc_macro_name: ident ! $paren: tt) => {
231-
#[derive($proc_macro_name)]
232-
#[allow(unused)]
233-
enum ProceduralMasqueradeDummyType {
234-
// The magic happens here.
235-
//
236-
// We use an `enum` with an explicit discriminant
237-
// because that is the only case where a type definition
238-
// can contain a (const) expression.
239-
//
240-
// `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
241-
//
242-
// By the time the `#[proc_macro_derive]` function
243-
// implementing `#[derive($proc_macro_name)]` is called,
244-
// `$paren` has already been replaced with the input of this inner macro,
245-
// but `stringify!` has not been expanded yet.
246-
//
247-
// This how arbitrary tokens can be inserted
248-
// in the input to the `#[proc_macro_derive]` function.
249-
//
250-
// Later, `stringify!(...)` is expanded into a string literal
251-
// which is then ignored.
252-
// Using `stringify!` enables passing arbitrary tokens
253-
// rather than only what can be parsed as a const expression.
254-
Input = (0, stringify! $paren ).0
247+
($proc_macro_name: ident ! $paren: tt) => {
248+
#[derive($proc_macro_name)]
249+
#[allow(unused)]
250+
enum ProceduralMasqueradeDummyType {
251+
// The magic happens here.
252+
//
253+
// We use an `enum` with an explicit discriminant
254+
// because that is the only case where a type definition
255+
// can contain a (const) expression.
256+
//
257+
// `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
258+
//
259+
// By the time the `#[proc_macro_derive]` function
260+
// implementing `#[derive($proc_macro_name)]` is called,
261+
// `$paren` has already been replaced with the input of this inner macro,
262+
// but `stringify!` has not been expanded yet.
263+
//
264+
// This how arbitrary tokens can be inserted
265+
// in the input to the `#[proc_macro_derive]` function.
266+
//
267+
// Later, `stringify!(...)` is expanded into a string literal
268+
// which is then ignored.
269+
// Using `stringify!` enables passing arbitrary tokens
270+
// rather than only what can be parsed as a const expression.
271+
Input = (0, stringify! $paren ).0
272+
}
273+
}
255274
}
256-
}
257-
}
258-
}
275+
};
259276
}

0 commit comments

Comments
 (0)