Skip to content

Commit ea01d16

Browse files
committed
Add in our rust formatters.
1 parent 4b079a5 commit ea01d16

File tree

12 files changed

+146
-135
lines changed

12 files changed

+146
-135
lines changed

extras/data-tests/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ impl TestCase {
3131
let (value, len) = r.unwrap();
3232
if len != self.string.len() || value != expected {
3333
if len != self.string.len() {
34-
eprintln!(
35-
"Expected empty string remainder, got: {:?}",
36-
self.string.len() - len
37-
);
34+
eprintln!("Expected empty string remainder, got: {:?}", self.string.len() - len);
3835
}
3936
if value != expected {
4037
eprintln!("Expected output {}, got {}", expected, value);
@@ -51,10 +48,7 @@ impl TestCase {
5148

5249
fn parse_test_file(filename: impl AsRef<Path>) -> impl Iterator<Item = TestCase> {
5350
let file = File::open(filename).unwrap();
54-
BufReader::new(file)
55-
.lines()
56-
.map(Result::unwrap)
57-
.map(TestCase::parse)
51+
BufReader::new(file).lines().map(Result::unwrap).map(TestCase::parse)
5852
}
5953

6054
fn run_test_cases(filename: impl AsRef<Path>) -> usize {

extras/simple-bench/src/main.rs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ use std::path::{Path, PathBuf};
66
use std::str::FromStr;
77
use std::time::Instant;
88

9+
use fast_float2::FastFloat;
910
use fastrand::Rng;
1011
use lexical::FromLexical;
11-
use structopt::StructOpt;
12-
13-
use fast_float2::FastFloat;
14-
1512
use random::RandomGen;
13+
use structopt::StructOpt;
1614

1715
#[derive(Debug, StructOpt)]
18-
#[structopt(
19-
name = "fast-float-simple-bench",
20-
about = "fast-float benchmark utility",
21-
no_version
22-
)]
16+
#[structopt(name = "fast-float-simple-bench", about = "fast-float benchmark utility", no_version)]
2317
struct Opt {
2418
/// Parse numbers as float32 (default is float64)
2519
#[structopt(short, long = "32")]
@@ -146,9 +140,7 @@ impl Method {
146140
fast_float::parse_partial::<T, _>(s).unwrap_or_default().0
147141
}),
148142
Self::Lexical => run_bench(data, repeat, |s: &str| {
149-
lexical_core::parse_partial::<T>(s.as_bytes())
150-
.unwrap_or_default()
151-
.0
143+
lexical_core::parse_partial::<T>(s.as_bytes()).unwrap_or_default().0
152144
}),
153145
Self::FromStr => run_bench(data, repeat, |s: &str| s.parse::<T>().unwrap_or_default()),
154146
};
@@ -180,12 +172,8 @@ fn print_report(results: &[BenchResult], title: &str) {
180172
println!("| {:^width$} |", title, width = width);
181173
println!("|{:=<width$}|", "", width = width + 2);
182174
print_table("ns/float", results, width, |t, n, _| t as f64 / n as f64);
183-
print_table("Mfloat/s", results, width, |t, n, _| {
184-
1e3 * n as f64 / t as f64
185-
});
186-
print_table("MB/s", results, width, |t, _, b| {
187-
b as f64 * 1e9 / 1024. / 1024. / t as f64
188-
});
175+
print_table("Mfloat/s", results, width, |t, n, _| 1e3 * n as f64 / t as f64);
176+
print_table("MB/s", results, width, |t, _, b| b as f64 * 1e9 / 1024. / 1024. / t as f64);
189177
println!("|{:width$}|", "", width = width + 2);
190178
println!("{:=<width$}", "", width = width + 4);
191179
}
@@ -219,11 +207,7 @@ fn print_table(
219207
for res in results {
220208
print!("| {:<h$}", res.name, h = h);
221209
let (n, b) = (res.count, res.bytes);
222-
let mut metrics = res
223-
.times
224-
.iter()
225-
.map(|&t| transform(t, n, b))
226-
.collect::<Vec<_>>();
210+
let mut metrics = res.times.iter().map(|&t| transform(t, n, b)).collect::<Vec<_>>();
227211
metrics.sort_by(|a, b| a.partial_cmp(b).unwrap());
228212
for &(_, idx) in columns {
229213
print!("{:>w$.2}", metrics[idx], w = w);
@@ -240,23 +224,23 @@ struct Input {
240224
impl Input {
241225
pub fn from_file(filename: impl AsRef<Path>) -> Self {
242226
let filename = filename.as_ref();
243-
let data = fs::read_to_string(&filename)
244-
.unwrap()
245-
.trim()
246-
.lines()
247-
.map(String::from)
248-
.collect();
227+
let data =
228+
fs::read_to_string(&filename).unwrap().trim().lines().map(String::from).collect();
249229
let name = filename.file_name().unwrap().to_str().unwrap().into();
250-
Self { data, name }
230+
Self {
231+
data,
232+
name,
233+
}
251234
}
252235

253236
pub fn from_random(gen: RandomGen, count: usize, seed: u64) -> Self {
254237
let mut rng = Rng::with_seed(seed);
255-
let data = iter::repeat_with(|| gen.gen(&mut rng))
256-
.take(count)
257-
.collect();
238+
let data = iter::repeat_with(|| gen.gen(&mut rng)).take(count).collect();
258239
let name = format!("{}", gen);
259-
Self { data, name }
240+
Self {
241+
data,
242+
name,
243+
}
260244
}
261245

262246
pub fn count(&self) -> usize {
@@ -281,14 +265,16 @@ impl Input {
281265
fn main() {
282266
let opt: Opt = StructOpt::from_args();
283267

284-
let methods = if !opt.only_fast_float && !matches!(&opt.command, &Cmd::All {..}) {
268+
let methods = if !opt.only_fast_float && !matches!(&opt.command, &Cmd::All { .. }) {
285269
Method::all().into()
286270
} else {
287271
vec![Method::FastFloat2]
288272
};
289273

290274
let inputs = match opt.command {
291-
Cmd::File { filename } => vec![Input::from_file(filename)],
275+
Cmd::File {
276+
filename,
277+
} => vec![Input::from_file(filename)],
292278
Cmd::Random {
293279
gen,
294280
count,
@@ -300,8 +286,11 @@ fn main() {
300286
fs::write(filename, input.data.join("\n")).unwrap();
301287
}
302288
vec![input]
303-
}
304-
Cmd::All { count, seed } => {
289+
},
290+
Cmd::All {
291+
count,
292+
seed,
293+
} => {
305294
let mut inputs = vec![];
306295
let data_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ext/data");
307296
inputs.push(Input::from_file(data_dir.join("mesh.txt")));
@@ -310,7 +299,7 @@ fn main() {
310299
inputs.push(Input::from_random(gen, count, seed))
311300
}
312301
inputs
313-
}
302+
},
314303
};
315304

316305
let mut results = vec![];

rustfmt.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Requires nightly to do proper formatting.
2+
use_small_heuristics = "Off"
3+
use_field_init_shorthand = true
4+
trailing_semicolon = true
5+
newline_style = "Unix"
6+
match_block_trailing_comma = true
7+
empty_item_single_line = false
8+
enum_discrim_align_threshold = 40
9+
fn_params_layout = "Tall"
10+
fn_single_line = false
11+
format_macro_matchers = true
12+
format_macro_bodies = true
13+
imports_indent = "Block"
14+
imports_layout = "HorizontalVertical"
15+
indent_style = "Block"
16+
match_arm_blocks = true
17+
overflow_delimited_expr = true
18+
group_imports = "StdExternalCrate"
19+
wrap_comments = true

src/binary.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
3333
mantissa += mantissa & 1;
3434
mantissa >>= 1;
3535
power2 = (mantissa >= (1_u64 << F::MANTISSA_EXPLICIT_BITS)) as i32;
36-
return AdjustedMantissa { mantissa, power2 };
36+
return AdjustedMantissa {
37+
mantissa,
38+
power2,
39+
};
3740
}
3841
if lo <= 1
3942
&& q >= F::MIN_EXPONENT_ROUND_TO_EVEN as i64
@@ -53,7 +56,10 @@ pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
5356
if power2 >= F::INFINITE_POWER {
5457
return am_inf;
5558
}
56-
AdjustedMantissa { mantissa, power2 }
59+
AdjustedMantissa {
60+
mantissa,
61+
power2,
62+
}
5763
}
5864

5965
#[inline]
@@ -67,9 +73,10 @@ fn full_multiplication(a: u64, b: u64) -> (u64, u64) {
6773
(r as u64, (r >> 64) as u64)
6874
}
6975

70-
// This will compute or rather approximate w * 5**q and return a pair of 64-bit words
71-
// approximating the result, with the "high" part corresponding to the most significant
72-
// bits and the low part corresponding to the least significant bits.
76+
// This will compute or rather approximate w * 5**q and return a pair of 64-bit
77+
// words approximating the result, with the "high" part corresponding to the
78+
// most significant bits and the low part corresponding to the least significant
79+
// bits.
7380
#[inline]
7481
fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {
7582
debug_assert!(q >= SMALLEST_POWER_OF_FIVE as i64);

src/common.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ impl<'a> AsciiStr<'a> {
2727
/// Safe if `n <= self.len()`
2828
#[inline]
2929
pub unsafe fn step_by(&mut self, n: usize) -> &mut Self {
30-
debug_assert!(n <= self.len(), "buffer overflow: stepping by greater than our buffer length.");
30+
debug_assert!(
31+
n <= self.len(),
32+
"buffer overflow: stepping by greater than our buffer length."
33+
);
3134
// SAFETY: Safe if `n <= self.len()`
3235
unsafe { self.ptr = self.ptr.add(n) };
3336
self
@@ -102,10 +105,12 @@ impl<'a> AsciiStr<'a> {
102105

103106
#[inline]
104107
pub fn first_digit(&self) -> Option<u8> {
105-
self.first().and_then(|x| if x.is_ascii_digit() {
106-
Some(x - b'0')
107-
} else {
108-
None
108+
self.first().and_then(|x| {
109+
if x.is_ascii_digit() {
110+
Some(x - b'0')
111+
} else {
112+
None
113+
}
109114
})
110115
}
111116

@@ -153,7 +158,8 @@ impl<'a> AsciiStr<'a> {
153158
}
154159
}
155160

156-
// Most of these are inherently unsafe; we assume we know what we're calling and when.
161+
// Most of these are inherently unsafe; we assume we know what we're calling and
162+
// when.
157163
pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> {
158164
#[inline]
159165
fn check_first(&self, c: u8) -> bool {
@@ -216,7 +222,8 @@ pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> {
216222
}
217223
}
218224

219-
impl ByteSlice for [u8] {}
225+
impl ByteSlice for [u8] {
226+
}
220227

221228
#[inline]
222229
pub fn is_8digits(v: u64) -> bool {

src/decimal.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl PartialEq for Decimal {
3333
}
3434
}
3535

36-
impl Eq for Decimal {}
36+
impl Eq for Decimal {
37+
}
3738

3839
impl Default for Decimal {
3940
fn default() -> Self {
@@ -253,7 +254,11 @@ pub fn parse_decimal(mut s: &[u8]) -> Decimal {
253254
exp_num = 10 * exp_num + digit as i32;
254255
}
255256
});
256-
d.decimal_point += if neg_exp { -exp_num } else { exp_num };
257+
d.decimal_point += if neg_exp {
258+
-exp_num
259+
} else {
260+
exp_num
261+
};
257262
}
258263
for i in d.num_digits..Decimal::MAX_DIGITS_WITHOUT_OVERFLOW {
259264
d.digits[i] = 0;

src/float.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ pub trait Float:
4545
fn pow10_fast_path(exponent: usize) -> Self;
4646
}
4747

48-
impl private::Sealed for f32 {}
48+
impl private::Sealed for f32 {
49+
}
4950

5051
impl Float for f32 {
5152
const INFINITY: Self = core::f32::INFINITY;
@@ -78,14 +79,14 @@ impl Float for f32 {
7879
#[inline]
7980
fn pow10_fast_path(exponent: usize) -> Self {
8081
#[allow(clippy::use_self)]
81-
const TABLE: [f32; 16] = [
82-
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 0., 0., 0., 0., 0.,
83-
];
82+
const TABLE: [f32; 16] =
83+
[1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 0., 0., 0., 0., 0.];
8484
TABLE[exponent & 15]
8585
}
8686
}
8787

88-
impl private::Sealed for f64 {}
88+
impl private::Sealed for f64 {
89+
}
8990

9091
impl Float for f64 {
9192
const INFINITY: Self = core::f64::INFINITY;

0 commit comments

Comments
 (0)