@@ -96,8 +96,7 @@ struct ExportField {
96
96
ty : String ,
97
97
ty_index : String ,
98
98
load : String ,
99
- get_index_from_component : String ,
100
- get_index_from_instance : String ,
99
+ get_index : String ,
101
100
}
102
101
103
102
#[ derive( Default , Debug , Clone , Copy ) ]
@@ -613,8 +612,7 @@ impl Wasmtime {
613
612
let ty;
614
613
let ty_index;
615
614
let load;
616
- let get_index_from_component;
617
- let get_index_from_instance;
615
+ let get_index;
618
616
match item {
619
617
WorldItem :: Function ( func) => {
620
618
generator. define_rust_guest_export ( resolve, None , func) ;
@@ -639,20 +637,13 @@ impl Wasmtime {
639
637
}}" ,
640
638
func. name
641
639
) ;
642
- get_index_from_component = format ! (
640
+ get_index = format ! (
643
641
"{{ let (item, index) = _component.get_export(None, \" {}\" )
644
642
.ok_or_else(|| anyhow::anyhow!(\" no export `{0}` found\" ))?;
645
643
{typecheck}
646
644
}}" ,
647
645
func. name
648
646
) ;
649
- get_index_from_instance = format ! (
650
- "{{ let (item, index) = _instance.get_export(&mut store, None, \" {}\" )
651
- .ok_or_else(|| anyhow::anyhow!(\" no function export `{0}` found\" ))?;
652
- {typecheck}
653
- }}" ,
654
- func. name
655
- ) ;
656
647
}
657
648
WorldItem :: Type ( _) => unreachable ! ( ) ,
658
649
WorldItem :: Interface { id, .. } => {
@@ -701,33 +692,13 @@ impl Wasmtime {
701
692
///
702
693
/// This constructor can be used to front-load string lookups to find exports
703
694
/// within a component.
704
- pub fn new(
705
- component: &{wt}::component::Component,
706
- instance_type: &{wt}::component::__internal::InstanceType,
707
- ) -> {wt}::Result<{struct_name}Indices> {{
708
- let instance = component.get_export_index(None, \" {instance_name}\" )
709
- .ok_or_else(|| anyhow::anyhow!(\" no exported instance named `{instance_name}`\" ))?;
710
- Self::_new(instance_type, |name| component.get_export_index(Some(&instance), name))
711
- }}
712
-
713
- /// This constructor is similar to [`{struct_name}Indices::new`] except that it
714
- /// performs string lookups after instantiation time.
715
- pub fn new_instance(
716
- mut store: impl {wt}::AsContextMut,
717
- instance: &{wt}::component::Instance,
695
+ pub fn new<_T>(
696
+ _instance_pre: &{wt}::component::InstancePre<_T>,
718
697
) -> {wt}::Result<{struct_name}Indices> {{
719
- let instance_export = instance. get_export_index(&mut store, None, \" {instance_name}\" )
698
+ let instance = _instance_pre.component(). get_export_index(None, \" {instance_name}\" )
720
699
.ok_or_else(|| anyhow::anyhow!(\" no exported instance named `{instance_name}`\" ))?;
721
- let instance_type = instance.instance_type(&mut store);
722
- Self::_new(&instance_type, |name| instance.get_export_index(&mut store, Some(&instance_export), name))
723
- }}
724
-
725
- fn _new(
726
- _instance_type: &{wt}::component::__internal::InstanceType,
727
- mut lookup: impl FnMut (&str) -> Option<{wt}::component::ComponentExportIndex>,
728
- ) -> {wt}::Result<{struct_name}Indices> {{
729
700
let mut lookup = move |name| {{
730
- lookup( name).ok_or_else(|| {{
701
+ _instance_pre.component().get_export_index(Some(&instance), name).ok_or_else(|| {{
731
702
anyhow::anyhow!(
732
703
\" instance export `{instance_name}` does \\
733
704
not have export `{{name}}`\"
@@ -758,10 +729,11 @@ fn _new(
758
729
mut store: impl {wt}::AsContextMut,
759
730
instance: &{wt}::component::Instance,
760
731
) -> {wt}::Result<{struct_name}> {{
732
+ let _instance = instance;
733
+ let _instance_pre = _instance.instance_pre(&store);
734
+ let _instance_type = _instance_pre.instance_type();
761
735
let mut store = store.as_context_mut();
762
736
let _ = &mut store;
763
- let _instance = instance;
764
- let _instance_type = _instance.instance_type(&mut store);
765
737
"
766
738
) ;
767
739
let mut fields = Vec :: new ( ) ;
@@ -866,9 +838,7 @@ fn _new(
866
838
) ) ;
867
839
ty_index = format ! ( "{path}Indices" ) ;
868
840
ty = path;
869
- get_index_from_component = format ! ( "{ty_index}::new(_component, _instance_type)?" ) ;
870
- get_index_from_instance =
871
- format ! ( "{ty_index}::new_instance(&mut store, _instance)?" ) ;
841
+ get_index = format ! ( "{ty_index}::new(_instance_pre)?" ) ;
872
842
}
873
843
}
874
844
let prev = self . exports . fields . insert (
@@ -877,8 +847,7 @@ fn _new(
877
847
ty,
878
848
ty_index,
879
849
load,
880
- get_index_from_component,
881
- get_index_from_instance,
850
+ get_index,
882
851
} ,
883
852
) ;
884
853
assert ! ( prev. is_none( ) ) ;
@@ -925,7 +894,7 @@ impl<_T> {camel}Pre<_T> {{
925
894
/// This method may fail if the component behind `instance_pre`
926
895
/// does not have the required exports.
927
896
pub fn new(instance_pre: {wt}::component::InstancePre<_T>) -> {wt}::Result<Self> {{
928
- let indices = {camel}Indices::new(instance_pre.component(), instance_pre.instance_type() )?;
897
+ let indices = {camel}Indices::new(& instance_pre)?;
929
898
Ok(Self {{ instance_pre, indices }})
930
899
}}
931
900
@@ -998,11 +967,6 @@ impl<_T> {camel}Pre<_T> {{
998
967
/// * If you've instantiated the instance yourself already
999
968
/// then you can use [`{camel}::new`].
1000
969
///
1001
- /// * You can also access the guts of instantiation through
1002
- /// [`{camel}Indices::new_instance`] followed
1003
- /// by [`{camel}Indices::load`] to crate an instance of this
1004
- /// type.
1005
- ///
1006
970
/// These methods are all equivalent to one another and move
1007
971
/// around the tradeoff of what work is performed when.
1008
972
///
@@ -1035,15 +999,13 @@ impl<_T> {camel}Pre<_T> {{
1035
999
///
1036
1000
/// This method may fail if the component does not have the
1037
1001
/// required exports.
1038
- pub fn new(component: &{wt}::component::Component,
1039
- instance_type: &{wt}::component::__internal::InstanceType,
1040
- ) -> {wt}::Result<Self> {{
1041
- let _component = component;
1042
- let _instance_type = instance_type;
1002
+ pub fn new<_T>(_instance_pre: &{wt}::component::InstancePre<_T>) -> {wt}::Result<Self> {{
1003
+ let _component = _instance_pre.component();
1004
+ let _instance_type = _instance_pre.instance_type();
1043
1005
" ,
1044
1006
) ;
1045
1007
for ( name, field) in self . exports . fields . iter ( ) {
1046
- uwriteln ! ( self . src, "let {name} = {};" , field. get_index_from_component ) ;
1008
+ uwriteln ! ( self . src, "let {name} = {};" , field. get_index ) ;
1047
1009
}
1048
1010
uwriteln ! ( self . src, "Ok({camel}Indices {{" ) ;
1049
1011
for ( name, _) in self . exports . fields . iter ( ) {
@@ -1052,34 +1014,6 @@ impl<_T> {camel}Pre<_T> {{
1052
1014
uwriteln ! ( self . src, "}})" ) ;
1053
1015
uwriteln ! ( self . src, "}}" ) ; // close `fn new`
1054
1016
1055
- uwriteln ! (
1056
- self . src,
1057
- "
1058
- /// Creates a new instance of [`{camel}Indices`] from an
1059
- /// instantiated component.
1060
- ///
1061
- /// This method of creating a [`{camel}`] will perform string
1062
- /// lookups for all exports when this method is called. This
1063
- /// will only succeed if the provided instance matches the
1064
- /// requirements of [`{camel}`].
1065
- pub fn new_instance(
1066
- mut store: impl {wt}::AsContextMut,
1067
- instance: &{wt}::component::Instance,
1068
- ) -> {wt}::Result<Self> {{
1069
- let _instance = instance;
1070
- let _instance_type = _instance.instance_type(&mut store);
1071
- " ,
1072
- ) ;
1073
- for ( name, field) in self . exports . fields . iter ( ) {
1074
- uwriteln ! ( self . src, "let {name} = {};" , field. get_index_from_instance) ;
1075
- }
1076
- uwriteln ! ( self . src, "Ok({camel}Indices {{" ) ;
1077
- for ( name, _) in self . exports . fields . iter ( ) {
1078
- uwriteln ! ( self . src, "{name}," ) ;
1079
- }
1080
- uwriteln ! ( self . src, "}})" ) ;
1081
- uwriteln ! ( self . src, "}}" ) ; // close `fn new_instance`
1082
-
1083
1017
uwriteln ! (
1084
1018
self . src,
1085
1019
"
@@ -1093,6 +1027,7 @@ impl<_T> {camel}Pre<_T> {{
1093
1027
mut store: impl {wt}::AsContextMut,
1094
1028
instance: &{wt}::component::Instance,
1095
1029
) -> {wt}::Result<{camel}> {{
1030
+ let _ = &mut store;
1096
1031
let _instance = instance;
1097
1032
" ,
1098
1033
) ;
@@ -1113,7 +1048,7 @@ impl<_T> {camel}Pre<_T> {{
1113
1048
/// Convenience wrapper around [`{camel}Pre::new`] and
1114
1049
/// [`{camel}Pre::instantiate{async__}`].
1115
1050
pub {async_} fn instantiate{async__}<_T>(
1116
- mut store: impl {wt}::AsContextMut<Data = _T>,
1051
+ store: impl {wt}::AsContextMut<Data = _T>,
1117
1052
component: &{wt}::component::Component,
1118
1053
linker: &{wt}::component::Linker<_T>,
1119
1054
) -> {wt}::Result<{camel}>
@@ -1123,13 +1058,13 @@ impl<_T> {camel}Pre<_T> {{
1123
1058
{camel}Pre::new(pre)?.instantiate{async__}(store){await_}
1124
1059
}}
1125
1060
1126
- /// Convenience wrapper around [`{camel}Indices::new_instance `] and
1061
+ /// Convenience wrapper around [`{camel}Indices::new `] and
1127
1062
/// [`{camel}Indices::load`].
1128
1063
pub fn new(
1129
1064
mut store: impl {wt}::AsContextMut,
1130
1065
instance: &{wt}::component::Instance,
1131
1066
) -> {wt}::Result<{camel}> {{
1132
- let indices = {camel}Indices::new_instance(&mut store, instance)?;
1067
+ let indices = {camel}Indices::new(& instance.instance_pre(&store) )?;
1133
1068
indices.load(&mut store, instance)
1134
1069
}}
1135
1070
" ,
0 commit comments