Skip to content

Commit

Permalink
move asyncify.rs to its own crate
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Jan 26, 2024
1 parent 618b923 commit 84e2fff
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 253 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ wit-bindgen-csharp = { path = 'crates/csharp', version = '0.16.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.16.0' }
wit-bindgen = { path = 'crates/guest-rust', version = '0.16.0', default-features = false }

isyswasfa-transform = { path = "../transform" }

[[bin]]
name = "wit-bindgen"

Expand All @@ -57,6 +59,7 @@ wit-bindgen-go = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-csharp = { workspace = true, features = ['clap'], optional = true }
wit-component = { workspace = true }
wasm-encoder = { workspace = true }
isyswasfa-transform = { workspace = true }

[features]
default = [
Expand Down
220 changes: 0 additions & 220 deletions crates/core/src/asyncify.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ mod ns;
pub use ns::Ns;
pub mod source;
pub use source::{Files, Source};
pub mod asyncify;
pub use asyncify::asyncify;

#[derive(Default, Copy, Clone, PartialEq, Eq, Debug)]
pub enum Direction {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ wit-bindgen-core = { workspace = true }
wit-bindgen-rust = { workspace = true }
wit-component = { workspace = true }
anyhow = { workspace = true }
isyswasfa-transform = { workspace = true }
22 changes: 11 additions & 11 deletions crates/rust-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ impl Parse for Config {
.collect()
}
Opt::With(with) => opts.with.extend(with),
Opt::Asyncify(suffix) => {
if opts.asyncify.is_some() {
Opt::Isyswasfa(suffix) => {
if opts.isyswasfa.is_some() {
return Err(Error::new(
suffix.span(),
"cannot specify second asyncify suffix",
"cannot specify second isyswasfa suffix",
));
}
opts.asyncify = Some(suffix.value())
opts.isyswasfa = Some(suffix.value())
}
}
}
Expand All @@ -116,8 +116,8 @@ impl Parse for Config {
let world = resolve
.select_world(pkg, world.as_deref())
.map_err(|e| Error::new(call_site, format!("{e:?}")))?;
let (resolve, world) = if let Some(suffix) = opts.asyncify.as_deref() {
wit_bindgen_core::asyncify(&resolve, world, suffix)
let (resolve, world) = if let Some(suffix) = opts.isyswasfa.as_deref() {
isyswasfa_transform::transform(&resolve, world, Some(suffix))
} else {
(resolve, world)
};
Expand Down Expand Up @@ -200,7 +200,7 @@ mod kw {
syn::custom_keyword!(export_prefix);
syn::custom_keyword!(additional_derives);
syn::custom_keyword!(with);
syn::custom_keyword!(asyncify);
syn::custom_keyword!(isyswasfa);
}

#[derive(Clone)]
Expand Down Expand Up @@ -261,7 +261,7 @@ enum Opt {
// Parse as paths so we can take the concrete types/macro names rather than raw strings
AdditionalDerives(Vec<syn::Path>),
With(HashMap<String, String>),
Asyncify(syn::LitStr),
Isyswasfa(syn::LitStr),
}

impl Parse for Opt {
Expand Down Expand Up @@ -352,10 +352,10 @@ impl Parse for Opt {
input.parse::<kw::export_prefix>()?;
input.parse::<Token![:]>()?;
Ok(Opt::ExportPrefix(input.parse()?))
} else if l.peek(kw::asyncify) {
input.parse::<kw::asyncify>()?;
} else if l.peek(kw::isyswasfa) {
input.parse::<kw::isyswasfa>()?;
input.parse::<Token![:]>()?;
Ok(Opt::Asyncify(input.parse()?))
Ok(Opt::Isyswasfa(input.parse()?))
} else if l.peek(kw::additional_derives) {
input.parse::<kw::additional_derives>()?;
input.parse::<Token![:]>()?;
Expand Down
26 changes: 17 additions & 9 deletions crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl InterfaceGenerator<'_> {
sig.self_is_first_param = true;
}
self.print_signature(func, TypeMode::Owned, &sig);
if let Some(suffix) = self.gen.opts.asyncify.clone() {
if let Some(suffix) = self.gen.opts.isyswasfa.clone() {
if func.name == format!("isyswasfa-poll{suffix}") {
self.src.push_str("{ isyswasfa_guest::poll(input) }\n");
} else if let Some(prefix) = func.name.strip_suffix("-isyswasfa") {
Expand Down Expand Up @@ -191,14 +191,22 @@ impl InterfaceGenerator<'_> {
// there's only one implementation of this trait and it must be
// pre-configured.
for (export_key, (trait_name, local_impl_name, methods)) in traits {
let impl_name = self.gen.lookup_export(&export_key)?;
let path_to_root = self.path_to_root();
uwriteln!(
self.src,
"use {path_to_root}{impl_name} as {local_impl_name};"
);
if self.gen.opts.isyswasfa.is_some()
&& matches!(export_key, ExportKey::World)
&& methods.len() == 1
{
uwriteln!(self.src, "struct {local_impl_name};");
uwriteln!(self.src, "impl {trait_name} for {local_impl_name} {{}}");
} else {
let impl_name = self.gen.lookup_export(&export_key)?;
let path_to_root = self.path_to_root();
uwriteln!(
self.src,
"use {path_to_root}{impl_name} as {local_impl_name};"
);
}

if self.gen.opts.asyncify.is_some() {
if self.gen.opts.isyswasfa.is_some() {
self.src.push_str("#[async_trait(?Self)]\n");
}
uwriteln!(self.src, "pub trait {trait_name} {{");
Expand Down Expand Up @@ -363,7 +371,7 @@ impl InterfaceGenerator<'_> {
self.src.push_str("}\n");
self.src.push_str("}\n");

if self.gen.opts.asyncify.is_some() {
if self.gen.opts.isyswasfa.is_some() {
if let Some(prefix) = func.name.strip_suffix("-isyswasfa") {
sig.async_ = true;
self.src.push_str("#[allow(unused_unsafe, clippy::all)]\n");
Expand Down
Loading

0 comments on commit 84e2fff

Please sign in to comment.