Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 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
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
40 changes: 40 additions & 0 deletions crates/rust/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,43 @@ mod inline_and_path {
generate_all,
});
}

#[allow(unused)]
mod newtyped_list {
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>;
}
world test {
import ntl;
export ntl;
}
"#,
with: {
"test:newtyped-list/ntl/newtyped-list": crate::newtyped_list::NewtypedList,
}
});

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
}
}
}