Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/guest-rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use syn::spanned::Spanned;
use syn::{braced, token, LitStr, Token};
use wit_bindgen_core::wit_parser::{PackageId, Resolve, UnresolvedPackageGroup, WorldId};
use wit_bindgen_core::AsyncFilterSet;
use wit_bindgen_core::WorldGenerator;
use wit_bindgen_rust::{Opts, Ownership, WithOption};

#[proc_macro]
Expand Down
36 changes: 33 additions & 3 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use anyhow::{bail, Result};
use core::panic;
use heck::*;
use indexmap::{IndexMap, IndexSet};
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::{self, Write as _};
use std::mem;
use std::path::Path;
use std::str::FromStr;
use wit_bindgen_core::abi::{Bitcast, WasmType};
use wit_bindgen_core::{
Expand All @@ -26,7 +28,7 @@ struct InterfaceName {
}

#[derive(Default)]
struct RustWasm {
pub struct RustWasm {
types: Types,
src_preamble: Source,
src: Source,
Expand Down Expand Up @@ -277,15 +279,43 @@ pub struct Opts {
}

impl Opts {
pub fn build(self) -> Box<dyn WorldGenerator> {
pub fn build(self) -> RustWasm {
let mut r = RustWasm::new();
r.skip = self.skip.iter().cloned().collect();
r.opts = self;
Box::new(r)
r
}
}

const GENERATED_FILE_NAME: &str = "wit_bindgen_generated.rs";
impl RustWasm {
pub fn generate_to_out_dir(self, world: Option<&str>) -> Result<()> {
self.generate_to_out_dir_modify(world, |s| Cow::Borrowed(s))
}

pub fn generate_to_out_dir_modify(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding some doc comments for these methods?

mut self,
world: Option<&str>,
modify: impl FnOnce(&[u8]) -> Cow<[u8]>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessarily tightly integrated into the generation step, so could this be omitted and performed as part of the top-level build itself? For example the caller of generate_to_out_dir would read the output file, perform the transformation, and then write back out the contents.

) -> Result<()> {
let mut resolve = Resolve::default();
println!("cargo:rerun-if-changed=wit/");
let (pkg, _files) = resolve.push_path("wit")?;
let main_packages = vec![pkg];
let world = resolve.select_world(&main_packages, world)?;
let mut files = Files::default();
self.generate(&resolve, world, &mut files)?;
let out_dir = std::env::var("OUT_DIR").expect("cargo sets OUT_DIR");
let dst = Path::new(&out_dir).join(GENERATED_FILE_NAME);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a hardcoded name could all the files be written with the name from the generator itself?

let (_name, contents) = files
.iter()
.next()
.expect("exactly one file should be generated");
let contents = modify(contents);
std::fs::write(&dst, contents)?;
Ok(())
}

fn new() -> RustWasm {
RustWasm::default()
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn main() -> Result<()> {
#[cfg(feature = "cpp")]
Opt::Cpp { opts, args } => (opts.build(args.out_dir.as_ref()), args),
#[cfg(feature = "rust")]
Opt::Rust { opts, args } => (opts.build(), args),
Opt::Rust { opts, args } => (Box::new(opts.build()) as Box<dyn WorldGenerator>, args),
#[cfg(feature = "go")]
Opt::TinyGo { args: _ } => {
bail!("Go bindgen has been moved to a separate repository. Please visit https://github.com/bytecodealliance/go-modules for the new Go bindings generator `wit-bindgen-go`.")
Expand Down