Skip to content

Commit 4609812

Browse files
authored
style: simplify string formatting (bytecodealliance#9047)
* style: simplify string formatting * fix: formatting in `benches/call.rs`
1 parent 25fcf41 commit 4609812

25 files changed

+115
-136
lines changed

benches/call.rs

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn bench_host_to_wasm<Params, Results>(
139139
{
140140
// Benchmark the "typed" version, which should be faster than the versions
141141
// below.
142-
c.bench_function(&format!("core - host-to-wasm - typed - {}", name), |b| {
142+
c.bench_function(&format!("core - host-to-wasm - typed - {name}"), |b| {
143143
let typed = instance
144144
.get_typed_func::<Params, Results>(&mut *store, name)
145145
.unwrap();
@@ -155,7 +155,7 @@ fn bench_host_to_wasm<Params, Results>(
155155

156156
// Benchmark the "untyped" version which should be the slowest of the three
157157
// here, but not unduly slow.
158-
c.bench_function(&format!("core - host-to-wasm - untyped - {}", name), |b| {
158+
c.bench_function(&format!("core - host-to-wasm - untyped - {name}"), |b| {
159159
let untyped = instance.get_func(&mut *store, name).unwrap();
160160
let params = typed_params.to_vals();
161161
let expected_results = typed_results.to_vals();
@@ -180,28 +180,25 @@ fn bench_host_to_wasm<Params, Results>(
180180

181181
// Benchmark the "unchecked" version which should be between the above two,
182182
// but is unsafe.
183-
c.bench_function(
184-
&format!("core - host-to-wasm - unchecked - {}", name),
185-
|b| {
186-
let untyped = instance.get_func(&mut *store, name).unwrap();
187-
let params = typed_params.to_vals();
188-
let results = typed_results.to_vals();
189-
let mut space = vec![ValRaw::i32(0); params.len().max(results.len())];
190-
b.iter(|| unsafe {
191-
for (i, param) in params.iter().enumerate() {
192-
space[i] = param.to_raw(&mut *store).unwrap();
193-
}
194-
untyped
195-
.call_unchecked(&mut *store, space.as_mut_ptr(), space.len())
196-
.unwrap();
197-
for (i, expected) in results.iter().enumerate() {
198-
let ty = expected.ty(&store).unwrap();
199-
let actual = Val::from_raw(&mut *store, space[i], ty);
200-
assert_vals_eq(expected, &actual);
201-
}
202-
})
203-
},
204-
);
183+
c.bench_function(&format!("core - host-to-wasm - unchecked - {name}"), |b| {
184+
let untyped = instance.get_func(&mut *store, name).unwrap();
185+
let params = typed_params.to_vals();
186+
let results = typed_results.to_vals();
187+
let mut space = vec![ValRaw::i32(0); params.len().max(results.len())];
188+
b.iter(|| unsafe {
189+
for (i, param) in params.iter().enumerate() {
190+
space[i] = param.to_raw(&mut *store).unwrap();
191+
}
192+
untyped
193+
.call_unchecked(&mut *store, space.as_mut_ptr(), space.len())
194+
.unwrap();
195+
for (i, expected) in results.iter().enumerate() {
196+
let ty = expected.ty(&store).unwrap();
197+
let actual = Val::from_raw(&mut *store, space[i], ty);
198+
assert_vals_eq(expected, &actual);
199+
}
200+
})
201+
});
205202
}
206203

207204
/// Benchmarks the overhead of calling the host from WebAssembly itself
@@ -398,7 +395,7 @@ fn wasm_to_host(c: &mut Criterion) {
398395
desc: &str,
399396
is_async: IsAsync,
400397
) {
401-
group.bench_function(&format!("core - wasm-to-host - {} - nop", desc), |b| {
398+
group.bench_function(&format!("core - wasm-to-host - {desc} - nop"), |b| {
402399
let run = instance
403400
.get_typed_func::<u64, ()>(&mut *store, "run-nop")
404401
.unwrap();
@@ -413,7 +410,7 @@ fn wasm_to_host(c: &mut Criterion) {
413410
})
414411
});
415412
group.bench_function(
416-
&format!("core - wasm-to-host - {} - nop-params-and-results", desc),
413+
&format!("core - wasm-to-host - {desc} - nop-params-and-results"),
417414
|b| {
418415
let run = instance
419416
.get_typed_func::<u64, ()>(&mut *store, "run-nop-params-and-results")
@@ -636,31 +633,28 @@ mod component {
636633
+ Sync,
637634
{
638635
// Benchmark the "typed" version.
639-
c.bench_function(
640-
&format!("component - host-to-wasm - typed - {}", name),
641-
|b| {
642-
let typed = instance
643-
.get_typed_func::<Params, Results>(&mut *store, name)
644-
.unwrap();
645-
b.iter(|| {
646-
let results = if is_async.use_async() {
647-
run_await(typed.call_async(&mut *store, typed_params)).unwrap()
648-
} else {
649-
typed.call(&mut *store, typed_params).unwrap()
650-
};
651-
assert_eq!(results, typed_results);
652-
if is_async.use_async() {
653-
run_await(typed.post_return_async(&mut *store)).unwrap()
654-
} else {
655-
typed.post_return(&mut *store).unwrap()
656-
}
657-
})
658-
},
659-
);
636+
c.bench_function(&format!("component - host-to-wasm - typed - {name}"), |b| {
637+
let typed = instance
638+
.get_typed_func::<Params, Results>(&mut *store, name)
639+
.unwrap();
640+
b.iter(|| {
641+
let results = if is_async.use_async() {
642+
run_await(typed.call_async(&mut *store, typed_params)).unwrap()
643+
} else {
644+
typed.call(&mut *store, typed_params).unwrap()
645+
};
646+
assert_eq!(results, typed_results);
647+
if is_async.use_async() {
648+
run_await(typed.post_return_async(&mut *store)).unwrap()
649+
} else {
650+
typed.post_return(&mut *store).unwrap()
651+
}
652+
})
653+
});
660654

661655
// Benchmark the "untyped" version.
662656
c.bench_function(
663-
&format!("component - host-to-wasm - untyped - {}", name),
657+
&format!("component - host-to-wasm - untyped - {name}"),
664658
|b| {
665659
let untyped = instance.get_func(&mut *store, name).unwrap();
666660
let params = typed_params.to_component_vals();
@@ -864,7 +858,7 @@ mod component {
864858
desc: &str,
865859
is_async: IsAsync,
866860
) {
867-
group.bench_function(&format!("component - wasm-to-host - {} - nop", desc), |b| {
861+
group.bench_function(&format!("component - wasm-to-host - {desc} - nop"), |b| {
868862
let run = instance
869863
.get_typed_func::<(u64,), ()>(&mut *store, "run-nop")
870864
.unwrap();
@@ -881,10 +875,7 @@ mod component {
881875
})
882876
});
883877
group.bench_function(
884-
&format!(
885-
"component - wasm-to-host - {} - nop-params-and-results",
886-
desc
887-
),
878+
&format!("component - wasm-to-host - {desc} - nop-params-and-results"),
888879
|b| {
889880
let run = instance
890881
.get_typed_func::<(u64,), ()>(&mut *store, "run-nop-params-and-results")

examples/component/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ fn main() -> Result<()> {
5757
host::add_to_linker(&mut linker, |state: &mut MyState| &mut state.host)?;
5858
let convert = Convert::instantiate(&mut store, &component, &linker)?;
5959
let result = convert.call_convert_celsius_to_fahrenheit(&mut store, 23.4)?;
60-
println!("Converted to: {:?}", result);
60+
println!("Converted to: {result:?}");
6161
Ok(())
6262
}

examples/fuel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ fn main() -> Result<()> {
2121
Ok(v) => v,
2222
Err(e) => {
2323
assert_eq!(e.downcast::<Trap>()?, Trap::OutOfFuel);
24-
println!("Exhausted fuel computing fib({})", n);
24+
println!("Exhausted fuel computing fib({n})");
2525
break;
2626
}
2727
};
2828
let fuel_consumed = fuel_before - store.get_fuel().unwrap();
29-
println!("fib({}) = {} [consumed {} fuel]", n, output, fuel_consumed);
29+
println!("fib({n}) = {output} [consumed {fuel_consumed} fuel]");
3030
store.set_fuel(10_000)?;
3131
}
3232
Ok(())

examples/multi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() -> Result<()> {
4040
let (a, b) = g.call(&mut store, (1, 3))?;
4141

4242
println!("Printing result...");
43-
println!("> {} {}", a, b);
43+
println!("> {a} {b}");
4444

4545
assert_eq!(a, 4);
4646
assert_eq!(b, 2);
@@ -56,7 +56,7 @@ fn main() -> Result<()> {
5656
let results = round_trip_many.call(&mut store, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))?;
5757

5858
println!("Printing result...");
59-
println!("> {:?}", results);
59+
println!("> {results:?}");
6060
assert_eq!(results, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
6161

6262
Ok(())

src/commands/run.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl RunCommand {
508508
.call(&mut *store, &values, &mut results)
509509
.with_context(|| {
510510
if let Some(name) = &self.invoke {
511-
format!("failed to invoke `{}`", name)
511+
format!("failed to invoke `{name}`")
512512
} else {
513513
format!("failed to invoke command default")
514514
}
@@ -527,8 +527,8 @@ impl RunCommand {
527527

528528
for result in results {
529529
match result {
530-
Val::I32(i) => println!("{}", i),
531-
Val::I64(i) => println!("{}", i),
530+
Val::I32(i) => println!("{i}"),
531+
Val::I64(i) => println!("{i}"),
532532
Val::F32(f) => println!("{}", f32::from_bits(f)),
533533
Val::F64(f) => println!("{}", f64::from_bits(f)),
534534
Val::V128(i) => println!("{}", i.as_u128()),
@@ -558,10 +558,10 @@ impl RunCommand {
558558
.unwrap_or_else(|| "unknown");
559559

560560
if let Err(coredump_err) = write_core_dump(store, &err, &source_name, coredump_path) {
561-
eprintln!("warning: coredump failed to generate: {}", coredump_err);
561+
eprintln!("warning: coredump failed to generate: {coredump_err}");
562562
err
563563
} else {
564-
err.context(format!("core dumped at {}", coredump_path))
564+
err.context(format!("core dumped at {coredump_path}"))
565565
}
566566
}
567567

@@ -847,7 +847,7 @@ impl RunCommand {
847847

848848
for (host, guest) in self.run.dirs.iter() {
849849
let dir = Dir::open_ambient_dir(host, ambient_authority())
850-
.with_context(|| format!("failed to open directory '{}'", host))?;
850+
.with_context(|| format!("failed to open directory '{host}'"))?;
851851
builder.preopened_dir(dir, guest)?;
852852
}
853853

@@ -989,9 +989,9 @@ fn write_core_dump(
989989
let core_dump = core_dump.serialize(store, name);
990990

991991
let mut core_dump_file =
992-
File::create(path).context(format!("failed to create file at `{}`", path))?;
992+
File::create(path).context(format!("failed to create file at `{path}`"))?;
993993
core_dump_file
994994
.write_all(&core_dump)
995-
.with_context(|| format!("failed to write core dump file at `{}`", path))?;
995+
.with_context(|| format!("failed to write core dump file at `{path}`"))?;
996996
Ok(())
997997
}

src/commands/settings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl SettingsCommand {
156156
println!("Settings inferred for the current host:");
157157

158158
for name in inferred {
159-
println!(" {}", name);
159+
println!(" {name}");
160160
}
161161
}
162162

@@ -169,7 +169,7 @@ impl SettingsCommand {
169169
}
170170

171171
println!();
172-
println!("{}", header);
172+
println!("{header}");
173173

174174
let width = settings.iter().map(|s| s.0.name.len()).max().unwrap_or(0);
175175

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl RunCommon {
327327

328328
for address in &self.common.wasi.tcplisten {
329329
let stdlistener = std::net::TcpListener::bind(address)
330-
.with_context(|| format!("failed to bind to address '{}'", address))?;
330+
.with_context(|| format!("failed to bind to address '{address}'"))?;
331331

332332
let _ = stdlistener.set_nonblocking(true)?;
333333

tests/all/cli_tests.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ fn timeout_in_start() -> Result<()> {
221221
let stderr = String::from_utf8_lossy(&output.stderr);
222222
assert!(
223223
stderr.contains("wasm trap: interrupt"),
224-
"bad stderr: {}",
225-
stderr
224+
"bad stderr: {stderr}"
226225
);
227226
Ok(())
228227
}
@@ -244,8 +243,7 @@ fn timeout_in_invoke() -> Result<()> {
244243
let stderr = String::from_utf8_lossy(&output.stderr);
245244
assert!(
246245
stderr.contains("wasm trap: interrupt"),
247-
"bad stderr: {}",
248-
stderr
246+
"bad stderr: {stderr}"
249247
);
250248
Ok(())
251249
}
@@ -1347,7 +1345,7 @@ mod test_programs {
13471345
)?;
13481346
println!("{}", String::from_utf8_lossy(&output.stderr));
13491347
let stdout = String::from_utf8_lossy(&output.stdout);
1350-
println!("{}", stdout);
1348+
println!("{stdout}");
13511349
assert!(stdout.starts_with("Called _start\n"));
13521350
assert!(stdout.ends_with("Done\n"));
13531351
assert!(output.status.success());

tests/all/component_model/bindgen/results.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ mod enum_error {
251251

252252
impl std::fmt::Display for MyTrap {
253253
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
254-
write!(f, "{:?}", self)
254+
write!(f, "{self:?}")
255255
}
256256
}
257257
impl std::error::Error for MyTrap {}
@@ -877,7 +877,7 @@ mod multiple_interfaces_error {
877877

878878
impl std::fmt::Display for MyTrap {
879879
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
880-
write!(f, "{:?}", self)
880+
write!(f, "{self:?}")
881881
}
882882
}
883883
impl std::error::Error for MyTrap {}

tests/all/component_model/func.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,7 @@ fn some_traps() -> Result<()> {
11241124
assert!(
11251125
err.to_string()
11261126
.contains("realloc return: beyond end of memory"),
1127-
"{:?}",
1128-
err,
1127+
"{err:?}",
11291128
);
11301129
}
11311130
let err = instance(&mut store)?

0 commit comments

Comments
 (0)