Skip to content

Commit 967d075

Browse files
authored
fix: re-export async_support in rt (#1136)
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent 629ced7 commit 967d075

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

crates/guest-rust/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,5 +877,8 @@ pub mod rt {
877877
#[cfg(all(feature = "realloc", not(target_env = "p2")))]
878878
pub use wit_bindgen_rt::cabi_realloc;
879879

880+
#[cfg(feature = "async")]
881+
pub use wit_bindgen_rt::async_support;
882+
880883
pub use crate::pre_wit_bindgen_0_20_0::*;
881884
}

crates/rust/src/bindgen.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
481481
}
482482

483483
Instruction::FutureLift { payload, .. } => {
484-
let async_support = self.gen.path_to_async_support();
484+
let async_support = self.gen.gen.async_support_path();
485485
let op = &operands[0];
486486
let name = payload
487487
.as_ref()
@@ -504,7 +504,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
504504
}
505505

506506
Instruction::StreamLift { payload, .. } => {
507-
let async_support = self.gen.path_to_async_support();
507+
let async_support = self.gen.gen.async_support_path();
508508
let op = &operands[0];
509509
let name = self
510510
.gen
@@ -523,7 +523,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
523523
}
524524

525525
Instruction::ErrorContextLift { .. } => {
526-
let async_support = self.gen.path_to_async_support();
526+
let async_support = self.gen.gen.async_support_path();
527527
let op = &operands[0];
528528
results.push(format!(
529529
"{async_support}::ErrorContext::from_handle({op} as u32)"
@@ -869,7 +869,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
869869
Instruction::AsyncCallWasm { name, size, align } => {
870870
let func = self.declare_import(name, &[WasmType::Pointer; 2], &[WasmType::I32]);
871871

872-
let async_support = self.gen.path_to_async_support();
872+
let async_support = self.gen.gen.async_support_path();
873873
let tmp = self.tmp();
874874
let layout = format!("layout{tmp}");
875875
let alloc = self.gen.path_to_std_alloc_module();
@@ -906,7 +906,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
906906
.unwrap()
907907
.to_upper_camel_case();
908908
let call = if self.async_ {
909-
let async_support = self.gen.path_to_async_support();
909+
let async_support = self.gen.gen.async_support_path();
910910
format!("{async_support}::futures::FutureExt::map(T::new")
911911
} else {
912912
format!("{ty}::new(T::new",)
@@ -975,7 +975,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
975975
} else {
976976
params
977977
};
978-
let async_support = self.gen.path_to_async_support();
978+
let async_support = self.gen.gen.async_support_path();
979979
// TODO: This relies on `abi::Generator` emitting
980980
// `AsyncCallReturn` immediately after this instruction to
981981
// complete the incomplete expression we generate here. We

crates/rust/src/interface.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ macro_rules! {macro_name} {{
497497
.unwrap_or_else(|| "$root".into())
498498
);
499499
let func_name = &func.name;
500-
let async_support = self.path_to_async_support();
500+
let async_support = self.gen.async_support_path();
501501

502502
match &self.resolve.types[ty].kind {
503503
TypeDefKind::Future(payload_type) => {
@@ -1116,7 +1116,7 @@ pub mod vtable{ordinal} {{
11161116
self.src.push_str("}\n");
11171117

11181118
if async_ {
1119-
let async_support = self.path_to_async_support();
1119+
let async_support = self.gen.async_support_path();
11201120
uwrite!(
11211121
self.src,
11221122
"\
@@ -2513,10 +2513,6 @@ pub mod vtable{ordinal} {{
25132513
self.path_from_runtime_module(RuntimeItem::StdAllocModule, "alloc")
25142514
}
25152515

2516-
pub fn path_to_async_support(&mut self) -> String {
2517-
"::wit_bindgen_rt::async_support".into()
2518-
}
2519-
25202516
fn path_from_runtime_module(
25212517
&mut self,
25222518
item: RuntimeItem,
@@ -2837,7 +2833,7 @@ impl<'a> {camel}Borrow<'a>{{
28372833
}
28382834

28392835
fn type_future(&mut self, _id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
2840-
let async_support = self.path_to_async_support();
2836+
let async_support = self.gen.async_support_path();
28412837
let mode = TypeMode {
28422838
style: TypeOwnershipStyle::Owned,
28432839
lists_borrowed: false,
@@ -2854,7 +2850,7 @@ impl<'a> {camel}Borrow<'a>{{
28542850
}
28552851

28562852
fn type_stream(&mut self, _id: TypeId, name: &str, ty: &Type, docs: &Docs) {
2857-
let async_support = self.path_to_async_support();
2853+
let async_support = self.gen.async_support_path();
28582854
let mode = TypeMode {
28592855
style: TypeOwnershipStyle::Owned,
28602856
lists_borrowed: false,
@@ -2871,7 +2867,7 @@ impl<'a> {camel}Borrow<'a>{{
28712867
}
28722868

28732869
fn type_error_context(&mut self, _id: TypeId, name: &str, docs: &Docs) {
2874-
let async_support = self.path_to_async_support();
2870+
let async_support = self.gen.async_support_path();
28752871
self.rustdoc(docs);
28762872
self.push_str(&format!("pub type {} = ", name.to_upper_camel_case()));
28772873
self.push_str(&format!("{async_support}::ErrorContext"));
@@ -2967,7 +2963,7 @@ impl<'a, 'b> wit_bindgen_core::AnonymousTypeGenerator<'a> for AnonTypeGenerator<
29672963
}
29682964

29692965
fn anonymous_type_future(&mut self, _id: TypeId, ty: &Option<Type>, _docs: &Docs) {
2970-
let async_support = self.interface.path_to_async_support();
2966+
let async_support = self.interface.gen.async_support_path();
29712967
let mode = TypeMode {
29722968
style: TypeOwnershipStyle::Owned,
29732969
lists_borrowed: false,
@@ -2980,7 +2976,7 @@ impl<'a, 'b> wit_bindgen_core::AnonymousTypeGenerator<'a> for AnonTypeGenerator<
29802976
}
29812977

29822978
fn anonymous_type_stream(&mut self, _id: TypeId, ty: &Type, _docs: &Docs) {
2983-
let async_support = self.interface.path_to_async_support();
2979+
let async_support = self.interface.gen.async_support_path();
29842980
let mode = TypeMode {
29852981
style: TypeOwnershipStyle::Owned,
29862982
lists_borrowed: false,
@@ -2993,7 +2989,7 @@ impl<'a, 'b> wit_bindgen_core::AnonymousTypeGenerator<'a> for AnonTypeGenerator<
29932989
}
29942990

29952991
fn anonymous_type_error_context(&mut self) {
2996-
let async_support = self.interface.path_to_async_support();
2992+
let async_support = self.interface.gen.async_support_path();
29972993
self.interface
29982994
.push_str(&format!("{async_support}::ErrorContext"));
29992995
}

crates/rust/src/lib.rs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ impl RustWasm {
395395
.unwrap_or(format!("{}::bitflags", self.runtime_path()))
396396
}
397397

398+
fn async_support_path(&self) -> String {
399+
format!("{}::async_support", self.runtime_path())
400+
}
401+
398402
fn name_interface(
399403
&mut self,
400404
resolve: &Resolve,
@@ -456,71 +460,73 @@ impl RustWasm {
456460
self.src.push_str("}\n");
457461

458462
if !self.future_payloads.is_empty() {
459-
self.src.push_str(
463+
let async_support = self.async_support_path();
464+
self.src.push_str(&format!(
460465
"\
461-
pub mod wit_future {
466+
pub mod wit_future {{
462467
#![allow(dead_code, unused_variables, clippy::all)]
463468
464469
#[doc(hidden)]
465-
pub trait FuturePayload: Unpin + Sized + 'static {
466-
fn new() -> (u32, &'static ::wit_bindgen_rt::async_support::FutureVtable<Self>);
467-
}",
468-
);
470+
pub trait FuturePayload: Unpin + Sized + 'static {{
471+
fn new() -> (u32, &'static {async_support}::FutureVtable<Self>);
472+
}}"
473+
));
469474
for code in self.future_payloads.values() {
470475
self.src.push_str(code);
471476
}
472-
self.src.push_str(
477+
self.src.push_str(&format!(
473478
"\
474479
/// Creates a new Component Model `future` with the specified payload type.
475-
pub fn new<T: FuturePayload>() -> (::wit_bindgen_rt::async_support::FutureWriter<T>, ::wit_bindgen_rt::async_support::FutureReader<T>) {
480+
pub fn new<T: FuturePayload>() -> ({async_support}::FutureWriter<T>, {async_support}::FutureReader<T>) {{
476481
let (handle, vtable) = T::new();
477-
::wit_bindgen_rt::async_support::with_entry(handle, |entry| match entry {
478-
::std::collections::hash_map::Entry::Vacant(entry) => {
479-
entry.insert(::wit_bindgen_rt::async_support::Handle::LocalOpen);
480-
}
482+
{async_support}::with_entry(handle, |entry| match entry {{
483+
::std::collections::hash_map::Entry::Vacant(entry) => {{
484+
entry.insert({async_support}::Handle::LocalOpen);
485+
}}
481486
::std::collections::hash_map::Entry::Occupied(_) => unreachable!(),
482-
});
487+
}});
483488
(
484-
::wit_bindgen_rt::async_support::FutureWriter::new(handle, vtable),
485-
::wit_bindgen_rt::async_support::FutureReader::new(handle, vtable),
489+
{async_support}::FutureWriter::new(handle, vtable),
490+
{async_support}::FutureReader::new(handle, vtable),
486491
)
487-
}
488-
}
492+
}}
493+
}}
489494
",
490-
);
495+
));
491496
}
492497

493498
if !self.stream_payloads.is_empty() {
494-
self.src.push_str(
499+
let async_support = self.async_support_path();
500+
self.src.push_str(&format!(
495501
"\
496-
pub mod wit_stream {
502+
pub mod wit_stream {{
497503
#![allow(dead_code, unused_variables, clippy::all)]
498504
499-
pub trait StreamPayload: Unpin + Sized + 'static {
500-
fn new() -> (u32, &'static ::wit_bindgen_rt::async_support::StreamVtable<Self>);
501-
}",
502-
);
505+
pub trait StreamPayload: Unpin + Sized + 'static {{
506+
fn new() -> (u32, &'static {async_support}::StreamVtable<Self>);
507+
}}"
508+
));
503509
for code in self.stream_payloads.values() {
504510
self.src.push_str(code);
505511
}
506512
self.src.push_str(
507-
"\
513+
&format!("\
508514
/// Creates a new Component Model `stream` with the specified payload type.
509-
pub fn new<T: StreamPayload>() -> (::wit_bindgen_rt::async_support::StreamWriter<T>, ::wit_bindgen_rt::async_support::StreamReader<T>) {
515+
pub fn new<T: StreamPayload>() -> ({async_support}::StreamWriter<T>, {async_support}::StreamReader<T>) {{
510516
let (handle, vtable) = T::new();
511-
::wit_bindgen_rt::async_support::with_entry(handle, |entry| match entry {
512-
::std::collections::hash_map::Entry::Vacant(entry) => {
513-
entry.insert(::wit_bindgen_rt::async_support::Handle::LocalOpen);
514-
}
517+
{async_support}::with_entry(handle, |entry| match entry {{
518+
::std::collections::hash_map::Entry::Vacant(entry) => {{
519+
entry.insert({async_support}::Handle::LocalOpen);
520+
}}
515521
::std::collections::hash_map::Entry::Occupied(_) => unreachable!(),
516-
});
522+
}});
517523
(
518-
::wit_bindgen_rt::async_support::StreamWriter::new(handle, vtable),
519-
::wit_bindgen_rt::async_support::StreamReader::new(handle, vtable),
524+
{async_support}::StreamWriter::new(handle, vtable),
525+
{async_support}::StreamReader::new(handle, vtable),
520526
)
521-
}
522-
}
523-
",
527+
}}
528+
}}
529+
"),
524530
);
525531
}
526532
}

0 commit comments

Comments
 (0)