Skip to content

Commit 56acfb5

Browse files
flavorful test and cast removals
1 parent 5dc028a commit 56acfb5

4 files changed

Lines changed: 52 additions & 35 deletions

File tree

crates/d/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,6 @@ impl WorldGenerator for D {
826826
let world_name = &world.name;
827827
let pkg = &resolve.packages[world.package.unwrap()].name;
828828
let version = env!("CARGO_PKG_VERSION");
829-
world_src.push_str(&format!(
830-
"\n@(imported!\"ldc.attributes\".section(\"component-type:wit-bindgen:{version}:\
831-
{pkg}:{world_name}:{opts_suffix}\"))\n"
832-
));
833829

834830
let mut producers = wasm_metadata::Producers::empty();
835831
producers.add(
@@ -848,7 +844,7 @@ impl WorldGenerator for D {
848844

849845
world_src.push_str(&format!(
850846
"
851-
void __wit_bindgen_component_types() {{
847+
void __wit_bindgen_component_type() {{
852848
imported!\"ldc.llvmasm\".__irEx!(
853849
\"\",
854850
\"\",

crates/d/src/wit_common.d

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ struct WitList(T) {
1818
T* ptr;
1919
size_t length;
2020

21-
this(T[] slice) @trusted {
22-
this = slice;
21+
this(inout T[] slice) inout @trusted {
22+
ptr = slice.ptr;
23+
length = slice.length;
2324
}
2425

2526
void opAssign(T[] slice) @trusted {
@@ -35,8 +36,7 @@ struct WitList(T) {
3536
bool opEquals(in T[] other) const => this[] == other;
3637
size_t toHash() const => this[].hashOf;
3738
}
38-
auto witList(T : U[], U)(T slice) => WitList!U(slice);
39-
39+
auto witList(T : U[], U)(inout T slice) => inout WitList!U(slice);
4040

4141
// WIT ABI for string matches List,
4242
// except list<char> in WIT is actually List!(dchar)
@@ -140,17 +140,17 @@ private:
140140
bool _present = false;
141141
T _value;
142142

143-
this(bool present, T value = T.init) @safe @nogc nothrow {
143+
this(bool present, inout T value) inout @safe @nogc nothrow {
144144
_present = present;
145145
_value = value;
146146
}
147147
public:
148-
static Option some(T value) @safe @nogc nothrow {
149-
return Option(true, value);
148+
static inout(Option) some(inout T value) @safe @nogc nothrow {
149+
return inout Option(true, value);
150150
}
151151

152152
static Option none() @safe @nogc nothrow {
153-
return Option(false);
153+
return Option(false, T.init);
154154
}
155155

156156
bool isSome() const @safe @nogc nothrow => _present;
@@ -168,12 +168,12 @@ public:
168168
{ return _present ? _value : fallback(); }
169169
}
170170

171-
auto some(T)(T value) @safe @nogc nothrow {
171+
auto some(T)(inout T value) @safe @nogc nothrow {
172172
return Option!T.some(value);
173173
}
174174

175-
auto none(T)(T value) @safe @nogc nothrow {
176-
return Option!T.some(value);
175+
auto none(T)() @safe @nogc nothrow {
176+
return Option!T.none;
177177
}
178178

179179
/// Based on Rust's Result
@@ -192,7 +192,7 @@ private:
192192
}
193193
Storage _storage;
194194

195-
this(bool hasError, Storage storage) @safe @nogc nothrow {
195+
this(bool hasError, inout(Storage) storage) inout @safe @nogc nothrow {
196196
_hasError = hasError;
197197
_storage = storage;
198198
}
@@ -201,22 +201,22 @@ public:
201201
static if (is(T == void)) {
202202
static Result ok() @safe @nogc nothrow => Result(false, Storage.init);
203203
} else {
204-
static Result ok(T value) @trusted @nogc nothrow {
204+
static Result ok(inout(T) value) @trusted @nogc nothrow {
205205
Storage newStorage = Storage.init;
206-
newStorage.value = value;
206+
newStorage.value = cast(T)value;
207207

208-
return Result(false, newStorage);
208+
return Result(false, cast(inout Storage)newStorage);
209209
}
210210
}
211211

212212
static if (is(E == void)) {
213213
static Result err() @safe @nogc nothrow => Result(true, Storage.init);
214214
} else {
215-
static Result err(E error) @trusted @nogc nothrow {
215+
static inout(Result) err(inout(E) error) @trusted @nogc nothrow {
216216
Storage newStorage = Storage.init;
217-
newStorage.error = error;
217+
newStorage.error = cast(E)error;
218218

219-
return Result(true, newStorage);
219+
return inout Result(true, cast(inout Storage)newStorage);
220220
}
221221
}
222222

@@ -320,6 +320,20 @@ T witClone(T : Tuple!U, U...)(in T val) {
320320
return clone;
321321
}
322322

323+
324+
void witFree(T : U[L], U, size_t L)(scope ref T val) {
325+
foreach (ref e; val) {
326+
e.witFree;
327+
}
328+
}
329+
T witClone(T : U[L], U, size_t L)(in T val) {
330+
T clone;
331+
foreach (i, ref e; clone) {
332+
e = val[i].witClone;
333+
}
334+
return clone;
335+
}
336+
323337
package(wit):
324338

325339
extern(C) {

crates/test/src/d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ fn compile(runner: &Runner, compile: &Compile<'_>, compiler: PathBuf) -> Result<
111111
cmd.arg(&compile.component.path)
112112
.arg("-betterC")
113113
.arg("-mtriple=wasm32-unknown-unknown")
114+
.arg("-fvisibility=hidden") // important to make sure unused symbols don't get linked
114115
.arg("-I")
115116
.arg(&compile.bindings_dir)
116117
.arg("-i") // compile included dependencies
117118
.arg("--de") // deperecations are errors
118119
.arg("-w") // warnings are errors
119120
.arg("-L--no-entry")
120-
.arg("-L--no-export-dynamic")
121121
.arg("--d-version=WitBindings_DummyLibc") // to provide bump allocator and `abort`
122122
.arg("--checkaction=halt") // to trap instead of using libc __assert
123123
.arg("-of")

tests/runtime/flavorful/runner.d

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void run() {
1313
}
1414

1515
{
16-
auto result = fListInRecord3(ListInRecord3(a: cast(WitString)"list_in_record3 input".witList));
16+
auto result = fListInRecord3(const ListInRecord3("list_in_record3 input".witList));
1717
scope(exit) result.witFree;
1818

1919
assert(
@@ -23,7 +23,7 @@ void run() {
2323
}
2424

2525
{
26-
auto result = fListInRecord4(ListInAlias(a: cast(WitString)"input4".witList));
26+
auto result = fListInRecord4(const ListInAlias("input4".witList));
2727
scope(exit) result.witFree;
2828

2929
assert(
@@ -32,7 +32,7 @@ void run() {
3232
);
3333
}
3434

35-
fListInVariant1(some(cast(WitString)"foo".witList), Result!(void, WitString).err(cast(WitString)"bar".witList));
35+
fListInVariant1(some("foo".witList), Result!(void, WitString).err("bar".witList));
3636

3737
{
3838
auto result = fListInVariant2();
@@ -41,17 +41,17 @@ void run() {
4141

4242
assert(
4343
result
44-
== some(cast(WitString)"list_in_variant2".witList)
44+
== some("list_in_variant2".witList)
4545
);
4646
}
4747

4848
{
49-
auto result = fListInVariant3(some(cast(WitString)"input3".witList));
49+
auto result = fListInVariant3(some("input3".witList));
5050
scope(exit) result.witFree;
5151

5252
assert(
5353
result
54-
== some(cast(WitString)"output3".witList)
54+
== some("output3".witList)
5555
);
5656
}
5757

@@ -62,8 +62,8 @@ void run() {
6262
assert(errnoResult().isOk);
6363

6464
{
65-
WitString[1] input = [cast(WitString)"typedef2".witList];
66-
auto result = listTypedefs(cast(WitString)"typedef1".witList, input[].witList);
65+
immutable WitString[1] input = ["typedef2".witList];
66+
auto result = listTypedefs("typedef1".witList, input[].witList);
6767
scope(exit) result.witFree;
6868

6969
assert(result[0] == (cast(ubyte[])"typedef3").witList);
@@ -72,12 +72,19 @@ void run() {
7272
}
7373

7474
{
75-
bool[2] input1 = [true, false];
76-
Result!(void, void)[2] input2 = [Result!(void, void).ok(), Result!(void, void).err()];
77-
MyErrno[2] input3 = [MyErrno.success, MyErrno.a];
75+
static immutable bool[] input1 = [true, false];
76+
static immutable Result!()[] input2 = [Result!().ok, Result!().err];
77+
static immutable MyErrno[] input3 = [MyErrno.success, MyErrno.a];
7878

7979
auto result = listOfVariants(input1[].witList, input2[].witList, input3[].witList);
8080
scope(exit) result.witFree;
81+
82+
static immutable bool[] output1 = [false, true];
83+
static immutable Result!()[] output2 = [Result!().err, Result!().ok];
84+
static immutable MyErrno[] output3 = [MyErrno.a, MyErrno.b];
85+
assert(result[0] == output1);
86+
assert(result[1] == output2);
87+
assert(result[2] == output3);
8188
}
8289
}
8390

0 commit comments

Comments
 (0)