Skip to content

Commit cd60ad3

Browse files
committed
fmt
1 parent 48ebbaa commit cd60ad3

3 files changed

Lines changed: 69 additions & 58 deletions

File tree

crates/csharp/src/function.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use wit_bindgen_core::abi::{self, Bindgen, Bitcast, Instruction};
99
use wit_bindgen_core::{uwrite, uwriteln, Direction, Ns};
1010
use wit_parser::abi::WasmType;
1111
use wit_parser::{
12-
Alignment, ArchitectureSize, Docs, FunctionKind, Handle, Resolve, SizeAlign, Type, TypeDefKind, TypeId
12+
Alignment, ArchitectureSize, Docs, FunctionKind, Handle, Resolve, SizeAlign, Type, TypeDefKind,
13+
TypeId,
1314
};
1415

1516
/// FunctionBindgen generates the C# code for calling functions defined in wit
@@ -42,7 +43,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
4243
params: Box<[String]>,
4344
results: Vec<TypeId>,
4445
parameter_type: ParameterType,
45-
result_type: Option<Type>
46+
result_type: Option<Type>,
4647
) -> FunctionBindgen<'a, 'b> {
4748
let mut locals = Ns::default();
4849
// Ensure temporary variable names don't clash with parameter names:
@@ -307,7 +308,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
307308
.interface_gen
308309
.type_name_with_qualifier(&func.result.unwrap(), true);
309310
let is_async = InterfaceGenerator::is_async(&func.kind);
310-
311+
311312
if is_async {
312313
uwriteln!(self.src, "Task<{ty}> {ret};");
313314
} else {
@@ -383,7 +384,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
383384
fn emit_allocation_for_type(&mut self, results: &[WasmType]) -> String {
384385
let address = self.locals.tmp("address");
385386
let buffer_size = self.get_size_for_type(results);
386-
let align= self.get_align_for_type(results);
387+
let align = self.get_align_for_type(results);
387388
uwriteln!(self.src, "void* {address} = global::System.Runtime.InteropServices.NativeMemory.AlignedAlloc({buffer_size}, {align});");
388389

389390
// TODO: Store the address somewhere so we can free it when the task completes.
@@ -396,7 +397,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
396397
[WasmType::I64] => 8,
397398
[WasmType::F32] => 4,
398399
[WasmType::F64] => 8,
399-
[WasmType::Pointer, WasmType::Length] => 4, // TODO: Wasm64
400+
[WasmType::Pointer, WasmType::Length] => 4, // TODO: Wasm64
400401
[WasmType::PointerOrI64, WasmType::Length] => 8,
401402
_ => {
402403
todo!("other types not yet supported");
@@ -410,7 +411,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
410411
[WasmType::I64] => 8,
411412
[WasmType::F32] => 4,
412413
[WasmType::F64] => 8,
413-
[WasmType::Pointer, WasmType::Length] => 4, // TODO: Wasm64
414+
[WasmType::Pointer, WasmType::Length] => 4, // TODO: Wasm64
414415
[WasmType::PointerOrI64, WasmType::Length] => 8,
415416
_ => {
416417
todo!("other types not yet supported");

crates/csharp/src/interface.rs

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ pub(crate) struct InterfaceGenerator<'a> {
5151

5252
impl InterfaceGenerator<'_> {
5353
pub fn is_async(kind: &FunctionKind) -> bool {
54-
matches!(
54+
matches!(
5555
kind,
5656
FunctionKind::AsyncFreestanding
5757
| FunctionKind::AsyncStatic(_)
5858
| FunctionKind::AsyncMethod(_)
5959
)
6060
}
61-
61+
6262
pub(crate) fn define_interface_types(&mut self, id: InterfaceId) {
6363
let mut live = LiveTypes::default();
6464
live.add_interface(self.resolve, id);
@@ -125,7 +125,7 @@ impl InterfaceGenerator<'_> {
125125
TypeDefKind::Handle(_) => {
126126
// Handles don't require a separate definition beyond what we already define for the corresponding
127127
// resource types.
128-
},
128+
}
129129
TypeDefKind::Future(t) => self.type_future(type_id, typedef_name, t, &type_def.docs),
130130
_ => unreachable!(),
131131
}
@@ -188,8 +188,9 @@ impl InterfaceGenerator<'_> {
188188
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => {
189189
func.item_name().to_upper_camel_case()
190190
}
191-
FunctionKind::Constructor(id) =>
192-
self.csharp_gen.all_resources[id].name.to_upper_camel_case(),
191+
FunctionKind::Constructor(id) => {
192+
self.csharp_gen.all_resources[id].name.to_upper_camel_case()
193+
}
193194
};
194195

195196
let access = self.csharp_gen.access_modifier();
@@ -214,7 +215,7 @@ impl InterfaceGenerator<'_> {
214215
} else {
215216
"void"
216217
}
217-
},
218+
}
218219
[result] => crate::world_generator::wasm_type(*result),
219220
_ => unreachable!(),
220221
};
@@ -345,7 +346,7 @@ impl InterfaceGenerator<'_> {
345346
self.resolve,
346347
ty,
347348
self.csharp_gen.opts.with_wit_results,
348-
);
349+
);
349350
if let Some(ty) = payload {
350351
self.csharp_gen.needs_result = true;
351352
self.type_name_with_qualifier(&ty, true)
@@ -362,18 +363,16 @@ impl InterfaceGenerator<'_> {
362363
let asyncified_type = match &func.kind {
363364
FunctionKind::AsyncFreestanding
364365
| FunctionKind::AsyncStatic(_)
365-
| FunctionKind::AsyncMethod(_) => {
366-
match func.result {
367-
None => "Task".to_string(),
368-
Some(_ty) => format!("Task<{}>", base_type)
369-
}
370-
},
371-
_ => base_type
366+
| FunctionKind::AsyncMethod(_) => match func.result {
367+
None => "Task".to_string(),
368+
Some(_ty) => format!("Task<{}>", base_type),
369+
},
370+
_ => base_type,
372371
};
373-
372+
374373
asyncified_type
375374
};
376-
375+
377376
result_type
378377
}
379378

@@ -400,7 +399,7 @@ impl InterfaceGenerator<'_> {
400399
.collect(),
401400
results.clone(),
402401
parameter_type,
403-
func.result
402+
func.result,
404403
);
405404

406405
abi::call(
@@ -439,14 +438,13 @@ impl InterfaceGenerator<'_> {
439438
FunctionKind::Freestanding
440439
| FunctionKind::Static(_)
441440
| FunctionKind::AsyncFreestanding
442-
| FunctionKind::AsyncStatic(_) => {
443-
func.item_name().to_upper_camel_case()
444-
}
441+
| FunctionKind::AsyncStatic(_) => func.item_name().to_upper_camel_case(),
445442
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => {
446443
func.item_name().to_upper_camel_case()
447444
}
448-
FunctionKind::Constructor(id) =>
445+
FunctionKind::Constructor(id) => {
449446
self.csharp_gen.all_resources[id].name.to_upper_camel_case()
447+
}
450448
};
451449

452450
let modifiers = modifiers(func, &camel_name, Direction::Export);
@@ -490,14 +488,15 @@ impl InterfaceGenerator<'_> {
490488
.collect::<Vec<_>>()
491489
.join(";\n");
492490

493-
let wasm_result_type =
494-
if async_ {"uint"
495-
} else { match &sig.results[..] {
491+
let wasm_result_type = if async_ {
492+
"uint"
493+
} else {
494+
match &sig.results[..] {
496495
[] => "void",
497496
[result] => crate::world_generator::wasm_type(*result),
498497
_ => unreachable!(),
499-
}
500-
};
498+
}
499+
};
501500

502501
let wasm_params = sig
503502
.params
@@ -569,7 +568,7 @@ impl InterfaceGenerator<'_> {
569568
(0..sig.results.len()).map(|i| format!("p{i}")).collect(),
570569
Vec::new(),
571570
ParameterType::ABI,
572-
func.result
571+
func.result,
573572
);
574573

575574
abi::post_return(bindgen.interface_gen.resolve, func, &mut bindgen);
@@ -585,34 +584,36 @@ impl InterfaceGenerator<'_> {
585584
}}
586585
"#
587586
);
588-
589587
}
590588

591589
if async_ {
592590
let import_module_name = &self.resolve.name_world_key(interface_name.unwrap());
593591

594-
uwriteln!(self.csharp_interop_src,
595-
r#"
592+
uwriteln!(
593+
self.csharp_interop_src,
594+
r#"
596595
[global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(EntryPoint = "[callback][async-lift]{import_module_name}#{wasm_func_name}")]
597596
public static uint {camel_name}Callback(uint eventRaw, uint waitable, uint code)
598597
{{
599598
// TODO: decode the parameters
600599
return (uint)CallbackCode.Exit;
601600
}}
602-
"#);
601+
"#
602+
);
603603

604-
let task_return_param =
605-
match &sig.results[..] {
606-
[] => "",
607-
[_result] => &format!("{} result", wasm_result_type),
608-
_ => unreachable!(),
609-
};
604+
let task_return_param = match &sig.results[..] {
605+
[] => "",
606+
[_result] => &format!("{} result", wasm_result_type),
607+
_ => unreachable!(),
608+
};
610609

611-
uwriteln!(self.csharp_interop_src,
612-
r#"
610+
uwriteln!(
611+
self.csharp_interop_src,
612+
r#"
613613
[global::System.Runtime.InteropServices.DllImportAttribute("[export]{import_module_name}", EntryPoint = "[task-return]{wasm_func_name}"), global::System.Runtime.InteropServices.WasmImportLinkageAttribute]
614614
internal static extern void {camel_name}TaskReturn({task_return_param});
615-
"#);
615+
"#
616+
);
616617
}
617618

618619
if !matches!(&func.kind, FunctionKind::Constructor(_)) {
@@ -779,17 +780,17 @@ impl InterfaceGenerator<'_> {
779780
self.type_name_with_qualifier(&Type::Id(*id), qualifier)
780781
}
781782
TypeDefKind::Future(ty) => {
782-
let name =
783-
ty.as_ref()
784-
.map(|ty| self.type_name_with_qualifier(ty, qualifier))
785-
.unwrap_or_else(|| "".to_owned());
783+
let name = ty
784+
.as_ref()
785+
.map(|ty| self.type_name_with_qualifier(ty, qualifier))
786+
.unwrap_or_else(|| "".to_owned());
786787
if name.is_empty() {
787788
return "Task".to_owned();
788789
} else {
789790
return format!("Task<{name}>");
790791
}
791792
}
792-
_ => {
793+
_ => {
793794
if let Some(name) = &ty.name {
794795
format!(
795796
"{}{}",
@@ -1370,11 +1371,11 @@ fn modifiers(func: &Function, name: &str, direction: Direction) -> String {
13701371
};
13711372

13721373
let static_modifiers = match &func.kind {
1373-
FunctionKind::Freestanding
1374-
| FunctionKind::Static(_)
1375-
| FunctionKind::AsyncFreestanding
1376-
| FunctionKind::AsyncStatic(_) => "static",
1377-
_ => "",
1374+
FunctionKind::Freestanding
1375+
| FunctionKind::Static(_)
1376+
| FunctionKind::AsyncFreestanding
1377+
| FunctionKind::AsyncStatic(_) => "static",
1378+
_ => "",
13781379
};
13791380

13801381
let abstract_modifier = if direction == Direction::Export {
@@ -1386,7 +1387,11 @@ fn modifiers(func: &Function, name: &str, direction: Direction) -> String {
13861387
let async_modifier = match &func.kind {
13871388
FunctionKind::AsyncMethod(_)
13881389
| FunctionKind::AsyncFreestanding
1389-
| FunctionKind::AsyncStatic(_) if abstract_modifier == "" => " async",
1390+
| FunctionKind::AsyncStatic(_)
1391+
if abstract_modifier == "" =>
1392+
{
1393+
" async"
1394+
}
13901395
_ => "",
13911396
};
13921397

crates/test/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,12 @@ impl Runner<'_> {
811811
}
812812
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
813813
.validate_all(&wasm)
814-
.with_context(|| format!("compiler produced invalid wasm file {output:?} for component {}", component.name))?;
814+
.with_context(|| {
815+
format!(
816+
"compiler produced invalid wasm file {output:?} for component {}",
817+
component.name
818+
)
819+
})?;
815820

816821
Ok(output)
817822
}

0 commit comments

Comments
 (0)