Generates an `export!` macro for the `world_id` specified. This will generate a macro which will then itself invoke all the other macros collected in `self.export_macros` prior. All these macros are woven together in this single invocation.
(&mut self, resolve: &Resolve, world_id: WorldId)
| 864 | /// other macros collected in `self.export_macros` prior. All these macros |
| 865 | /// are woven together in this single invocation. |
| 866 | fn finish_export_macro(&mut self, resolve: &Resolve, world_id: WorldId) { |
| 867 | if self.export_macros.is_empty() { |
| 868 | return; |
| 869 | } |
| 870 | let world = &resolve.worlds[world_id]; |
| 871 | let world_name = world.name.to_snake_case(); |
| 872 | |
| 873 | let default_bindings_module = self |
| 874 | .opts |
| 875 | .default_bindings_module |
| 876 | .clone() |
| 877 | .unwrap_or("self".to_string()); |
| 878 | let (macro_export, use_vis) = if self.opts.pub_export_macro { |
| 879 | ("#[macro_export]", "pub") |
| 880 | } else { |
| 881 | ("", "pub(crate)") |
| 882 | }; |
| 883 | let export_macro_name = self |
| 884 | .opts |
| 885 | .export_macro_name |
| 886 | .as_deref() |
| 887 | .unwrap_or("export") |
| 888 | .to_string(); |
| 889 | uwriteln!( |
| 890 | self.src, |
| 891 | r#" |
| 892 | /// Generates `#[unsafe(no_mangle)]` functions to export the specified type as |
| 893 | /// the root implementation of all generated traits. |
| 894 | /// |
| 895 | /// For more information see the documentation of `wit_bindgen::generate!`. |
| 896 | /// |
| 897 | /// ```rust |
| 898 | /// # macro_rules! {export_macro_name} {{ ($($t:tt)*) => (); }} |
| 899 | /// # trait Guest {{}} |
| 900 | /// struct MyType; |
| 901 | /// |
| 902 | /// impl Guest for MyType {{ |
| 903 | /// // ... |
| 904 | /// }} |
| 905 | /// |
| 906 | /// {export_macro_name}!(MyType); |
| 907 | /// ``` |
| 908 | #[allow(unused_macros)] |
| 909 | #[doc(hidden)] |
| 910 | {macro_export} |
| 911 | macro_rules! __export_{world_name}_impl {{ |
| 912 | ($ty:ident) => ({default_bindings_module}::{export_macro_name}!($ty with_types_in {default_bindings_module});); |
| 913 | ($ty:ident with_types_in $($path_to_types_root:tt)*) => ("# |
| 914 | ); |
| 915 | for (name, path_to_types) in self.export_macros.iter() { |
| 916 | let mut path = "$($path_to_types_root)*".to_string(); |
| 917 | if !path_to_types.is_empty() { |
| 918 | path.push_str("::"); |
| 919 | path.push_str(path_to_types) |
| 920 | } |
| 921 | uwriteln!(self.src, "{path}::{name}!($ty with_types_in {path});"); |
| 922 | } |
| 923 |