Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 46e2a2e

Browse files
committed
Support platforms without a timer
I've dabbled recently in seeing how hard it would be to compile rustfmt to wasm and then run it in a web browser, and it turns out that it's [not too hard][wasm]! In addition to patching a few dependencies which already have a number of patches out rustfmt also needed some modifications to get it to work, namely avoiding the usage of `Instant::now()` on the "happy path" which doesn't work on wasm (it just panics). This commit is an attempt to add a support for this by avoiding using `Instant::now()` on the wasm target, but panicking if the actual time elapsed is requested (which doesn't happen unless verbosely logging I believe). [wasm]: https://alexcrichton.github.io/rustfmt-wasm/
1 parent 6ada5b5 commit 46e2a2e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/formatting.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn format_project<T: FormatHandler>(
6464
config: &Config,
6565
handler: &mut T,
6666
) -> Result<FormatReport, ErrorKind> {
67-
let mut timer = Timer::Initialized(Instant::now());
67+
let mut timer = Timer::start();
6868

6969
let main_file = input.file_name();
7070
let input_is_stdin = main_file == FileName::Stdin;
@@ -344,21 +344,31 @@ pub(crate) struct ModifiedLines {
344344

345345
#[derive(Clone, Copy, Debug)]
346346
enum Timer {
347+
Disabled,
347348
Initialized(Instant),
348349
DoneParsing(Instant, Instant),
349350
DoneFormatting(Instant, Instant, Instant),
350351
}
351352

352353
impl Timer {
354+
fn start() -> Timer {
355+
if cfg!(target_arch = "wasm32") {
356+
Timer::Disabled
357+
} else {
358+
Timer::Initialized(Instant::now())
359+
}
360+
}
353361
fn done_parsing(self) -> Self {
354362
match self {
363+
Timer::Disabled => Timer::Disabled,
355364
Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
356365
_ => panic!("Timer can only transition to DoneParsing from Initialized state"),
357366
}
358367
}
359368

360369
fn done_formatting(self) -> Self {
361370
match self {
371+
Timer::Disabled => Timer::Disabled,
362372
Timer::DoneParsing(init_time, parse_time) => {
363373
Timer::DoneFormatting(init_time, parse_time, Instant::now())
364374
}
@@ -369,6 +379,7 @@ impl Timer {
369379
/// Returns the time it took to parse the source files in seconds.
370380
fn get_parse_time(&self) -> f32 {
371381
match *self {
382+
Timer::Disabled => panic!("this platform cannot time execution"),
372383
Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
373384
// This should never underflow since `Instant::now()` guarantees monotonicity.
374385
Self::duration_to_f32(parse_time.duration_since(init))
@@ -381,6 +392,7 @@ impl Timer {
381392
/// not included.
382393
fn get_format_time(&self) -> f32 {
383394
match *self {
395+
Timer::Disabled => panic!("this platform cannot time execution"),
384396
Timer::DoneFormatting(_init, parse_time, format_time) => {
385397
Self::duration_to_f32(format_time.duration_since(parse_time))
386398
}

0 commit comments

Comments
 (0)