Skip to content
Draft
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ wit-bindgen = { path = '../guest-rust', features = ['async'] }
test-helpers = { path = '../test-helpers' }
# For use with the custom attributes test
serde_json = "1"
bytes = "*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind putting a version requirement here instead of just using *?


[features]
serde = ['dep:serde', 'wit-bindgen-core/serde']
Expand Down
10 changes: 7 additions & 3 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,15 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let val = format!("vec{}", tmp);
let ptr = format!("ptr{}", tmp);
let len = format!("len{}", tmp);
let vec = self.r#gen.path_to_vec();
if realloc.is_none() {
self.push_str(&format!("let {} = {};\n", val, operands[0]));
} else {
let op0 = operands.pop().unwrap();
self.push_str(&format!("let {} = ({}).into_boxed_slice();\n", val, op0));
self.push_str(&format!(
"let {} = <_ as Into<{vec}<_>>>::into({}).into_boxed_slice();\n",
val, op0
));
}
self.push_str(&format!("let {} = {}.as_ptr().cast::<u8>();\n", ptr, val));
self.push_str(&format!("let {} = {}.len();\n", len, val));
Expand All @@ -691,7 +695,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str(&format!("let {} = {};\n", len, operands[1]));
let vec = self.r#gen.path_to_vec();
let result = format!(
"{vec}::from_raw_parts({}.cast(), {1}, {1})",
"<_ as From<{vec}<_>>>::from({vec}::from_raw_parts({}.cast(), {1}, {1}))",
operands[0], len
);
results.push(result);
Expand Down Expand Up @@ -810,7 +814,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "let e{tmp} = {body};");
uwriteln!(self.src, "{result}.push(e{tmp});");
uwriteln!(self.src, "}}");
results.push(result);
results.push(format!("<_ as From<Vec<_>>>::from({result})"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From CI I believe this is giong to want to use {vec} instead of Vec

let dealloc = self.r#gen.path_to_cabi_dealloc();
self.push_str(&format!(
"{dealloc}({base}, {len} * {size}, {align});\n",
Expand Down
3 changes: 2 additions & 1 deletion crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ impl RustWasm {
}

self.src.push_str("mod _rt {\n");
self.src.push_str("#![allow(dead_code, clippy::all)]\n");
self.src
.push_str("#![allow(dead_code, unused_imports, clippy::all)]\n");
let mut emitted = IndexSet::new();
while !self.rt_module.is_empty() {
for item in mem::take(&mut self.rt_module) {
Expand Down
130 changes: 130 additions & 0 deletions crates/rust/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,133 @@ mod inline_and_path {
generate_all,
});
}

#[allow(unused)]
mod newtyped_list {
use std::ops::Deref;

wit_bindgen::generate!({
inline: r#"
package test:newtyped-list;

interface ntl {
type newtyped-list = list<u8>;
type typed-list = list<u8>;

use-newtyped-list: func(nl: newtyped-list) -> newtyped-list;
use-typed-list: func(tl: typed-list) -> typed-list;
use-list: func(l: list<u8>) -> list<u8>;
}

interface ntl-bytes {
type newtyped-bytes-list = list<u8>;
type typed-bytes-list = list<u8>;

use-newtyped-bytes-list: func(nl: newtyped-bytes-list) -> newtyped-bytes-list;
use-typed-bytes-list: func(tl: typed-bytes-list) -> typed-bytes-list;
use-bytes-list: func(l: list<u8>) -> list<u8>;
}

interface ntl-noncopy {
// this will be new-typed by a non-copy struct
type noncopy-byte = u8;

type newtyped-noncopy-list = list<noncopy-byte>;
type typed-noncopy-list = list<noncopy-byte>;

use-newtyped-noncopy-list: func(nl: newtyped-noncopy-list) -> newtyped-noncopy-list;
use-typed-noncopy-list: func(tl: typed-noncopy-list) -> typed-noncopy-list;
use-noncopy-list: func(l: list<noncopy-byte>) -> list<noncopy-byte>;
}

interface ntl-noncanonical {
// tuples are non-canonical, but can implement copy
type noncanonical = tuple<u8,u8>;

type newtyped-noncanonical-list = list<noncanonical>;
type typed-noncanonical-list = list<noncanonical>;

use-newtyped-noncanonical-list: func(nl: newtyped-noncanonical-list) -> newtyped-noncanonical-list;
use-typed-noncanonical-list: func(tl: typed-noncanonical-list) -> typed-noncanonical-list;
use-noncanonical-list: func(l: list<noncanonical>) -> list<noncanonical>;
}

world test {
import ntl;
export ntl;
import ntl-bytes;
export ntl-bytes;
import ntl-noncopy;
export ntl-noncopy;
import ntl-noncanonical;
export ntl-noncanonical;
}
"#,
with: {
"test:newtyped-list/ntl/newtyped-list": crate::newtyped_list::NewtypedList,
"test:newtyped-list/ntl-bytes/newtyped-bytes-list": bytes::Bytes,
"test:newtyped-list/ntl-noncopy/noncopy-byte": crate::newtyped_list::NoncopyByte,
"test:newtyped-list/ntl-noncopy/newtyped-noncopy-list": crate::newtyped_list::NewtypedNoncopyList,
"test:newtyped-list/ntl-noncanonical/newtyped-noncanonical-list": crate::newtyped_list::NewtypedNoncanonicalList,
}
});

pub struct NewtypedList(Vec<u8>);

impl From<Vec<u8>> for NewtypedList {
fn from(value: Vec<u8>) -> Self {
NewtypedList(value)
}
}

impl From<NewtypedList> for Vec<u8> {
fn from(value: NewtypedList) -> Self {
value.0
}
}

pub struct NoncopyByte(u8);
pub struct NewtypedNoncopyList(Vec<String>);

impl From<Vec<String>> for NewtypedNoncopyList {
fn from(value: Vec<String>) -> Self {
NewtypedNoncopyList(value)
}
}

impl From<NewtypedNoncopyList> for Vec<String> {
fn from(value: NewtypedNoncopyList) -> Self {
value.0
}
}

impl Deref for NewtypedNoncopyList {
type Target = Vec<String>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub struct NewtypedNoncanonicalList(Vec<(u8, u8)>);

impl From<Vec<(u8, u8)>> for NewtypedNoncanonicalList {
fn from(value: Vec<(u8, u8)>) -> Self {
NewtypedNoncanonicalList(value)
}
}

impl From<NewtypedNoncanonicalList> for Vec<(u8, u8)> {
fn from(value: NewtypedNoncanonicalList) -> Self {
value.0
}
}

impl Deref for NewtypedNoncanonicalList {
type Target = Vec<(u8, u8)>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
}
Loading