@@ -102,14 +102,6 @@ use std::io::Write;
102102use std:: path:: PathBuf ;
103103use std:: sync:: Mutex ;
104104
105- macro_rules! move_out {
106- ( $field: expr) => { {
107- let mut tmp = Default :: default ( ) ;
108- core:: mem:: swap( & mut tmp, & mut $field) ;
109- tmp
110- } } ;
111- }
112-
113105// TokenStream isn't Send/Sync
114106type SyncTokenStream = String ;
115107
@@ -170,7 +162,7 @@ pub struct BakedExporter {
170162 BTreeMap < SyncTokenStream , HashSet < ( DataLocale , DataMarkerAttributes ) > > ,
171163 > ,
172164 > ,
173- /// (Key, Marker ) pairs to wire up in mod.rs. This is populated by `flush` and consumed by `close`.
165+ /// (marker, file name ) pairs to wire up in mod.rs. This is populated by `flush` and consumed by `close`.
174166 impl_data : Mutex < BTreeMap < DataMarkerInfo , SyncTokenStream > > ,
175167 // List of dependencies used by baking.
176168 dependencies : CrateEnv ,
@@ -276,7 +268,7 @@ impl BakedExporter {
276268 }
277269
278270 fn print_deps ( & mut self ) {
279- let mut deps = move_out ! ( self . dependencies)
271+ let mut deps = core :: mem :: take ( & mut self . dependencies )
280272 . into_iter ( )
281273 . collect :: < BTreeSet < _ > > ( ) ;
282274 if !self . use_separate_crates {
@@ -298,33 +290,44 @@ impl BakedExporter {
298290
299291 fn write_impl_macros (
300292 & self ,
293+ marker : DataMarkerInfo ,
301294 body : TokenStream ,
302295 iterable_body : TokenStream ,
303- marker : DataMarkerInfo ,
304- marker_bake : TokenStream ,
305296 ) -> Result < ( ) , DataError > {
306- let marker_string = marker_bake . to_string ( ) ;
307- let marker_last = marker_bake . into_iter ( ) . last ( ) . unwrap ( ) ;
297+ let marker_unqualified = bake_marker ( marker ) . into_iter ( ) . last ( ) . unwrap ( ) ;
298+
308299 let doc = format ! (
309300 " Implement `DataProvider<{}>` on the given struct using the data" ,
310- marker_last
301+ marker_unqualified
311302 ) ;
312303 let doc_iterable = format ! (
313304 " Implement `IterableDataProvider<{}>` on the given struct using the data" ,
314- marker_last
305+ marker_unqualified
315306 ) ;
316307
317- let ident = Self :: ident ( marker) ;
308+ let ident = marker
309+ . path
310+ . replace ( '/' , "_" )
311+ . replace ( '@' , "_v" )
312+ . to_ascii_lowercase ( ) ;
313+
314+ let macro_ident = format ! ( "impl_{ident}" , ) . parse :: < TokenStream > ( ) . unwrap ( ) ;
315+ let macro_ident_iterable = format ! ( "impliterable_{ident}" )
316+ . parse :: < TokenStream > ( )
317+ . unwrap ( ) ;
318318
319- let prefixed_macro_ident = format ! ( "__impl_{ident}" ) . parse :: < TokenStream > ( ) . unwrap ( ) ;
320- let prefixed_macro_ident_iterable = format ! ( "__impliterable_{ident}" )
319+ // We prefix all macros with `__`, as these will be automatically exported at the crate root, which is annoying
320+ // for crates that include the data but don't want it to be public. We then reexport them as items that use
321+ // normal scoping that clients can control.
322+ let prefixed_macro_ident = format ! ( "__{macro_ident}" ) . parse :: < TokenStream > ( ) . unwrap ( ) ;
323+ let prefixed_macro_ident_iterable = format ! ( "__{macro_ident_iterable}" )
321324 . parse :: < TokenStream > ( )
322325 . unwrap ( ) ;
323326
324327 let maybe_msrv = maybe_msrv ( ) ;
325328
326329 self . write_to_file (
327- PathBuf :: from ( format ! ( "macros/{ }.rs.data" , ident ) ) ,
330+ PathBuf :: from ( format ! ( "{ident }.rs.data" ) ) ,
328331 quote ! {
329332 #[ doc = #doc]
330333 /// hardcoded in this file. This allows the struct to be used with
@@ -338,6 +341,9 @@ impl BakedExporter {
338341 #body
339342 }
340343 }
344+ #[ doc( inline) ]
345+ pub use #prefixed_macro_ident as #macro_ident;
346+
341347 #[ doc = #doc_iterable]
342348 /// hardcoded in this file. This allows the struct to be used with
343349 /// `DatagenDriver` for this marker.
@@ -348,23 +354,14 @@ impl BakedExporter {
348354 #iterable_body
349355 }
350356 }
357+ #[ doc( inline) ]
358+ pub use #prefixed_macro_ident_iterable as #macro_ident_iterable;
351359 } ,
352360 ) ?;
353361
354- self . impl_data
355- . lock ( )
356- . expect ( "poison" )
357- . insert ( marker, marker_string) ;
362+ self . impl_data . lock ( ) . expect ( "poison" ) . insert ( marker, ident) ;
358363 Ok ( ( ) )
359364 }
360-
361- fn ident ( marker : DataMarkerInfo ) -> String {
362- marker
363- . path
364- . to_ascii_lowercase ( )
365- . replace ( '@' , "_v" )
366- . replace ( '/' , "_" )
367- }
368365}
369366
370367impl DataExporter for BakedExporter {
@@ -393,17 +390,25 @@ impl DataExporter for BakedExporter {
393390 marker : DataMarkerInfo ,
394391 payload : & DataPayload < ExportMarker > ,
395392 ) -> Result < ( ) , DataError > {
396- let marker_bake = bake_marker ( marker, & self . dependencies ) ;
397-
398- let singleton_ident = format ! ( "SINGLETON_{}" , Self :: ident( marker) . to_ascii_uppercase( ) )
399- . parse :: < TokenStream > ( )
400- . unwrap ( ) ;
393+ let marker_bake = bake_marker ( marker) ;
394+
395+ let singleton_ident = format ! (
396+ "SINGLETON_{}" ,
397+ marker
398+ . path
399+ . get( )
400+ . replace( '/' , "_" )
401+ . replace( '@' , "_v" )
402+ . to_ascii_uppercase( )
403+ )
404+ . parse :: < TokenStream > ( )
405+ . unwrap ( ) ;
401406
402407 let bake = payload. tokenize ( & self . dependencies ) ;
403408
404409 let maybe_msrv = maybe_msrv ( ) ;
405410
406- self . write_impl_macros ( quote ! {
411+ self . write_impl_macros ( marker , quote ! {
407412 #maybe_msrv
408413 impl $provider {
409414 // Exposing singleton structs as consts allows us to get rid of fallibility
@@ -434,11 +439,11 @@ impl DataExporter for BakedExporter {
434439 Ok ( HashSet :: from_iter( [ Default :: default ( ) ] ) )
435440 }
436441 }
437- } , marker , marker_bake )
442+ } )
438443 }
439444
440445 fn flush ( & self , marker : DataMarkerInfo ) -> Result < ( ) , DataError > {
441- let marker_bake = bake_marker ( marker, & self . dependencies ) ;
446+ let marker_bake = bake_marker ( marker) ;
442447
443448 let ( struct_type, into_data_payload) = if marker_bake
444449 . to_string ( )
@@ -608,6 +613,7 @@ impl DataExporter for BakedExporter {
608613 } ;
609614
610615 self . write_impl_macros (
616+ marker,
611617 quote ! {
612618 #maybe_msrv
613619 impl icu_provider:: DataProvider <#marker_bake> for $provider {
@@ -627,54 +633,34 @@ impl DataExporter for BakedExporter {
627633 }
628634 }
629635 } ,
630- marker,
631- marker_bake,
632636 )
633637 }
634638
635639 fn close ( & mut self ) -> Result < ( ) , DataError > {
636640 log:: info!( "Writing macros module..." ) ;
637641
638- let data = move_out ! ( self . impl_data) . into_inner ( ) . expect ( "poison" ) ;
639-
640- let marker_bakes = data
641- . values ( )
642- . map ( |marker| marker. parse :: < TokenStream > ( ) . unwrap ( ) )
643- . collect :: < Vec < _ > > ( ) ;
644-
645- let (
646- macro_idents,
647- prefixed_macro_idents,
648- macro_idents_iterable,
649- prefixed_macro_idents_iterable,
650- mod_idents,
651- file_paths,
652- ) : ( Vec < _ > , Vec < _ > , Vec < _ > , Vec < _ > , Vec < _ > , Vec < _ > ) =
653- itertools:: multiunzip ( data. keys ( ) . map ( |& marker| {
654- let ident = Self :: ident ( marker) ;
655- (
656- format ! ( "impl_{}" , ident) . parse :: < TokenStream > ( ) . unwrap ( ) ,
657- // We prefix all macros with `__`, as these will be automatically exported at the crate root, which is annoying
658- // for crates that include the data but don't want it to be public. We then reexport them as items that use
659- // normal scoping that clients can control.
660- format ! ( "__impl_{}" , ident) . parse :: < TokenStream > ( ) . unwrap ( ) ,
661- format ! ( "impliterable_{}" , ident)
662- . parse :: < TokenStream > ( )
663- . unwrap ( ) ,
664- format ! ( "__impliterable_{}" , ident)
665- . parse :: < TokenStream > ( )
666- . unwrap ( ) ,
667- ident. parse :: < TokenStream > ( ) . unwrap ( ) ,
668- format ! ( "macros/{}.rs.data" , ident) ,
669- )
670- } ) ) ;
642+ let data = core:: mem:: take ( & mut self . impl_data )
643+ . into_inner ( )
644+ . expect ( "poison" ) ;
671645
672646 let maybe_msrv = maybe_msrv ( ) ;
673647
674- // macros.rs is the interface for built-in data. It exposes one macro per marker.
648+ let marker_bakes = data. keys ( ) . copied ( ) . map ( bake_marker) ;
649+
650+ let file_paths = data. values ( ) . map ( |i| format ! ( "{i}.rs.data" ) ) ;
651+
652+ let macro_idents = data
653+ . values ( )
654+ . map ( |i| format ! ( "impl_{i}" ) . parse :: < TokenStream > ( ) . unwrap ( ) ) ;
655+
656+ // mod.rs is the interface for built-in data. It exposes one macro per marker.
675657 self . write_to_file (
676- PathBuf :: from ( "macros .rs" ) ,
658+ PathBuf :: from ( "mod .rs" ) ,
677659 quote ! {
660+ #(
661+ include!( #file_paths) ;
662+ ) *
663+
678664 /// Marks a type as a data provider. You can then use macros like
679665 /// `impl_core_helloworld_v1` to add implementations.
680666 ///
@@ -700,26 +686,9 @@ impl DataExporter for BakedExporter {
700686 }
701687 #[ doc( inline) ]
702688 pub use __make_provider as make_provider;
703- #(
704- #[ macro_use]
705- #[ path = #file_paths]
706- mod #mod_idents;
707- #[ doc( inline) ]
708- pub use #prefixed_macro_idents as #macro_idents;
709- #[ doc( inline) ]
710- pub use #prefixed_macro_idents_iterable as #macro_idents_iterable;
711- ) *
712- } ,
713- ) ?;
714689
715- // mod.rs is the interface for using databake directly. It exposes the macros from macros.rs,
716- // as well as `impl_data_provider` and `impl_any_provider` which include all markers.
717- self . write_to_file (
718- PathBuf :: from ( "mod.rs" ) ,
719- quote ! {
720- include!( "macros.rs" ) ;
721-
722- // Not public as it will only work locally due to needing access to the macros from `macros.rs`.
690+ // Not public as it will only work locally due to needing access to the other macros.
691+ #[ allow( unused_macros) ]
723692 macro_rules! impl_data_provider {
724693 ( $provider: ty) => {
725694 make_provider!( $provider) ;
@@ -729,8 +698,7 @@ impl DataExporter for BakedExporter {
729698 } ;
730699 }
731700
732- // Not public because `impl_data_provider` isn't. Users can implement `DynamicDataProvider<AnyMarker>`
733- // using `impl_dynamic_data_provider!`.
701+ // Not public because `impl_data_provider` isn't.
734702 #[ allow( unused_macros) ]
735703 macro_rules! impl_any_provider {
736704 ( $provider: ty) => {
@@ -748,20 +716,6 @@ impl DataExporter for BakedExporter {
748716 }
749717 }
750718 }
751-
752- // For backwards compatibility
753- #maybe_msrv
754- pub struct BakedDataProvider ;
755- impl_data_provider!( BakedDataProvider ) ;
756- } ,
757- ) ?;
758-
759- // For backwards compatibility
760- self . write_to_file (
761- PathBuf :: from ( "any.rs" ) ,
762- quote ! {
763- // This assumes that `mod.rs` is already included.
764- impl_any_provider!( BakedDataProvider ) ;
765719 } ,
766720 ) ?;
767721
@@ -773,20 +727,26 @@ impl DataExporter for BakedExporter {
773727
774728macro_rules! cb {
775729 ( $( $marker: path = $path: literal, ) + #[ experimental] $( $emarker: path = $epath: literal, ) +) => {
776- fn bake_marker( marker: DataMarkerInfo , _env : & databake :: CrateEnv ) -> databake:: TokenStream {
730+ fn bake_marker( marker: DataMarkerInfo ) -> databake:: TokenStream {
777731 if * marker. path == * icu_provider:: hello_world:: HelloWorldV1Marker :: INFO . path {
778732 return databake:: quote!( icu_provider:: hello_world:: HelloWorldV1Marker ) ;
779733 }
780734
781735 $(
782736 if * marker. path == * $path {
783- return databake:: quote!( $marker) ;
737+ return stringify!( $marker)
738+ . replace( "icu :: " , "icu_" )
739+ . parse( )
740+ . unwrap( ) ;
784741 }
785742 ) +
786743
787744 $(
788745 if * marker. path == * $epath {
789- return databake:: quote!( $emarker) ;
746+ return stringify!( $emarker)
747+ . replace( "icu :: " , "icu_" )
748+ . parse( )
749+ . unwrap( ) ;
790750 }
791751 ) +
792752
0 commit comments