@@ -8,11 +8,14 @@ use std::collections::{HashMap, HashSet};
88use std:: fmt:: Write ;
99use std:: ops:: Deref ;
1010use std:: { iter, mem} ;
11- use wit_bindgen_core:: { Direction , Files , InterfaceGenerator as _, WorldGenerator , uwrite, uwriteln} ;
11+ use wit_bindgen_core:: {
12+ uwrite, uwriteln, Direction , Files , InterfaceGenerator as _, WorldGenerator ,
13+ } ;
1214use wit_component:: WitPrinter ;
1315use wit_parser:: abi:: WasmType ;
1416use wit_parser:: {
15- Function , InterfaceId , Resolve , SizeAlign , Type , TypeDefKind , TypeId , TypeOwner , WorldId , WorldItem , WorldKey
17+ Function , InterfaceId , Resolve , SizeAlign , Type , TypeDefKind , TypeId , TypeOwner , WorldId ,
18+ WorldItem , WorldKey ,
1619} ;
1720
1821/// CSharp is the world generator for wit files. It coordinates all the generated code.
@@ -97,7 +100,7 @@ impl CSharp {
97100 }
98101
99102 // We can share some types to save some code size and allow a more intuitive user experience.
100- pub ( crate ) fn type_is_bidirectional ( resolve : & Resolve , ty : & TypeId ) -> bool {
103+ pub ( crate ) fn type_is_bidirectional ( resolve : & Resolve , ty : & TypeId ) -> bool {
101104 let type_def = & resolve. types [ * ty] ;
102105 let kind = & type_def. kind ;
103106
@@ -108,8 +111,19 @@ impl CSharp {
108111 } ;
109112 }
110113
111- fn write_world_fragments ( & mut self , direction : Direction , direction_name : & str , src : & mut String , access : & str , name : & String ) {
112- if self . world_fragments . iter ( ) . any ( |f| f. direction == Some ( direction) ) {
114+ fn write_world_fragments (
115+ & mut self ,
116+ direction : Direction ,
117+ direction_name : & str ,
118+ src : & mut String ,
119+ access : & str ,
120+ name : & String ,
121+ ) {
122+ if self
123+ . world_fragments
124+ . iter ( )
125+ . any ( |f| f. direction == Some ( direction) )
126+ {
113127 uwrite ! (
114128 src,
115129 "
@@ -281,26 +295,25 @@ impl WorldGenerator for CSharp {
281295 let world = & resolve. worlds [ world_id] ;
282296 let mut types = Vec :: new ( ) ;
283297 for ( name, export) in world. exports . iter ( ) {
284- match name {
285- WorldKey :: Name ( name) => {
286- match export {
287- WorldItem :: Type ( id) => {
288- if !CSharp :: type_is_bidirectional ( resolve, & id) {
289- types. push ( ( name. as_str ( ) , * id) )
290- }
291- } ,
292- _ => { } ,
298+ match name {
299+ WorldKey :: Name ( name) => match export {
300+ WorldItem :: Type ( id) => {
301+ if !CSharp :: type_is_bidirectional ( resolve, & id) {
302+ types. push ( ( name. as_str ( ) , * id) )
293303 }
294- } ,
295- WorldKey :: Interface ( _) => { } ,
304+ }
305+ _ => { }
306+ } ,
307+ WorldKey :: Interface ( _) => { }
296308 }
297309 }
298310
299311 if !types. is_empty ( ) {
300312 export_types ( & mut gen, & types) ;
301313 }
302314
303- for ( resource, funcs) in by_resource ( funcs. iter ( ) . copied ( ) ,
315+ for ( resource, funcs) in by_resource (
316+ funcs. iter ( ) . copied ( ) ,
304317 gen. csharp_gen . world_resources . keys ( ) . copied ( ) ,
305318 ) {
306319 if let Some ( resource) = resource {
@@ -384,16 +397,23 @@ impl WorldGenerator for CSharp {
384397 ) ;
385398
386399 src. push_str (
387- & self
388- . world_fragments
389- . iter ( )
390- . filter ( |f| f. direction . is_none ( ) )
391- . map ( |f| f. csharp_src . deref ( ) )
392- . collect :: < Vec < _ > > ( )
393- . join ( "\n " ) ,
394- ) ;
400+ & self
401+ . world_fragments
402+ . iter ( )
403+ . filter ( |f| f. direction . is_none ( ) )
404+ . map ( |f| f. csharp_src . deref ( ) )
405+ . collect :: < Vec < _ > > ( )
406+ . join ( "\n " ) ,
407+ ) ;
395408
396- src. push_str ( self . bidirectional_types_src . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) . as_str ( ) ) ;
409+ src. push_str (
410+ self . bidirectional_types_src
411+ . iter ( )
412+ . cloned ( )
413+ . collect :: < Vec < _ > > ( )
414+ . join ( "\n " )
415+ . as_str ( ) ,
416+ ) ;
397417 self . write_world_fragments ( Direction :: Import , "Imports" , & mut src, access, & name) ;
398418 self . write_world_fragments ( Direction :: Export , "Exports" , & mut src, access, & name) ;
399419
@@ -568,15 +588,25 @@ impl WorldGenerator for CSharp {
568588 if !& self . world_fragments . is_empty ( ) {
569589 src. push_str ( "\n " ) ;
570590
571- if self . world_fragments . iter ( ) . any ( |f| f. direction == Some ( Direction :: Import ) ) {
591+ if self
592+ . world_fragments
593+ . iter ( )
594+ . any ( |f| f. direction == Some ( Direction :: Import ) )
595+ {
572596 src. push_str ( "\n " ) ;
573597
574598 src. push_str ( "namespace Imports {\n " ) ;
575599
576- src. push_str ( & format ! ( "{access} partial class {name}WorldImportsInterop : I{name}WorldImports\n " ) ) ;
600+ src. push_str ( & format ! (
601+ "{access} partial class {name}WorldImportsInterop : I{name}WorldImports\n "
602+ ) ) ;
577603 src. push_str ( "{" ) ;
578604
579- for fragment in self . world_fragments . iter ( ) . filter ( |f| f. direction == Some ( Direction :: Import ) ) {
605+ for fragment in self
606+ . world_fragments
607+ . iter ( )
608+ . filter ( |f| f. direction == Some ( Direction :: Import ) )
609+ {
580610 if !fragment. csharp_interop_src . is_empty ( ) {
581611 src. push_str ( "\n " ) ;
582612
@@ -587,21 +617,28 @@ impl WorldGenerator for CSharp {
587617 src. push_str ( "}\n " ) ;
588618 }
589619
590- if self . world_fragments . iter ( ) . any ( |f| f. direction == Some ( Direction :: Export ) ) {
620+ if self
621+ . world_fragments
622+ . iter ( )
623+ . any ( |f| f. direction == Some ( Direction :: Export ) )
624+ {
591625 src. push_str ( "namespace Exports {\n " ) ;
592626
593627 src. push_str ( & format ! ( "{access} static class {name}WorldInterop\n " ) ) ;
594628 src. push_str ( "{" ) ;
595629
596- for fragment in self . world_fragments . iter ( ) . filter ( |f| f. direction == Some ( Direction :: Export ) ) {
630+ for fragment in self
631+ . world_fragments
632+ . iter ( )
633+ . filter ( |f| f. direction == Some ( Direction :: Export ) )
634+ {
597635 src. push_str ( "\n " ) ;
598636
599637 src. push_str ( & fragment. csharp_interop_src ) ;
600638 }
601639 src. push_str ( "}\n " ) ;
602640 src. push_str ( "}\n " ) ;
603641 }
604-
605642 }
606643
607644 if self . needs_async_support {
@@ -847,18 +884,14 @@ impl WorldGenerator for CSharp {
847884
848885 Ok ( ( ) )
849886 }
850-
851887}
852888
853889enum Stubs < ' a > {
854890 World ( & ' a Vec < InterfaceFragment > ) ,
855891 Interface ( & ' a Vec < InterfaceFragment > ) ,
856892}
857893
858- fn export_types (
859- gen : & mut InterfaceGenerator ,
860- types : & [ ( & str , TypeId ) ] ,
861- ) {
894+ fn export_types ( gen : & mut InterfaceGenerator , types : & [ ( & str , TypeId ) ] ) {
862895 for ( ty_name, ty) in types {
863896 gen. define_type ( ty_name, * ty) ;
864897 }
@@ -932,18 +965,21 @@ fn interface_name(
932965 }
933966 } ;
934967
935- let name = format ! ( "{}{}" , match name {
936- WorldKey :: Name ( name) => name. to_upper_camel_case( ) ,
937- WorldKey :: Interface ( id) => resolve. interfaces[ * id]
938- . name
939- . as_ref( )
940- . unwrap( )
941- . to_upper_camel_case( ) ,
968+ let name = format ! (
969+ "{}{}" ,
970+ match name {
971+ WorldKey :: Name ( name) => name. to_upper_camel_case( ) ,
972+ WorldKey :: Interface ( id) => resolve. interfaces[ * id]
973+ . name
974+ . as_ref( )
975+ . unwrap( )
976+ . to_upper_camel_case( ) ,
942977 } ,
943978 match direction {
944979 Direction :: Import => "Imports" ,
945980 Direction :: Export => "Exports" ,
946- } ) ;
981+ }
982+ ) ;
947983
948984 let namespace = match & pkg {
949985 Some ( name) => {
0 commit comments