Skip to content

Commit

Permalink
Move generated files to the generated/ sub-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jul 3, 2024
1 parent bc90478 commit f9494f2
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 72 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<name>::...`) in
* reference it like you would an external crate (via `generated::<name>::...`) in
your source code
* build using `cargo component build` and out pops your component!

Expand All @@ -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.

Expand Down Expand Up @@ -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;

Expand All @@ -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`.

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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\
Expand All @@ -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)]
Expand Down
3 changes: 3 additions & 0 deletions example/src/generated/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Generated by `cargo-component`. DO NOT EDIT!
mod component_bindings;
pub use component_bindings::*;
6 changes: 3 additions & 3 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -20,4 +20,4 @@ impl Guest for Component {
}
}

bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
8 changes: 4 additions & 4 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl NewCommand {
None => {
if self.is_command() {
Ok(r#"#[allow(warnings)]
mod bindings;
mod generated;
fn main() {
println!("Hello, world!");
Expand All @@ -361,9 +361,9 @@ fn main() {
.into())
} else {
Ok(r#"#[allow(warnings)]
mod bindings;
mod generated;
use bindings::Guest;
use generated::Guest;
struct Component;
Expand All @@ -374,7 +374,7 @@ impl Guest for Component {
}
}
bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
"#
.into())
}
Expand Down
12 changes: 6 additions & 6 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl UseTrie {

self.insert(
[
"bindings",
"generated",
"exports",
pkg.name.namespace.as_str(),
pkg.name.name.as_str(),
Expand All @@ -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<str> {
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")
Expand Down Expand Up @@ -499,7 +499,7 @@ impl<'a> UnimplementedFunction<'a> {
write!(
source,
"{name}",
name = trie.insert(["bindings"], &type_name)
name = trie.insert(["generated"], &type_name)
)
.unwrap();
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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}`",
Expand Down
6 changes: 3 additions & 3 deletions tests/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit f9494f2

Please sign in to comment.