Skip to content

Commit 96816c8

Browse files
authored
Merge pull request #333 from bytecodealliance/ydnar/result-dx
2 parents a2d79b6 + 313c60e commit 96816c8

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

cm/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## [Unreleased]
6+
7+
### Added
8+
9+
- Mutating methods `SetOK` and `SetErr` on `result` types (`Result[Shape, OK, Err]`).
10+
511
## [v0.2.2] — 2025-03-16
612

713
### Fixed

cm/result.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ type result[Shape, OK, Err any] struct {
3939
data Shape // [unsafe.Sizeof(*(*Shape)(unsafe.Pointer(nil)))]byte
4040
}
4141

42+
// OK returns an OK result with shape Shape and type OK and Err.
43+
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
44+
func OK[R AnyResult[Shape, OK, Err], Shape, OK, Err any](ok OK) R {
45+
var r Result[Shape, OK, Err]
46+
r.validate()
47+
r.isErr = ResultOK
48+
*((*OK)(unsafe.Pointer(&r.data))) = ok
49+
return R(r)
50+
}
51+
52+
// Err returns an error result with shape Shape and type OK and Err.
53+
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
54+
func Err[R AnyResult[Shape, OK, Err], Shape, OK, Err any](err Err) R {
55+
var r Result[Shape, OK, Err]
56+
r.validate()
57+
r.isErr = ResultErr
58+
*((*Err)(unsafe.Pointer(&r.data))) = err
59+
return R(r)
60+
}
61+
62+
// SetOK sets r to an OK result.
63+
func (r *result[Shape, OK, Err]) SetOK(ok OK) {
64+
r.validate()
65+
r.isErr = ResultOK
66+
*((*OK)(unsafe.Pointer(&r.data))) = ok
67+
}
68+
69+
// SetErr sets r to an error result.
70+
func (r *result[Shape, OK, Err]) SetErr(err Err) {
71+
r.validate()
72+
r.isErr = ResultErr
73+
*((*Err)(unsafe.Pointer(&r.data))) = err
74+
}
75+
4276
// IsOK returns true if r represents the OK case.
4377
func (r *result[Shape, OK, Err]) IsOK() bool {
4478
r.validate()
@@ -107,23 +141,3 @@ func (r *result[Shape, OK, Err]) validate() {
107141
panic("result: size of data type == 0, but result size != 1")
108142
}
109143
}
110-
111-
// OK returns an OK result with shape Shape and type OK and Err.
112-
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
113-
func OK[R AnyResult[Shape, OK, Err], Shape, OK, Err any](ok OK) R {
114-
var r Result[Shape, OK, Err]
115-
r.validate()
116-
r.isErr = ResultOK
117-
*((*OK)(unsafe.Pointer(&r.data))) = ok
118-
return R(r)
119-
}
120-
121-
// Err returns an error result with shape Shape and type OK and Err.
122-
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
123-
func Err[R AnyResult[Shape, OK, Err], Shape, OK, Err any](err Err) R {
124-
var r Result[Shape, OK, Err]
125-
r.validate()
126-
r.isErr = ResultErr
127-
*((*Err)(unsafe.Pointer(&r.data))) = err
128-
return R(r)
129-
}

cm/result_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,29 @@ var (
1313
)
1414

1515
type resulter[OK, Err any] interface {
16+
SetOK(OK)
17+
SetErr(Err)
1618
IsOK() bool
1719
IsErr() bool
1820
OK() *OK
1921
Err() *Err
2022
Result() (OK, Err, bool)
2123
}
2224

25+
func TestResultSetOKSetErr(t *testing.T) {
26+
var r Result[string, int32, string]
27+
28+
r.SetOK(12345)
29+
if want, got := int32(12345), r.OK(); *got != want {
30+
t.Errorf("OK(): %v, expected %v", got, want)
31+
}
32+
33+
r.SetErr("error")
34+
if want, got := "error", r.Err(); *got != want {
35+
t.Errorf("Err(): %v, expected %v", got, want)
36+
}
37+
}
38+
2339
func TestResultOKOrErr(t *testing.T) {
2440
r1 := OK[Result[string, string, struct{}]]("hello")
2541
if ok := r1.OK(); ok == nil {

0 commit comments

Comments
 (0)