diff --git a/README.md b/README.md index e64e15fe..d878afa5 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ That means being able to reference WebAssembly components via same way as Rust crate dependencies: * add a dependency on a WebAssembly component to `Cargo.toml` -* reference it like you would an external crate (via `bindings::::...`) in +* reference it like you would an external crate (via `generated::::...`) in your source code * build using `cargo component build` and out pops your component! @@ -130,7 +130,7 @@ including Rust. bindings "inline" with the component's source code. Unlike `wit-bindgen`, `cargo component` generates bindings directly into your -project at `src/bindings.rs` so that bindings are generated based on the +project at `src/generated/` so that bindings are generated based on the resolved dependencies from `Cargo.toml` rather than parsing a local definition of the component's interface. @@ -214,9 +214,9 @@ The implementation of the component will be in `src/lib.rs`: ```rust #[allow(warnings)] -mod bindings; +mod generated; -use bindings::Guest; +use generated::Guest; struct Component; @@ -227,10 +227,10 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); ``` -The `bindings` module contains the the types and traits that correspond to the +The `generated` module contains the the types and traits that correspond to the world targeted by the component; it is automatically generated by `cargo component`. diff --git a/example/src/bindings.rs b/example/src/generated/component_bindings.rs similarity index 98% rename from example/src/bindings.rs rename to example/src/generated/component_bindings.rs index c18c0d01..33478e34 100644 --- a/example/src/bindings.rs +++ b/example/src/generated/component_bindings.rs @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.25.0. DO NOT EDIT! // Options used: #[allow(dead_code)] pub mod example { @@ -249,7 +249,7 @@ macro_rules! __export_example_impl { pub(crate) use __export_example_impl as export; #[cfg(target_arch = "wasm32")] -#[link_section = "component-type:wit-bindgen:0.24.0:example:encoded world"] +#[link_section = "component-type:wit-bindgen:0.25.0:example:encoded world"] #[doc(hidden)] pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 354] = *b"\ \0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xe4\x01\x01A\x02\x01\ @@ -259,7 +259,7 @@ e\x05\0\x01B\x03\x01p}\x01@\x01\x03urls\0\0\x04\0\x05fetch\x01\x01\x03\x01\x19ex ample:component/backend\x05\x01\x01B\x03\x01p}\x01@\x01\x03urls\0\0\x04\0\x05fet\ ch\x01\x01\x04\x01\x19example:component/backend\x05\x02\x04\x01\x19example:compo\ nent/example\x04\0\x0b\x0d\x01\0\x07example\x03\0\0\0G\x09producers\x01\x0cproce\ -ssed-by\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x060.24.0"; +ssed-by\x02\x0dwit-component\x070.208.1\x10wit-bindgen-rust\x060.25.0"; #[inline(never)] #[doc(hidden)] diff --git a/example/src/generated/mod.rs b/example/src/generated/mod.rs new file mode 100644 index 00000000..a4f8f784 --- /dev/null +++ b/example/src/generated/mod.rs @@ -0,0 +1,3 @@ +// Generated by `cargo-component`. DO NOT EDIT! +mod component_bindings; +pub use component_bindings::*; diff --git a/example/src/lib.rs b/example/src/lib.rs index 7d3ebd84..b59fd0c7 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -1,7 +1,7 @@ #[allow(warnings)] -mod bindings; +mod generated; -use bindings::{ +use generated::{ example::component::{backend as origin, cache}, exports::example::component::backend::Guest, }; @@ -20,4 +20,4 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); diff --git a/src/commands/new.rs b/src/commands/new.rs index 0578d9ca..b059b14b 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -352,7 +352,7 @@ impl NewCommand { None => { if self.is_command() { Ok(r#"#[allow(warnings)] -mod bindings; +mod generated; fn main() { println!("Hello, world!"); @@ -361,9 +361,9 @@ fn main() { .into()) } else { Ok(r#"#[allow(warnings)] -mod bindings; +mod generated; -use bindings::Guest; +use generated::Guest; struct Component; @@ -374,7 +374,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "# .into()) } diff --git a/src/generator.rs b/src/generator.rs index 3391b5eb..fcf29987 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -178,7 +178,7 @@ impl UseTrie { self.insert( [ - "bindings", + "generated", "exports", pkg.name.namespace.as_str(), pkg.name.name.as_str(), @@ -191,7 +191,7 @@ impl UseTrie { /// Inserts an export trait for the given world key. fn insert_export_trait(&mut self, resolve: &Resolve, key: &WorldKey) -> Cow { match key { - WorldKey::Name(name) => self.insert(["bindings", "exports", name.as_str()], "Guest"), + WorldKey::Name(name) => self.insert(["generated", "exports", name.as_str()], "Guest"), WorldKey::Interface(id) => { let iface = &resolve.interfaces[*id]; self.insert_interface_type(resolve, iface, "Guest") @@ -499,7 +499,7 @@ impl<'a> UnimplementedFunction<'a> { write!( source, "{name}", - name = trie.insert(["bindings"], &type_name) + name = trie.insert(["generated"], &type_name) ) .unwrap(); } @@ -712,7 +712,7 @@ impl<'a> ImplementationGenerator<'a> { writeln!( &mut source, "\nimpl {name} for {IMPLEMENTER} {{", - name = trie.insert(["bindings"], "Guest") + name = trie.insert(["generated"], "Guest") )?; for (i, func) in self.functions.iter().enumerate() { @@ -767,7 +767,7 @@ impl<'a> SourceGenerator<'a> { let impls = generator.generate(&mut trie)?; let mut source = String::new(); - writeln!(&mut source, "#[allow(warnings)]\nmod bindings;")?; + writeln!(&mut source, "#[allow(warnings)]\nmod generated;")?; writeln!(&mut source)?; write!( &mut source, @@ -787,7 +787,7 @@ impl<'a> SourceGenerator<'a> { writeln!( &mut source, - "\nbindings::export!({IMPLEMENTER} with_types_in bindings);" + "\ngenerated::export!({IMPLEMENTER} with_types_in generated);" )?; if self.format { diff --git a/src/lib.rs b/src/lib.rs index 5a4b701e..2f14bfb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -822,8 +822,10 @@ async fn generate_package_bindings( .manifest_path .parent() .unwrap() - .join("src"); - let bindings_path = output_dir.join("bindings.rs"); + .join("src") + .join("generated"); + let bindings_mod_path = output_dir.join("mod.rs"); + let bindings_path = output_dir.join("component_bindings.rs"); let last_modified_output = bindings_path .is_file() @@ -859,6 +861,19 @@ async fn generate_package_bindings( ) })?; + fs::write( + &bindings_mod_path, + r#"// Generated by `cargo-component`. DO NOT EDIT! +mod component_bindings; +pub use component_bindings::*; +"#, + ) + .with_context(|| { + format!( + "failed to write bindings file `{path}`", + path = bindings_mod_path.display() + ) + })?; fs::write(&bindings_path, bindings).with_context(|| { format!( "failed to write bindings file `{path}`", diff --git a/tests/bench.rs b/tests/bench.rs index 191186a2..a69f49da 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -35,11 +35,11 @@ world generator { #![feature(test)] #[allow(warnings)] -mod bindings; +mod generated; extern crate test; -use bindings::{Guest, Size}; +use generated::{Guest, Size}; use test::Bencher; struct Component; @@ -58,7 +58,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); #[bench] fn bench_recursive_fibonacci(b: &mut Bencher) { diff --git a/tests/build.rs b/tests/build.rs index 208c6cc0..93e6c5d3 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -277,8 +277,8 @@ world example { fs::write( project.root().join("src/lib.rs"), "#[allow(warnings)] -mod bindings; -use bindings::exports::{foo::bar::baz::{Guest as Baz, Ty}, bar::baz::qux::Guest as Qux}; +mod generated; +use generated::exports::{foo::bar::baz::{Guest as Baz, Ty}, bar::baz::qux::Guest as Qux}; struct Component; @@ -294,7 +294,7 @@ impl Qux for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); ", )?; @@ -334,8 +334,8 @@ fn empty_world_with_dep_valid() -> Result<()> { project.root().join("src/lib.rs"), " #[allow(warnings)] - mod bindings; - use bindings::{Guest, Foo}; + mod generated; + use generated::{Guest, Foo}; struct Component; impl Guest for Component { @@ -344,7 +344,7 @@ fn empty_world_with_dep_valid() -> Result<()> { } } - bindings::export!(Component with_types_in bindings); + generated::export!(Component with_types_in generated); ", )?; @@ -372,11 +372,11 @@ fn empty_world_with_dep_valid() -> Result<()> { project.root().join("src/lib.rs"), " #[allow(warnings)] - mod bindings; + mod generated; #[no_mangle] pub extern \"C\" fn foo() { - bindings::foo_bar::hello(); + generated::foo_bar::hello(); } ", )?; @@ -413,21 +413,21 @@ fn it_builds_with_resources() -> Result<()> { project.root().join("src/lib.rs"), r#" #[allow(warnings)] - mod bindings; + mod generated; use std::cell::Cell; struct Component; - impl bindings::exports::baz::Guest for Component { + impl generated::exports::baz::Guest for Component { type KeyedInteger = KeyedInteger; } - bindings::export!(Component with_types_in bindings); + generated::export!(Component with_types_in generated); pub struct KeyedInteger(Cell); - impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger { + impl generated::exports::baz::GuestKeyedInteger for KeyedInteger { fn new(x: u32) -> Self { Self(Cell::new(x)) } @@ -486,21 +486,21 @@ fn it_builds_resources_with_specified_ownership_model() -> Result<()> { project.root().join("src/lib.rs"), r#" #[allow(warnings)] - mod bindings; + mod generated; use std::cell::Cell; struct Component; - impl bindings::exports::baz::Guest for Component { + impl generated::exports::baz::Guest for Component { type KeyedInteger = KeyedInteger; } - bindings::export!(Component with_types_in bindings); + generated::export!(Component with_types_in generated); pub struct KeyedInteger(Cell); - impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger { + impl generated::exports::baz::GuestKeyedInteger for KeyedInteger { fn new(x: u32) -> Self { Self(Cell::new(x)) } @@ -565,9 +565,9 @@ world random-generator { comp1.root().join("src/lib.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; -use bindings::{Guest, Seed, exports::my::comp1::other}; +use generated::{Guest, Seed, exports::my::comp1::other}; struct Component; @@ -583,7 +583,7 @@ impl other::Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#, )?; @@ -618,9 +618,9 @@ world random-generator { comp2.root().join("src/lib.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; -use bindings::{Guest, my_comp1, my}; +use generated::{Guest, my_comp1, my}; struct Component; @@ -630,7 +630,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#, )?; @@ -741,9 +741,9 @@ world foo-world { project.root().join("src/lib.rs"), r#" #[allow(warnings)] -mod bindings; -use bindings::Guest; -use bindings::my::derive::foo::Bar; +mod generated; +use generated::Guest; +use generated::my::derive::foo::Bar; struct Component; @@ -756,7 +756,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#, )?; @@ -796,15 +796,15 @@ fn it_builds_with_versioned_wit() -> Result<()> { project.root().join("src/lib.rs"), r#" #[allow(warnings)] - mod bindings; + mod generated; struct Component; - impl bindings::exports::foo::bar::foo::Guest for Component { + impl generated::exports::foo::bar::foo::Guest for Component { fn f() {} } - bindings::export!(Component with_types_in bindings); + generated::export!(Component with_types_in generated); "#, )?; diff --git a/tests/publish.rs b/tests/publish.rs index 0513dbb9..aab6e77d 100644 --- a/tests/publish.rs +++ b/tests/publish.rs @@ -45,7 +45,7 @@ world foo { // Ensure there's a using declaration in the generated source let source = fs::read_to_string(project.root().join("src/lib.rs"))?; - assert!(source.contains("use bindings::Guest;")); + assert!(source.contains("use generated::Guest;")); project .cargo_component("publish --init") @@ -141,16 +141,16 @@ world foo { let source = r#" #[allow(warnings)] -mod bindings; -use bindings::Guest; +mod generated; +use generated::Guest; struct Component; impl Guest for Component { fn bar() -> String { - bindings::test_foo::bar() + generated::test_foo::bar() } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#; fs::write(project.root().join("src/lib.rs"), source)?; diff --git a/tests/run.rs b/tests/run.rs index 30d43eeb..c26e41e7 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -15,7 +15,7 @@ fn it_runs_with_command_component() -> Result<()> { project.root().join("src/main.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; fn main() { if std::env::args().any(|v| v == "--verbose") { @@ -75,9 +75,9 @@ world generator { project.root().join("src/lib.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; -use bindings::exports::wasi::cli::run::Guest; +use generated::exports::wasi::cli::run::Guest; struct Component; @@ -92,7 +92,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#, )?; diff --git a/tests/test.rs b/tests/test.rs index d18b0dc8..ee4e06f0 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -50,9 +50,9 @@ world generator { project.root().join("src/main.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; -use bindings::{Seed}; +use generated::{Seed}; fn rand(seed: Seed) -> u32 { seed.value + 1 @@ -104,9 +104,9 @@ world generator { project.root().join("src/lib.rs"), r#" #[allow(warnings)] -mod bindings; +mod generated; -use bindings::{Seed}; +use generated::{Seed}; fn rand(seed: Seed) -> u32 { seed.value + 1 diff --git a/tests/update.rs b/tests/update.rs index 4d7a95f1..920af619 100644 --- a/tests/update.rs +++ b/tests/update.rs @@ -171,8 +171,8 @@ world foo { let source = r#" #[allow(warnings)] -mod bindings; -use bindings::{baz, Guest}; +mod generated; +use generated::{baz, Guest}; struct Component; impl Guest for Component { fn bar() -> String { @@ -180,7 +180,7 @@ impl Guest for Component { } } -bindings::export!(Component with_types_in bindings); +generated::export!(Component with_types_in generated); "#; fs::write(project.root().join("src/lib.rs"), source)?;