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

Change the name of wit-bindgen's host implementation traits. #5890

Merged
Merged
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
6 changes: 6 additions & 0 deletions crates/component-macro/tests/codegen/simple-wasi.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ interface wasi-filesystem {
stat: func() -> result<descriptor-stat, errno>
}

interface wall-clock {
record wall-clock {
}
}

default world wasi {
import wasi-filesystem: self.wasi-filesystem
import wall-clock: self.wall-clock
}
27 changes: 13 additions & 14 deletions crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rust::{to_rust_ident, RustGenerator, TypeMode};
use crate::rust::{to_rust_ident, to_rust_upper_camel_case, RustGenerator, TypeMode};
use crate::types::{TypeInfo, Types};
use heck::*;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Wasmtime {
gen.generate_trappable_error_types(TypeOwner::Interface(*id));
let iface = &resolve.interfaces[*id];

let camel = name.to_upper_camel_case();
let camel = to_rust_upper_camel_case(name);
uwriteln!(gen.src, "pub struct {camel} {{");
for (_, func) in iface.functions.iter() {
uwriteln!(
Expand Down Expand Up @@ -237,7 +237,7 @@ impl Wasmtime {
}

fn finish(&mut self, resolve: &Resolve, world: WorldId) -> String {
let camel = resolve.worlds[world].name.to_upper_camel_case();
let camel = to_rust_upper_camel_case(&resolve.worlds[world].name);
uwriteln!(self.src, "pub struct {camel} {{");
for (name, (ty, _)) in self.exports.fields.iter() {
uwriteln!(self.src, "{name}: {ty},");
Expand Down Expand Up @@ -363,7 +363,7 @@ impl Wasmtime {
return;
}

let world_camel = resolve.worlds[world].name.to_upper_camel_case();
let world_camel = to_rust_upper_camel_case(&resolve.worlds[world].name);
if self.opts.async_ {
uwriteln!(self.src, "#[wasmtime::component::__internal::async_trait]")
}
Expand Down Expand Up @@ -401,11 +401,11 @@ impl Wasmtime {
where U: \
"
);
let world_camel = resolve.worlds[world].name.to_upper_camel_case();
let world_camel = to_rust_upper_camel_case(&resolve.worlds[world].name);
let world_trait = format!("{world_camel}Imports");
for (i, name) in interfaces
.iter()
.map(|n| format!("{n}::{}", n.to_upper_camel_case()))
.map(|n| format!("{n}::Host"))
.chain(if functions.is_empty() {
None
} else {
Expand Down Expand Up @@ -591,7 +591,7 @@ impl<'a> InterfaceGenerator<'a> {
self.rustdoc(docs);
self.src.push_str("wasmtime::component::flags!(\n");
self.src
.push_str(&format!("{} {{\n", name.to_upper_camel_case()));
.push_str(&format!("{} {{\n", to_rust_upper_camel_case(name)));
for flag in flags.flags.iter() {
// TODO wasmtime-component-macro doesnt support docs for flags rn
uwrite!(
Expand Down Expand Up @@ -657,7 +657,7 @@ impl<'a> InterfaceGenerator<'a> {
let info = self.info(id);

for (name, mode) in self.modes_of(id) {
let name = name.to_upper_camel_case();
let name = to_rust_upper_camel_case(&name);

self.rustdoc(docs);
let lt = self.lifetime_for(&info, mode);
Expand Down Expand Up @@ -783,14 +783,14 @@ impl<'a> InterfaceGenerator<'a> {
fn type_enum(&mut self, id: TypeId, name: &str, enum_: &Enum, docs: &Docs) {
let info = self.info(id);

let name = name.to_upper_camel_case();
let name = to_rust_upper_camel_case(name);
self.rustdoc(docs);
self.push_str("#[derive(wasmtime::component::ComponentType)]\n");
self.push_str("#[derive(wasmtime::component::Lift)]\n");
self.push_str("#[derive(wasmtime::component::Lower)]\n");
self.push_str("#[component(enum)]\n");
self.push_str("#[derive(Clone, Copy, PartialEq, Eq)]\n");
self.push_str(&format!("pub enum {} {{\n", name.to_upper_camel_case()));
self.push_str(&format!("pub enum {} {{\n", name));
for case in enum_.cases.iter() {
self.rustdoc(&case.docs);
self.push_str(&format!("#[component(name = \"{}\")]", case.name));
Expand Down Expand Up @@ -953,24 +953,23 @@ impl<'a> InterfaceGenerator<'a> {

fn generate_add_to_linker(&mut self, id: InterfaceId, name: &str) {
let iface = &self.resolve.interfaces[id];
let camel = name.to_upper_camel_case();
let owner = TypeOwner::Interface(id);

if self.gen.opts.async_ {
uwriteln!(self.src, "#[wasmtime::component::__internal::async_trait]")
}
// Generate the `pub trait` which represents the host functionality for
// this import.
uwriteln!(self.src, "pub trait {camel}: Sized {{");
uwriteln!(self.src, "pub trait Host: Sized {{");
for (_, func) in iface.functions.iter() {
self.generate_function_trait_sig(owner, func);
}
uwriteln!(self.src, "}}");

let where_clause = if self.gen.opts.async_ {
format!("T: Send, U: {camel} + Send")
format!("T: Send, U: Host + Send")
} else {
format!("U: {camel}")
format!("U: Host")
};
uwriteln!(
self.src,
Expand Down
11 changes: 11 additions & 0 deletions crates/wit-bindgen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ pub trait RustGenerator<'a> {
}
}

/// Translate `name` to a Rust `snake_case` identifier.
pub fn to_rust_ident(name: &str) -> String {
match name {
// Escape Rust keywords.
Expand Down Expand Up @@ -425,3 +426,13 @@ pub fn to_rust_ident(name: &str) -> String {
s => s.to_snake_case(),
}
}

/// Translate `name` to a Rust `UpperCamelCase` identifier.
pub fn to_rust_upper_camel_case(name: &str) -> String {
match name {
// We use `Host` as the name of the trait for host implementations
// to fill in, so rename it if "Host" is used as a regular identifier.
"host" => "Host_".into(),
s => s.to_upper_camel_case(),
}
}
2 changes: 1 addition & 1 deletion tests/all/component_model/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod one_import {
hit: bool,
}

impl foo::Foo for MyImports {
impl foo::Host for MyImports {
fn foo(&mut self) -> Result<()> {
self.hit = true;
Ok(())
Expand Down
10 changes: 5 additions & 5 deletions tests/all/component_model/bindgen/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod empty_error {
#[derive(Default)]
struct MyImports {}

impl imports::Imports for MyImports {
impl imports::Host for MyImports {
fn empty_error(&mut self, a: f64) -> Result<Result<f64, ()>, Error> {
if a == 0.0 {
Ok(Ok(a))
Expand Down Expand Up @@ -171,7 +171,7 @@ mod string_error {
#[derive(Default)]
struct MyImports {}

impl imports::Imports for MyImports {
impl imports::Host for MyImports {
fn string_error(&mut self, a: f64) -> Result<Result<f64, String>, Error> {
if a == 0.0 {
Ok(Ok(a))
Expand Down Expand Up @@ -313,7 +313,7 @@ mod enum_error {
#[derive(Default)]
struct MyImports {}

impl imports::Imports for MyImports {
impl imports::Host for MyImports {
fn enum_error(&mut self, a: f64) -> Result<f64, imports::TrappableE1> {
if a == 0.0 {
Ok(a)
Expand Down Expand Up @@ -440,7 +440,7 @@ mod record_error {
#[derive(Default)]
struct MyImports {}

impl imports::Imports for MyImports {
impl imports::Host for MyImports {
fn record_error(&mut self, a: f64) -> Result<f64, imports::TrappableE2> {
if a == 0.0 {
Ok(a)
Expand Down Expand Up @@ -576,7 +576,7 @@ mod variant_error {
#[derive(Default)]
struct MyImports {}

impl imports::Imports for MyImports {
impl imports::Host for MyImports {
fn variant_error(&mut self, a: f64) -> Result<f64, imports::TrappableE3> {
if a == 0.0 {
Ok(a)
Expand Down