Skip to content

Commit f288d38

Browse files
committed
wit-bindgen: reduce to a single Indices constructor which takes InstancePre
1 parent 43d39bc commit f288d38

File tree

1 file changed

+21
-86
lines changed

1 file changed

+21
-86
lines changed

crates/wit-bindgen/src/lib.rs

+21-86
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ struct ExportField {
9696
ty: String,
9797
ty_index: String,
9898
load: String,
99-
get_index_from_component: String,
100-
get_index_from_instance: String,
99+
get_index: String,
101100
}
102101

103102
#[derive(Default, Debug, Clone, Copy)]
@@ -613,8 +612,7 @@ impl Wasmtime {
613612
let ty;
614613
let ty_index;
615614
let load;
616-
let get_index_from_component;
617-
let get_index_from_instance;
615+
let get_index;
618616
match item {
619617
WorldItem::Function(func) => {
620618
generator.define_rust_guest_export(resolve, None, func);
@@ -639,20 +637,13 @@ impl Wasmtime {
639637
}}",
640638
func.name
641639
);
642-
get_index_from_component = format!(
640+
get_index = format!(
643641
"{{ let (item, index) = _component.get_export(None, \"{}\")
644642
.ok_or_else(|| anyhow::anyhow!(\"no export `{0}` found\"))?;
645643
{typecheck}
646644
}}",
647645
func.name
648646
);
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-
);
656647
}
657648
WorldItem::Type(_) => unreachable!(),
658649
WorldItem::Interface { id, .. } => {
@@ -701,33 +692,13 @@ impl Wasmtime {
701692
///
702693
/// This constructor can be used to front-load string lookups to find exports
703694
/// 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>,
718697
) -> {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}\")
720699
.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> {{
729700
let mut lookup = move |name| {{
730-
lookup(name).ok_or_else(|| {{
701+
_instance_pre.component().get_export_index(Some(&instance), name).ok_or_else(|| {{
731702
anyhow::anyhow!(
732703
\"instance export `{instance_name}` does \\
733704
not have export `{{name}}`\"
@@ -758,10 +729,11 @@ fn _new(
758729
mut store: impl {wt}::AsContextMut,
759730
instance: &{wt}::component::Instance,
760731
) -> {wt}::Result<{struct_name}> {{
732+
let _instance = instance;
733+
let _instance_pre = _instance.instance_pre(&store);
734+
let _instance_type = _instance_pre.instance_type();
761735
let mut store = store.as_context_mut();
762736
let _ = &mut store;
763-
let _instance = instance;
764-
let _instance_type = _instance.instance_type(&mut store);
765737
"
766738
);
767739
let mut fields = Vec::new();
@@ -866,9 +838,7 @@ fn _new(
866838
));
867839
ty_index = format!("{path}Indices");
868840
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)?");
872842
}
873843
}
874844
let prev = self.exports.fields.insert(
@@ -877,8 +847,7 @@ fn _new(
877847
ty,
878848
ty_index,
879849
load,
880-
get_index_from_component,
881-
get_index_from_instance,
850+
get_index,
882851
},
883852
);
884853
assert!(prev.is_none());
@@ -925,7 +894,7 @@ impl<_T> {camel}Pre<_T> {{
925894
/// This method may fail if the component behind `instance_pre`
926895
/// does not have the required exports.
927896
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)?;
929898
Ok(Self {{ instance_pre, indices }})
930899
}}
931900
@@ -998,11 +967,6 @@ impl<_T> {camel}Pre<_T> {{
998967
/// * If you've instantiated the instance yourself already
999968
/// then you can use [`{camel}::new`].
1000969
///
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-
///
1006970
/// These methods are all equivalent to one another and move
1007971
/// around the tradeoff of what work is performed when.
1008972
///
@@ -1035,15 +999,13 @@ impl<_T> {camel}Pre<_T> {{
1035999
///
10361000
/// This method may fail if the component does not have the
10371001
/// 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();
10431005
",
10441006
);
10451007
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);
10471009
}
10481010
uwriteln!(self.src, "Ok({camel}Indices {{");
10491011
for (name, _) in self.exports.fields.iter() {
@@ -1052,34 +1014,6 @@ impl<_T> {camel}Pre<_T> {{
10521014
uwriteln!(self.src, "}})");
10531015
uwriteln!(self.src, "}}"); // close `fn new`
10541016

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-
10831017
uwriteln!(
10841018
self.src,
10851019
"
@@ -1093,6 +1027,7 @@ impl<_T> {camel}Pre<_T> {{
10931027
mut store: impl {wt}::AsContextMut,
10941028
instance: &{wt}::component::Instance,
10951029
) -> {wt}::Result<{camel}> {{
1030+
let _ = &mut store;
10961031
let _instance = instance;
10971032
",
10981033
);
@@ -1113,7 +1048,7 @@ impl<_T> {camel}Pre<_T> {{
11131048
/// Convenience wrapper around [`{camel}Pre::new`] and
11141049
/// [`{camel}Pre::instantiate{async__}`].
11151050
pub {async_} fn instantiate{async__}<_T>(
1116-
mut store: impl {wt}::AsContextMut<Data = _T>,
1051+
store: impl {wt}::AsContextMut<Data = _T>,
11171052
component: &{wt}::component::Component,
11181053
linker: &{wt}::component::Linker<_T>,
11191054
) -> {wt}::Result<{camel}>
@@ -1123,13 +1058,13 @@ impl<_T> {camel}Pre<_T> {{
11231058
{camel}Pre::new(pre)?.instantiate{async__}(store){await_}
11241059
}}
11251060
1126-
/// Convenience wrapper around [`{camel}Indices::new_instance`] and
1061+
/// Convenience wrapper around [`{camel}Indices::new`] and
11271062
/// [`{camel}Indices::load`].
11281063
pub fn new(
11291064
mut store: impl {wt}::AsContextMut,
11301065
instance: &{wt}::component::Instance,
11311066
) -> {wt}::Result<{camel}> {{
1132-
let indices = {camel}Indices::new_instance(&mut store, instance)?;
1067+
let indices = {camel}Indices::new(&instance.instance_pre(&store))?;
11331068
indices.load(&mut store, instance)
11341069
}}
11351070
",

0 commit comments

Comments
 (0)