Skip to content

Commit f9da328

Browse files
committed
Move generated files to the generated/ sub-directory
1 parent bc90478 commit f9da328

File tree

13 files changed

+87
-69
lines changed

13 files changed

+87
-69
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ That means being able to reference WebAssembly components via
113113
same way as Rust crate dependencies:
114114

115115
* add a dependency on a WebAssembly component to `Cargo.toml`
116-
* reference it like you would an external crate (via `bindings::<name>::...`) in
116+
* reference it like you would an external crate (via `generated::<name>::...`) in
117117
your source code
118118
* build using `cargo component build` and out pops your component!
119119

@@ -130,7 +130,7 @@ including Rust.
130130
bindings "inline" with the component's source code.
131131

132132
Unlike `wit-bindgen`, `cargo component` generates bindings directly into your
133-
project at `src/bindings.rs` so that bindings are generated based on the
133+
project at `src/generated/` so that bindings are generated based on the
134134
resolved dependencies from `Cargo.toml` rather than parsing a local definition
135135
of the component's interface.
136136

@@ -214,9 +214,9 @@ The implementation of the component will be in `src/lib.rs`:
214214

215215
```rust
216216
#[allow(warnings)]
217-
mod bindings;
217+
mod generated;
218218

219-
use bindings::Guest;
219+
use generated::Guest;
220220

221221
struct Component;
222222

@@ -227,10 +227,10 @@ impl Guest for Component {
227227
}
228228
}
229229

230-
bindings::export!(Component with_types_in bindings);
230+
generated::export!(Component with_types_in generated);
231231
```
232232

233-
The `bindings` module contains the the types and traits that correspond to the
233+
The `generated` module contains the the types and traits that correspond to the
234234
world targeted by the component; it is automatically generated by
235235
`cargo component`.
236236

File renamed without changes.

example/src/generated/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Generated by `cargo-component`. DO NOT EDIT!
2+
mod component_bindings;
3+
pub use component_bindings::*;

example/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[allow(warnings)]
2-
mod bindings;
2+
mod generated;
33

4-
use bindings::{
4+
use generated::{
55
example::component::{backend as origin, cache},
66
exports::example::component::backend::Guest,
77
};
@@ -20,4 +20,4 @@ impl Guest for Component {
2020
}
2121
}
2222

23-
bindings::export!(Component with_types_in bindings);
23+
generated::export!(Component with_types_in generated);

src/commands/new.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl NewCommand {
352352
None => {
353353
if self.is_command() {
354354
Ok(r#"#[allow(warnings)]
355-
mod bindings;
355+
mod generated;
356356
357357
fn main() {
358358
println!("Hello, world!");
@@ -361,9 +361,9 @@ fn main() {
361361
.into())
362362
} else {
363363
Ok(r#"#[allow(warnings)]
364-
mod bindings;
364+
mod generated;
365365
366-
use bindings::Guest;
366+
use generated::Guest;
367367
368368
struct Component;
369369
@@ -374,7 +374,7 @@ impl Guest for Component {
374374
}
375375
}
376376
377-
bindings::export!(Component with_types_in bindings);
377+
generated::export!(Component with_types_in generated);
378378
"#
379379
.into())
380380
}

src/generator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl UseTrie {
178178

179179
self.insert(
180180
[
181-
"bindings",
181+
"generated",
182182
"exports",
183183
pkg.name.namespace.as_str(),
184184
pkg.name.name.as_str(),
@@ -191,7 +191,7 @@ impl UseTrie {
191191
/// Inserts an export trait for the given world key.
192192
fn insert_export_trait(&mut self, resolve: &Resolve, key: &WorldKey) -> Cow<str> {
193193
match key {
194-
WorldKey::Name(name) => self.insert(["bindings", "exports", name.as_str()], "Guest"),
194+
WorldKey::Name(name) => self.insert(["generated", "exports", name.as_str()], "Guest"),
195195
WorldKey::Interface(id) => {
196196
let iface = &resolve.interfaces[*id];
197197
self.insert_interface_type(resolve, iface, "Guest")
@@ -499,7 +499,7 @@ impl<'a> UnimplementedFunction<'a> {
499499
write!(
500500
source,
501501
"{name}",
502-
name = trie.insert(["bindings"], &type_name)
502+
name = trie.insert(["generated"], &type_name)
503503
)
504504
.unwrap();
505505
}
@@ -712,7 +712,7 @@ impl<'a> ImplementationGenerator<'a> {
712712
writeln!(
713713
&mut source,
714714
"\nimpl {name} for {IMPLEMENTER} {{",
715-
name = trie.insert(["bindings"], "Guest")
715+
name = trie.insert(["generated"], "Guest")
716716
)?;
717717

718718
for (i, func) in self.functions.iter().enumerate() {
@@ -767,7 +767,7 @@ impl<'a> SourceGenerator<'a> {
767767
let impls = generator.generate(&mut trie)?;
768768

769769
let mut source = String::new();
770-
writeln!(&mut source, "#[allow(warnings)]\nmod bindings;")?;
770+
writeln!(&mut source, "#[allow(warnings)]\nmod generated;")?;
771771
writeln!(&mut source)?;
772772
write!(
773773
&mut source,
@@ -787,7 +787,7 @@ impl<'a> SourceGenerator<'a> {
787787

788788
writeln!(
789789
&mut source,
790-
"\nbindings::export!({IMPLEMENTER} with_types_in bindings);"
790+
"\ngenerated::export!({IMPLEMENTER} with_types_in generated);"
791791
)?;
792792

793793
if self.format {

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,10 @@ async fn generate_package_bindings(
822822
.manifest_path
823823
.parent()
824824
.unwrap()
825-
.join("src");
826-
let bindings_path = output_dir.join("bindings.rs");
825+
.join("src")
826+
.join("generated");
827+
let bindings_mod_path = output_dir.join("mod.rs");
828+
let bindings_path = output_dir.join("component_bindings.rs");
827829

828830
let last_modified_output = bindings_path
829831
.is_file()
@@ -859,6 +861,19 @@ async fn generate_package_bindings(
859861
)
860862
})?;
861863

864+
fs::write(
865+
&bindings_mod_path,
866+
r#"// Generated by `cargo-component`. DO NOT EDIT!
867+
mod component_bindings;
868+
pub use component_bindings::*;
869+
"#,
870+
)
871+
.with_context(|| {
872+
format!(
873+
"failed to write bindings file `{path}`",
874+
path = bindings_mod_path.display()
875+
)
876+
})?;
862877
fs::write(&bindings_path, bindings).with_context(|| {
863878
format!(
864879
"failed to write bindings file `{path}`",

tests/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ world generator {
3535
#![feature(test)]
3636
3737
#[allow(warnings)]
38-
mod bindings;
38+
mod generated;
3939
4040
extern crate test;
4141
42-
use bindings::{Guest, Size};
42+
use generated::{Guest, Size};
4343
use test::Bencher;
4444
4545
struct Component;
@@ -58,7 +58,7 @@ impl Guest for Component {
5858
}
5959
}
6060
61-
bindings::export!(Component with_types_in bindings);
61+
generated::export!(Component with_types_in generated);
6262
6363
#[bench]
6464
fn bench_recursive_fibonacci(b: &mut Bencher) {

tests/build.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ world example {
277277
fs::write(
278278
project.root().join("src/lib.rs"),
279279
"#[allow(warnings)]
280-
mod bindings;
281-
use bindings::exports::{foo::bar::baz::{Guest as Baz, Ty}, bar::baz::qux::Guest as Qux};
280+
mod generated;
281+
use generated::exports::{foo::bar::baz::{Guest as Baz, Ty}, bar::baz::qux::Guest as Qux};
282282
283283
struct Component;
284284
@@ -294,7 +294,7 @@ impl Qux for Component {
294294
}
295295
}
296296
297-
bindings::export!(Component with_types_in bindings);
297+
generated::export!(Component with_types_in generated);
298298
",
299299
)?;
300300

@@ -334,8 +334,8 @@ fn empty_world_with_dep_valid() -> Result<()> {
334334
project.root().join("src/lib.rs"),
335335
"
336336
#[allow(warnings)]
337-
mod bindings;
338-
use bindings::{Guest, Foo};
337+
mod generated;
338+
use generated::{Guest, Foo};
339339
struct Component;
340340
341341
impl Guest for Component {
@@ -344,7 +344,7 @@ fn empty_world_with_dep_valid() -> Result<()> {
344344
}
345345
}
346346
347-
bindings::export!(Component with_types_in bindings);
347+
generated::export!(Component with_types_in generated);
348348
",
349349
)?;
350350

@@ -372,11 +372,11 @@ fn empty_world_with_dep_valid() -> Result<()> {
372372
project.root().join("src/lib.rs"),
373373
"
374374
#[allow(warnings)]
375-
mod bindings;
375+
mod generated;
376376
377377
#[no_mangle]
378378
pub extern \"C\" fn foo() {
379-
bindings::foo_bar::hello();
379+
generated::foo_bar::hello();
380380
}
381381
",
382382
)?;
@@ -413,21 +413,21 @@ fn it_builds_with_resources() -> Result<()> {
413413
project.root().join("src/lib.rs"),
414414
r#"
415415
#[allow(warnings)]
416-
mod bindings;
416+
mod generated;
417417
418418
use std::cell::Cell;
419419
420420
struct Component;
421421
422-
impl bindings::exports::baz::Guest for Component {
422+
impl generated::exports::baz::Guest for Component {
423423
type KeyedInteger = KeyedInteger;
424424
}
425425
426-
bindings::export!(Component with_types_in bindings);
426+
generated::export!(Component with_types_in generated);
427427
428428
pub struct KeyedInteger(Cell<u32>);
429429
430-
impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger {
430+
impl generated::exports::baz::GuestKeyedInteger for KeyedInteger {
431431
fn new(x: u32) -> Self {
432432
Self(Cell::new(x))
433433
}
@@ -486,21 +486,21 @@ fn it_builds_resources_with_specified_ownership_model() -> Result<()> {
486486
project.root().join("src/lib.rs"),
487487
r#"
488488
#[allow(warnings)]
489-
mod bindings;
489+
mod generated;
490490
491491
use std::cell::Cell;
492492
493493
struct Component;
494494
495-
impl bindings::exports::baz::Guest for Component {
495+
impl generated::exports::baz::Guest for Component {
496496
type KeyedInteger = KeyedInteger;
497497
}
498498
499-
bindings::export!(Component with_types_in bindings);
499+
generated::export!(Component with_types_in generated);
500500
501501
pub struct KeyedInteger(Cell<u32>);
502502
503-
impl bindings::exports::baz::GuestKeyedInteger for KeyedInteger {
503+
impl generated::exports::baz::GuestKeyedInteger for KeyedInteger {
504504
fn new(x: u32) -> Self {
505505
Self(Cell::new(x))
506506
}
@@ -565,9 +565,9 @@ world random-generator {
565565
comp1.root().join("src/lib.rs"),
566566
r#"
567567
#[allow(warnings)]
568-
mod bindings;
568+
mod generated;
569569
570-
use bindings::{Guest, Seed, exports::my::comp1::other};
570+
use generated::{Guest, Seed, exports::my::comp1::other};
571571
572572
struct Component;
573573
@@ -583,7 +583,7 @@ impl other::Guest for Component {
583583
}
584584
}
585585
586-
bindings::export!(Component with_types_in bindings);
586+
generated::export!(Component with_types_in generated);
587587
"#,
588588
)?;
589589

@@ -618,9 +618,9 @@ world random-generator {
618618
comp2.root().join("src/lib.rs"),
619619
r#"
620620
#[allow(warnings)]
621-
mod bindings;
621+
mod generated;
622622
623-
use bindings::{Guest, my_comp1, my};
623+
use generated::{Guest, my_comp1, my};
624624
625625
struct Component;
626626
@@ -630,7 +630,7 @@ impl Guest for Component {
630630
}
631631
}
632632
633-
bindings::export!(Component with_types_in bindings);
633+
generated::export!(Component with_types_in generated);
634634
"#,
635635
)?;
636636

@@ -741,9 +741,9 @@ world foo-world {
741741
project.root().join("src/lib.rs"),
742742
r#"
743743
#[allow(warnings)]
744-
mod bindings;
745-
use bindings::Guest;
746-
use bindings::my::derive::foo::Bar;
744+
mod generated;
745+
use generated::Guest;
746+
use generated::my::derive::foo::Bar;
747747
748748
struct Component;
749749
@@ -756,7 +756,7 @@ impl Guest for Component {
756756
}
757757
}
758758
759-
bindings::export!(Component with_types_in bindings);
759+
generated::export!(Component with_types_in generated);
760760
"#,
761761
)?;
762762

@@ -796,15 +796,15 @@ fn it_builds_with_versioned_wit() -> Result<()> {
796796
project.root().join("src/lib.rs"),
797797
r#"
798798
#[allow(warnings)]
799-
mod bindings;
799+
mod generated;
800800
801801
struct Component;
802802
803-
impl bindings::exports::foo::bar::foo::Guest for Component {
803+
impl generated::exports::foo::bar::foo::Guest for Component {
804804
fn f() {}
805805
}
806806
807-
bindings::export!(Component with_types_in bindings);
807+
generated::export!(Component with_types_in generated);
808808
"#,
809809
)?;
810810

0 commit comments

Comments
 (0)