Skip to content

Commit 8368d74

Browse files
committed
wip: test more list types
1 parent f41ad1f commit 8368d74

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ wit-bindgen = { path = '../guest-rust', features = ['async'] }
3434
test-helpers = { path = '../test-helpers' }
3535
# For use with the custom attributes test
3636
serde_json = "1"
37+
bytes = "*"
3738

3839
[features]
3940
serde = ['dep:serde', 'wit-bindgen-core/serde']

crates/rust/src/bindgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
814814
uwriteln!(self.src, "let e{tmp} = {body};");
815815
uwriteln!(self.src, "{result}.push(e{tmp});");
816816
uwriteln!(self.src, "}}");
817-
results.push(result);
817+
results.push(format!("<_ as From<Vec<_>>>::from({result})"));
818818
let dealloc = self.r#gen.path_to_cabi_dealloc();
819819
self.push_str(&format!(
820820
"{dealloc}({base}, {len} * {size}, {align});\n",

crates/rust/tests/codegen.rs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ mod inline_and_path {
3434

3535
#[allow(unused)]
3636
mod newtyped_list {
37+
use std::ops::Deref;
38+
3739
wit_bindgen::generate!({
3840
inline: r#"
3941
package test:newtyped-list;
@@ -47,17 +49,60 @@ mod newtyped_list {
4749
use-list: func(l: list<u8>) -> list<u8>;
4850
}
4951
52+
interface ntl-bytes {
53+
type newtyped-bytes-list = list<u8>;
54+
type typed-bytes-list = list<u8>;
55+
56+
use-newtyped-bytes-list: func(nl: newtyped-bytes-list) -> newtyped-bytes-list;
57+
use-typed-bytes-list: func(tl: typed-bytes-list) -> typed-bytes-list;
58+
use-bytes-list: func(l: list<u8>) -> list<u8>;
59+
}
60+
61+
interface ntl-noncopy {
62+
// this will be new-typed by a non-copy struct
63+
type noncopy-byte = u8;
64+
65+
type newtyped-noncopy-list = list<noncopy-byte>;
66+
type typed-noncopy-list = list<noncopy-byte>;
67+
68+
use-newtyped-noncopy-list: func(nl: newtyped-noncopy-list) -> newtyped-noncopy-list;
69+
use-typed-noncopy-list: func(tl: typed-noncopy-list) -> typed-noncopy-list;
70+
use-noncopy-list: func(l: list<noncopy-byte>) -> list<noncopy-byte>;
71+
}
72+
73+
interface ntl-noncanonical {
74+
// tuples are non-canonical, but can implement copy
75+
type noncanonical = tuple<u8,u8>;
76+
77+
type newtyped-noncanonical-list = list<noncanonical>;
78+
type typed-noncanonical-list = list<noncanonical>;
79+
80+
use-newtyped-noncanonical-list: func(nl: newtyped-noncanonical-list) -> newtyped-noncanonical-list;
81+
use-typed-noncanonical-list: func(tl: typed-noncanonical-list) -> typed-noncanonical-list;
82+
use-noncanonical-list: func(l: list<noncanonical>) -> list<noncanonical>;
83+
}
84+
5085
world test {
5186
import ntl;
5287
export ntl;
88+
import ntl-bytes;
89+
export ntl-bytes;
90+
import ntl-noncopy;
91+
export ntl-noncopy;
92+
import ntl-noncanonical;
93+
export ntl-noncanonical;
5394
}
5495
"#,
5596
with: {
5697
"test:newtyped-list/ntl/newtyped-list": crate::newtyped_list::NewtypedList,
98+
"test:newtyped-list/ntl-bytes/newtyped-bytes-list": bytes::Bytes,
99+
"test:newtyped-list/ntl-noncopy/noncopy-byte": crate::newtyped_list::NoncopyByte,
100+
"test:newtyped-list/ntl-noncopy/newtyped-noncopy-list": crate::newtyped_list::NewtypedNoncopyList,
101+
"test:newtyped-list/ntl-noncanonical/newtyped-noncanonical-list": crate::newtyped_list::NewtypedNoncanonicalList,
57102
}
58103
});
59104

60-
struct NewtypedList(Vec<u8>);
105+
pub struct NewtypedList(Vec<u8>);
61106

62107
impl From<Vec<u8>> for NewtypedList {
63108
fn from(value: Vec<u8>) -> Self {
@@ -70,4 +115,49 @@ mod newtyped_list {
70115
value.0
71116
}
72117
}
118+
119+
pub struct NoncopyByte(u8);
120+
pub struct NewtypedNoncopyList(Vec<String>);
121+
122+
impl From<Vec<String>> for NewtypedNoncopyList {
123+
fn from(value: Vec<String>) -> Self {
124+
NewtypedNoncopyList(value)
125+
}
126+
}
127+
128+
impl From<NewtypedNoncopyList> for Vec<String> {
129+
fn from(value: NewtypedNoncopyList) -> Self {
130+
value.0
131+
}
132+
}
133+
134+
impl Deref for NewtypedNoncopyList {
135+
type Target = Vec<String>;
136+
137+
fn deref(&self) -> &Self::Target {
138+
&self.0
139+
}
140+
}
141+
142+
pub struct NewtypedNoncanonicalList(Vec<(u8, u8)>);
143+
144+
impl From<Vec<(u8, u8)>> for NewtypedNoncanonicalList {
145+
fn from(value: Vec<(u8, u8)>) -> Self {
146+
NewtypedNoncanonicalList(value)
147+
}
148+
}
149+
150+
impl From<NewtypedNoncanonicalList> for Vec<(u8, u8)> {
151+
fn from(value: NewtypedNoncanonicalList) -> Self {
152+
value.0
153+
}
154+
}
155+
156+
impl Deref for NewtypedNoncanonicalList {
157+
type Target = Vec<(u8, u8)>;
158+
159+
fn deref(&self) -> &Self::Target {
160+
&self.0
161+
}
162+
}
73163
}

0 commit comments

Comments
 (0)