Skip to content

Commit 35f9a93

Browse files
Result construction helpers & make resource containing parameters
mutable
1 parent 947cc54 commit 35f9a93

4 files changed

Lines changed: 76 additions & 32 deletions

File tree

crates/d/src/lib.rs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,22 +1231,32 @@ impl<'a> DInterfaceGenerator<'a> {
12311231
let lower_param_name = name.to_lower_camel_case();
12321232
let escaped_param_name = escape_d_identifier(&lower_param_name);
12331233

1234-
let needs_in_qualifier = match param {
1235-
Type::ErrorContext | Type::String => true,
1234+
let qualifier = match param {
1235+
Type::ErrorContext => todo!(),
1236+
Type::String => "in ".to_owned(),
12361237
Type::Id(id) => match &self.resolve.types[*id].kind {
1237-
TypeDefKind::Handle(_) | TypeDefKind::Enum(_) | TypeDefKind::Flags(_) => false,
1238-
_ => true,
1238+
TypeDefKind::Enum(_) | TypeDefKind::Flags(_) | TypeDefKind::Handle(_) => {
1239+
"".to_owned()
1240+
}
1241+
TypeDefKind::Future(_) | TypeDefKind::Map(_, _) | TypeDefKind::Stream(_) => {
1242+
todo!()
1243+
}
1244+
_ => {
1245+
if matches!(self.direction, Some(Direction::Export))
1246+
&& self.r#gen.types.get(*id).has_resource
1247+
{
1248+
"scope ref ".to_owned()
1249+
} else {
1250+
"in ".to_owned()
1251+
}
1252+
}
12391253
},
1240-
_ => false,
1254+
_ => "".to_owned(),
12411255
};
12421256

12431257
res.arguments.push((
12441258
escaped_param_name.into(),
1245-
if needs_in_qualifier {
1246-
"in ".to_owned()
1247-
} else {
1248-
"".to_owned()
1249-
} + &self.type_name(&param, self.fqn),
1259+
qualifier + &self.type_name(&param, self.fqn),
12501260
));
12511261
}
12521262

@@ -2721,7 +2731,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27212731
self.push_str(&format!("if ({len}) deallocate ~= cast(){ptr};\n"));
27222732
}
27232733

2724-
results.push(format!("{list_name}({ptr}[0..{len}])"));
2734+
let result = format!("{list_name}({ptr}[0..{len}])");
2735+
2736+
let tmpvar = tempname("_list", self.tmp());
2737+
self.push_str(&format!("auto {tmpvar} = {result};\n"));
2738+
results.push(tmpvar);
27252739
}
27262740
abi::Instruction::StringLift => {
27272741
let tmp = self.tmp();
@@ -2741,7 +2755,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27412755
self.push_str(&format!("if ({len}) deallocate ~= cast(){ptr};\n"));
27422756
}
27432757

2744-
results.push(format!("WitString({ptr}[0..{len}])"));
2758+
let result = format!("WitString({ptr}[0..{len}])");
2759+
2760+
let tmpvar = tempname("_list", self.tmp());
2761+
self.push_str(&format!("auto {tmpvar} = {result};\n"));
2762+
results.push(tmpvar);
27452763
}
27462764
abi::Instruction::ListLift { ty, element, .. } => {
27472765
let Block {
@@ -2794,7 +2812,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27942812
}
27952813

27962814
let list_name = self.r#gen.type_name(&Type::Id(*ty), self.r#gen.fqn);
2797-
results.push(format!("{list_name}({list})"));
2815+
let result = format!("{list_name}({list})");
2816+
2817+
let tmpvar = tempname("_witList", self.tmp());
2818+
self.push_str(&format!("auto {tmpvar} = {result};\n"));
2819+
results.push(tmpvar);
27982820
}
27992821

28002822
abi::Instruction::FixedLengthListLift { size, id, .. } => {
@@ -2900,7 +2922,16 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
29002922
}
29012923
abi::Instruction::HandleLift { ty, .. } => {
29022924
let name = self.r#gen.type_name(&Type::Id(*ty), self.r#gen.fqn);
2903-
results.push(format!("{name}({})", operands[0]));
2925+
let result = format!("{name}({})", operands[0]);
2926+
2927+
if matches!(self.r#gen.direction, Some(Direction::Export)) && operands[0] == "this"
2928+
{
2929+
results.push(result);
2930+
} else {
2931+
let tmpvar = tempname("_handle", self.tmp());
2932+
self.push_str(&format!("auto {tmpvar} = {result};\n"));
2933+
results.push(tmpvar);
2934+
}
29042935
}
29052936

29062937
abi::Instruction::TupleLower { tuple, .. } => {
@@ -3246,10 +3277,10 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
32463277
bool {is_err} = ({op0}) != 0;
32473278
if ({is_err}) {{
32483279
{err}
3249-
{resultname} = {full_type}.err({err_value});
3280+
{resultname} = {full_type}.makeErr({err_value});
32503281
}} else {{
32513282
{ok}
3252-
{resultname} = {full_type}.ok({ok_value});
3283+
{resultname} = {full_type}.makeOk({ok_value});
32533284
}}\n"
32543285
));
32553286
results.push(resultname);

crates/d/src/wit_common.d

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,20 @@ private:
199199

200200
public:
201201
static if (is(T == void)) {
202-
static Result ok() @safe @nogc nothrow => Result(false, Storage.init);
202+
static Result makeOk() @safe @nogc nothrow => Result(false, Storage.init);
203203
} else {
204-
static Result ok(inout(T) value) @trusted @nogc nothrow {
204+
static inout(Result) makeOk(inout(T) value) @trusted @nogc nothrow {
205205
Storage newStorage = Storage.init;
206206
newStorage.value = cast(T)value;
207207

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

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

@@ -242,6 +242,19 @@ public:
242242
}
243243
}
244244

245+
auto ok(E, T)(inout T value) @safe @nogc nothrow {
246+
return Result!(T, E).makeOk(value);
247+
}
248+
auto ok(E)() @safe @nogc nothrow {
249+
return Result!(void, E).makeOk();
250+
}
251+
252+
auto err(T, E)(inout E value) @safe @nogc nothrow {
253+
return Result!(T, E).makeErr(value);
254+
}
255+
auto err(T)() @safe @nogc nothrow {
256+
return Result!(T, void).makeErr();
257+
}
245258

246259
void witFree(T)(scope ref T val) if (__traits(isArithmetic, T)) {
247260
// no-op
@@ -288,15 +301,15 @@ void witDrop(T : Result!(U, V), U, V)(scope ref T val) {
288301
T witClone(T : Result!(U, V), U, V)(in T val) {
289302
if (val.isErr) {
290303
static if (!is(V == void)) {
291-
return T.err(val.unwrapErr.witClone);
304+
return T.makeErr(val.unwrapErr.witClone);
292305
} else {
293-
return T.err;
306+
return T.makeErr;
294307
}
295308
} else {
296309
static if (!is(U == void)) {
297-
return T.ok(val.unwrap.witClone);
310+
return T.makeOk(val.unwrap.witClone);
298311
} else {
299-
return T.ok;
312+
return T.makeOk;
300313
}
301314
}
302315
}

tests/runtime/flavorful/runner.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void run() {
3232
);
3333
}
3434

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

3737
{
3838
auto result = fListInVariant2();
@@ -73,14 +73,14 @@ void run() {
7373

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

7979
auto result = listOfVariants(input1[].witList, input2[].witList, input3[].witList);
8080
scope(exit) result.witFree;
8181

8282
static immutable bool[] output1 = [false, true];
83-
static immutable Result!()[] output2 = [Result!().err, Result!().ok];
83+
static immutable Result!()[] output2 = [err!void, ok!void];
8484
static immutable MyErrno[] output3 = [MyErrno.a, MyErrno.b];
8585
assert(result[0] == output1);
8686
assert(result[1] == output2);

tests/runtime/flavorful/test.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ Result!(void, MyErrno) errnoResult() {
4646

4747
if (first) {
4848
first = false;
49-
return Result!(void, MyErrno).err(MyErrno.b);
49+
return MyErrno.b.err!void;
5050
} else {
51-
return Result!(void, MyErrno).ok();
51+
return ok!MyErrno;
5252
}
5353
}
5454

@@ -76,14 +76,14 @@ Tuple!(WitList!bool, WitList!(Result!()), WitList!MyErrno) listOfVariants(in Wit
7676
static immutable bool[] boolsCmp = [true, false];
7777
assert(bools == boolsCmp[]);
7878

79-
static immutable Result!()[] resultsCmp = [Result!().ok, Result!().err];
79+
static immutable Result!()[] resultsCmp = [ok!void, err!void];
8080
assert(results == resultsCmp[]);
8181

8282
static immutable MyErrno[] enumsCmp = [MyErrno.success, MyErrno.a];
8383
assert(enums == enumsCmp[]);
8484

8585
static immutable bool[] boolsOut = [false, true];
86-
static immutable Result!(void)[] resultsOut = [Result!().err, Result!().ok];
86+
static immutable Result!(void)[] resultsOut = [err!void, ok!void];
8787
static immutable MyErrno[] enumsOut = [MyErrno.a, MyErrno.b];
8888
return tuple(
8989
boolsOut.witList,

0 commit comments

Comments
 (0)