Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generated files to the generated/ sub-directory #316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
File renamed without changes.
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
58 changes: 29 additions & 29 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -294,7 +294,7 @@ impl Qux for Component {
}
}

bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
",
)?;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
",
)?;

Expand Down Expand Up @@ -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();
}
",
)?;
Expand Down Expand Up @@ -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<u32>);

impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger {
impl generated::exports::baz::GuestKeyedInteger for KeyedInteger {
fn new(x: u32) -> Self {
Self(Cell::new(x))
}
Expand Down Expand Up @@ -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<u32>);

impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger {
impl generated::exports::baz::GuestKeyedInteger for KeyedInteger {
fn new(x: u32) -> Self {
Self(Cell::new(x))
}
Expand Down Expand Up @@ -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;

Expand All @@ -583,7 +583,7 @@ impl other::Guest for Component {
}
}

bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
"#,
)?;

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

Expand All @@ -630,7 +630,7 @@ impl Guest for Component {
}
}

bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
"#,
)?;

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

Expand All @@ -756,7 +756,7 @@ impl Guest for Component {
}
}

bindings::export!(Component with_types_in bindings);
generated::export!(Component with_types_in generated);
"#,
)?;

Expand Down Expand Up @@ -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);
"#,
)?;

Expand Down
Loading