Skip to content

Commit 66c6fe5

Browse files
committed
Merge pull request #112 from marcusklaas/config-fix
Remove global mutable config to allow for concurrency
2 parents 1488d5e + d7b49fd commit 66c6fe5

File tree

8 files changed

+107
-103
lines changed

8 files changed

+107
-103
lines changed

src/changes.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::fs::File;
2121
use std::io::{Write, stdout};
2222
use WriteMode;
2323
use NewlineStyle;
24+
use config::Config;
2425
use utils::round_up_to_power_of_two;
2526

2627
// This is basically a wrapper around a bunch of Ropes which makes it convenient
@@ -130,11 +131,12 @@ impl<'a> ChangeSet<'a> {
130131
}
131132

132133
pub fn write_all_files(&self,
133-
mode: WriteMode)
134+
mode: WriteMode,
135+
config: &Config)
134136
-> Result<(HashMap<String, String>), ::std::io::Error> {
135137
let mut result = HashMap::new();
136138
for filename in self.file_map.keys() {
137-
let one_result = try!(self.write_file(filename, mode));
139+
let one_result = try!(self.write_file(filename, mode, config));
138140
if let Some(r) = one_result {
139141
result.insert(filename.clone(), r);
140142
}
@@ -145,18 +147,20 @@ impl<'a> ChangeSet<'a> {
145147

146148
pub fn write_file(&self,
147149
filename: &str,
148-
mode: WriteMode)
150+
mode: WriteMode,
151+
config: &Config)
149152
-> Result<Option<String>, ::std::io::Error> {
150153
let text = &self.file_map[filename];
151154

152155
// prints all newlines either as `\n` or as `\r\n`
153156
fn write_system_newlines<T>(
154157
mut writer: T,
155-
text: &StringBuffer)
158+
text: &StringBuffer,
159+
config: &Config)
156160
-> Result<(), ::std::io::Error>
157161
where T: Write,
158162
{
159-
match config!(newline_style) {
163+
match config.newline_style {
160164
NewlineStyle::Unix => write!(writer, "{}", text),
161165
NewlineStyle::Windows => {
162166
for (c, _) in text.chars() {
@@ -181,7 +185,7 @@ impl<'a> ChangeSet<'a> {
181185
{
182186
// Write text to temp file
183187
let tmp_file = try!(File::create(&tmp_name));
184-
try!(write_system_newlines(tmp_file, text));
188+
try!(write_system_newlines(tmp_file, text, config));
185189
}
186190

187191
try!(::std::fs::rename(filename, bk_name));
@@ -190,18 +194,18 @@ impl<'a> ChangeSet<'a> {
190194
WriteMode::NewFile(extn) => {
191195
let filename = filename.to_owned() + "." + extn;
192196
let file = try!(File::create(&filename));
193-
try!(write_system_newlines(file, text));
197+
try!(write_system_newlines(file, text, config));
194198
}
195199
WriteMode::Display => {
196200
println!("{}:\n", filename);
197201
let stdout = stdout();
198202
let stdout_lock = stdout.lock();
199-
try!(write_system_newlines(stdout_lock, text));
203+
try!(write_system_newlines(stdout_lock, text, config));
200204
}
201205
WriteMode::Return(_) => {
202206
// io::Write is not implemented for String, working around with Vec<u8>
203207
let mut v = Vec::new();
204-
try!(write_system_newlines(&mut v, text));
208+
try!(write_system_newlines(&mut v, text, config));
205209
// won't panic, we are writing correct utf8
206210
return Ok(Some(String::from_utf8(v).unwrap()));
207211
}

src/config.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use {NewlineStyle, BraceStyle, ReturnIndent};
1414
use lists::SeparatorTactic;
1515
use issues::ReportTactic;
1616

17-
#[derive(RustcDecodable)]
17+
#[derive(RustcDecodable, Clone)]
1818
pub struct Config {
1919
pub max_width: usize,
2020
pub ideal_width: usize,
@@ -32,20 +32,8 @@ pub struct Config {
3232
}
3333

3434
impl Config {
35-
fn from_toml(toml: &str) -> Config {
35+
pub fn from_toml(toml: &str) -> Config {
3636
let parsed = toml.parse().unwrap();
3737
toml::decode(parsed).unwrap()
3838
}
3939
}
40-
41-
pub fn set_config(toml: &str) {
42-
unsafe {
43-
::CONFIG = Some(Config::from_toml(toml));
44-
}
45-
}
46-
47-
macro_rules! config {
48-
($name: ident) => {
49-
unsafe { ::CONFIG.as_ref().unwrap().$name }
50-
};
51-
}

src/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn rewrite_string_lit(context: &RewriteContext, s: &str, span: Span, width: usiz
6262
// strings, or if the string is too long for the line.
6363
let l_loc = context.codemap.lookup_char_pos(span.lo);
6464
let r_loc = context.codemap.lookup_char_pos(span.hi);
65-
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= config!(max_width) {
65+
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= context.config.max_width {
6666
return context.codemap.span_to_snippet(span).ok();
6767
}
6868

@@ -82,7 +82,7 @@ fn rewrite_string_lit(context: &RewriteContext, s: &str, span: Span, width: usiz
8282
// First line.
8383
width - 2 // 2 = " + \
8484
} else {
85-
config!(max_width) - offset - 1 // 1 = either \ or ;
85+
context.config.max_width - offset - 1 // 1 = either \ or ;
8686
};
8787

8888
let mut cur_end = cur_start + max_chars;
@@ -206,7 +206,7 @@ fn rewrite_struct_lit(context: &RewriteContext,
206206
trailing_separator: if base.is_some() {
207207
SeparatorTactic::Never
208208
} else {
209-
config!(struct_lit_trailing_comma)
209+
context.config.struct_lit_trailing_comma
210210
},
211211
indent: indent,
212212
h_width: budget,
@@ -247,7 +247,7 @@ fn rewrite_tuple_lit(context: &RewriteContext,
247247
let rem_width = if i == items.len() - 1 {
248248
width - 2
249249
} else {
250-
config!(max_width) - indent - 2
250+
context.config.max_width - indent - 2
251251
};
252252
item.rewrite(context, rem_width, indent)
253253
})

src/items.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a> FmtVisitor<'a> {
144144

145145
// Check if vertical layout was forced by compute_budget_for_args.
146146
if one_line_budget <= 0 {
147-
if config!(fn_args_paren_newline) {
147+
if self.config.fn_args_paren_newline {
148148
result.push('\n');
149149
result.push_str(&make_indent(arg_indent));
150150
arg_indent = arg_indent + 1; // extra space for `(`
@@ -170,8 +170,8 @@ impl<'a> FmtVisitor<'a> {
170170
// If we've already gone multi-line, or the return type would push
171171
// over the max width, then put the return type on a new line.
172172
if result.contains("\n") ||
173-
result.len() + indent + ret_str.len() > config!(max_width) {
174-
let indent = match config!(fn_return_indent) {
173+
result.len() + indent + ret_str.len() > self.config.max_width {
174+
let indent = match self.config.fn_return_indent {
175175
ReturnIndent::WithWhereClause => indent + 4,
176176
// TODO we might want to check that using the arg indent doesn't
177177
// blow our budget, and if it does, then fallback to the where
@@ -363,15 +363,15 @@ impl<'a> FmtVisitor<'a> {
363363
if !newline_brace {
364364
used_space += 2;
365365
}
366-
let one_line_budget = if used_space > config!(max_width) {
366+
let one_line_budget = if used_space > self.config.max_width {
367367
0
368368
} else {
369-
config!(max_width) - used_space
369+
self.config.max_width - used_space
370370
};
371371

372372
// 2 = `()`
373373
let used_space = indent + result.len() + 2;
374-
let max_space = config!(ideal_width) + config!(leeway);
374+
let max_space = self.config.ideal_width + self.config.leeway;
375375
debug!("compute_budgets_for_args: used_space: {}, max_space: {}",
376376
used_space, max_space);
377377
if used_space < max_space {
@@ -383,9 +383,9 @@ impl<'a> FmtVisitor<'a> {
383383

384384
// Didn't work. we must force vertical layout and put args on a newline.
385385
if let None = budgets {
386-
let new_indent = indent + config!(tab_spaces);
386+
let new_indent = indent + self.config.tab_spaces;
387387
let used_space = new_indent + 2; // account for `(` and `)`
388-
let max_space = config!(ideal_width) + config!(leeway);
388+
let max_space = self.config.ideal_width + self.config.leeway;
389389
if used_space > max_space {
390390
// Whoops! bankrupt.
391391
// TODO take evasive action, perhaps kill the indent or something.
@@ -398,7 +398,7 @@ impl<'a> FmtVisitor<'a> {
398398
}
399399

400400
fn newline_for_brace(&self, where_clause: &ast::WhereClause) -> bool {
401-
match config!(fn_brace_style) {
401+
match self.config.fn_brace_style {
402402
BraceStyle::AlwaysNextLine => true,
403403
BraceStyle::SameLineWhere if where_clause.predicates.len() > 0 => true,
404404
_ => false,
@@ -421,7 +421,7 @@ impl<'a> FmtVisitor<'a> {
421421
self.changes.push_str_span(span, &generics_str);
422422

423423
self.last_pos = body_start;
424-
self.block_indent += config!(tab_spaces);
424+
self.block_indent += self.config.tab_spaces;
425425
for (i, f) in enum_def.variants.iter().enumerate() {
426426
let next_span_start: BytePos = if i == enum_def.variants.len() - 1 {
427427
span.hi
@@ -431,7 +431,7 @@ impl<'a> FmtVisitor<'a> {
431431

432432
self.visit_variant(f, i == enum_def.variants.len() - 1, next_span_start);
433433
}
434-
self.block_indent -= config!(tab_spaces);
434+
self.block_indent -= self.config.tab_spaces;
435435

436436
self.format_missing_with_indent(span.lo + BytePos(enum_snippet.rfind('}').unwrap() as u32));
437437
self.changes.push_str_span(span, "}");
@@ -478,8 +478,8 @@ impl<'a> FmtVisitor<'a> {
478478
+ field.node.name.to_string().len()
479479
+ 1; // 1 = (
480480

481-
let comma_cost = if config!(enum_trailing_comma) { 1 } else { 0 };
482-
let budget = config!(ideal_width) - indent - comma_cost - 1; // 1 = )
481+
let comma_cost = if self.config.enum_trailing_comma { 1 } else { 0 };
482+
let budget = self.config.ideal_width - indent - comma_cost - 1; // 1 = )
483483

484484
let fmt = ListFormatting {
485485
tactic: ListTactic::HorizontalVertical,
@@ -500,13 +500,13 @@ impl<'a> FmtVisitor<'a> {
500500

501501
// Make sure we do not exceed column limit
502502
// 4 = " = ,"
503-
assert!(config!(max_width) >= vis.len() + name.len() + expr_snippet.len() + 4,
503+
assert!(self.config.max_width >= vis.len() + name.len() + expr_snippet.len() + 4,
504504
"Enum variant exceeded column limit");
505505
}
506506

507507
self.changes.push_str_span(field.span, &result);
508508

509-
if !last_field || config!(enum_trailing_comma) {
509+
if !last_field || self.config.enum_trailing_comma {
510510
self.changes.push_str_span(field.span, ",");
511511
}
512512
}
@@ -543,11 +543,11 @@ impl<'a> FmtVisitor<'a> {
543543
// This will drop the comment in between the header and body.
544544
self.last_pos = span.lo + BytePos(struct_snippet.find_uncommented("{").unwrap() as u32 + 1);
545545

546-
self.block_indent += config!(tab_spaces);
546+
self.block_indent += self.config.tab_spaces;
547547
for (i, f) in struct_def.fields.iter().enumerate() {
548548
self.visit_field(f, i == struct_def.fields.len() - 1, span.lo, &struct_snippet);
549549
}
550-
self.block_indent -= config!(tab_spaces);
550+
self.block_indent -= self.config.tab_spaces;
551551

552552
self.format_missing_with_indent(span.lo + BytePos(struct_snippet.rfind('}').unwrap() as u32));
553553
self.changes.push_str_span(span, "}");
@@ -608,21 +608,21 @@ impl<'a> FmtVisitor<'a> {
608608

609609
let mut field_str = match name {
610610
Some(name) => {
611-
let budget = config!(ideal_width) - self.block_indent;
611+
let budget = self.config.ideal_width - self.block_indent;
612612
// 3 is being conservative and assuming that there will be a trailing comma.
613613
if self.block_indent + vis.len() + name.len() + typ.len() + 3 > budget {
614614
format!("{}{}:\n{}{}",
615615
vis,
616616
name,
617-
&make_indent(self.block_indent + config!(tab_spaces)),
617+
&make_indent(self.block_indent + self.config.tab_spaces),
618618
typ)
619619
} else {
620620
format!("{}{}: {}", vis, name, typ)
621621
}
622622
}
623623
None => format!("{}{}", vis, typ),
624624
};
625-
if !last_field || config!(struct_trailing_comma) {
625+
if !last_field || self.config.struct_trailing_comma {
626626
field_str.push(',');
627627
}
628628
self.changes.push_str_span(field.span, &field_str);
@@ -647,7 +647,7 @@ impl<'a> FmtVisitor<'a> {
647647
return result;
648648
}
649649

650-
let budget = config!(max_width) - indent - 2;
650+
let budget = self.config.max_width - indent - 2;
651651
// TODO might need to insert a newline if the generics are really long
652652
result.push('<');
653653

@@ -723,7 +723,7 @@ impl<'a> FmtVisitor<'a> {
723723
.zip(comments.into_iter())
724724
.collect();
725725

726-
let budget = config!(ideal_width) + config!(leeway) - indent - 10;
726+
let budget = self.config.ideal_width + self.config.leeway - indent - 10;
727727
let fmt = ListFormatting {
728728
tactic: ListTactic::Vertical,
729729
separator: ",",

0 commit comments

Comments
 (0)