-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathoption.go
59 lines (52 loc) · 1.31 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cm
// Option represents a Component Model [option<T>] type.
//
// [option<T>]: https://component-model.bytecodealliance.org/design/wit.html#options
type Option[T any] struct {
_ HostLayout
option[T]
}
// None returns an [Option] representing the none case,
// equivalent to the zero value.
func None[T any]() Option[T] {
return Option[T]{}
}
// Some returns an [Option] representing the some case.
func Some[T any](v T) Option[T] {
return Option[T]{
option: option[T]{
isSome: true,
some: v,
},
}
}
// option represents the internal representation of a Component Model option type.
// The first byte is a bool representing none or some,
// followed by storage for the associated type T.
type option[T any] struct {
_ HostLayout
isSome bool
some T
}
// None returns true if o represents the none case.
func (o *option[T]) None() bool {
return !o.isSome
}
// Some returns a non-nil *T if o represents the some case,
// or nil if o represents the none case.
func (o *option[T]) Some() *T {
if o.isSome {
return &o.some
}
return nil
}
// Value returns T if o represents the some case,
// or the zero value of T if o represents the none case.
// This does not have a pointer receiver, so it can be chained.
func (o option[T]) Value() T {
if !o.isSome {
var zero T
return zero
}
return o.some
}