Skip to content

Commit 9fdcdca

Browse files
authored
Merge pull request #9 from Alexhuszagh/fmtclippy
Add in rustfmt and clippy
2 parents 4b079a5 + b3588da commit 9fdcdca

File tree

15 files changed

+226
-171
lines changed

15 files changed

+226
-171
lines changed

.github/workflows/ci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: CI
22

33
on:
4-
push:
5-
pull_request:
4+
[push, pull_request, workflow_dispatch]
65

76
jobs:
87
test:

.github/workflows/lint.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Lint
2+
3+
on:
4+
[push, pull_request, workflow_dispatch]
5+
6+
jobs:
7+
lint:
8+
name: Lint
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
with:
13+
submodules: recursive
14+
- uses: dtolnay/rust-toolchain@master
15+
with:
16+
toolchain: nightly
17+
components: rustfmt, clippy
18+
- run: cargo check
19+
- run: cargo fmt -- --check
20+
- run: RUSTFLAGS="--deny warnings" cargo build
21+
- run: cargo clippy --all-features -- --deny warnings

clippy.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
avoid-breaking-exported-api = false
2+
disallowed-macros = [
3+
# Can also use an inline table with a `path` key.
4+
{ path = "std::print", reason = "no IO allowed" },
5+
{ path = "std::println", reason = "no IO allowed" },
6+
{ path = "std::format", reason = "no string allocation allowed" },
7+
{ path = "std::debug", reason = "debugging macros should not be present in any release" },
8+
# NOTE: unimplemented is fine because this can be for intentionally disabled methods
9+
{ path = "std::todo", reason = "should never have TODO macros in releases" },
10+
]
11+
disallowed-methods = [
12+
{ path = "std::io::stdout", reason = "no IO allowed" },
13+
{ path = "std::io::stdin", reason = "no IO allowed" },
14+
{ path = "std::io::stderr", reason = "no IO allowed" },
15+
]
16+
disallowed-types = [
17+
{ path = "std::io::File", reason = "no IO allowed" },
18+
{ path = "std::io::BufReader", reason = "need our own abstractions for reading/writing" },
19+
{ path = "std::io::BufWriter", reason = "need our own abstractions for reading/writing" },
20+
]

extras/data-tests/src/main.rs

+2-8
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

+28-39
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

+19
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

+13-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn compute_float<F: Float>(q: i64, mut w: u64) -> AdjustedMantissa {
1717
w <<= lz;
1818
let (lo, hi) = compute_product_approx(q, w, F::MANTISSA_EXPLICIT_BITS + 3);
1919
if lo == 0xFFFF_FFFF_FFFF_FFFF {
20-
let inside_safe_exponent = (q >= -27) && (q <= 55);
20+
let inside_safe_exponent = (-27..=55).contains(&q);
2121
if !inside_safe_exponent {
2222
return am_error;
2323
}
@@ -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);

0 commit comments

Comments
 (0)