How Gust Represents Option and Result in Go
Gust uses one pointer to represent an optional value in Go. That makes it possible to distinguish a missing pointer from a pointer that was explicitly supplied as nil. The difference shows up when you convert an option back to Go’s familiar value-and-boolean pair.
A present value can be nil
Internally, Option[T] has one field, value *T. Its zero value represents None. Calling Some(v) stores the address of the constructor’s parameter, so the option has a value even when v itself is nil. Gust’s Option implementation
var p *int
v, ok := option.Some(p).Split()
fmt.Println(v == nil, ok) // true true
v, ok = option.None[*int]().Split()
fmt.Println(v == nil, ok) // true false
Copying the option copies its pointer. Constructing it with Some copies the argument into the parameter whose address Gust keeps. Whether that parameter needs heap storage depends on how the caller uses the result and what the compiler can inline. To measure the cost in your own code, benchmark the calling function with go test -bench=. -benchmem. Go FAQ on stack and heap allocation
Wrapping a pointer or its element
Gust has two other ways to wrap a pointer. Both return an absent option for a nil input. For a non-nil input, the distinction is easiest to see by changing the original variable after wrapping it.
port := 8080
elem := option.ElemOpt(&port) // Option[int]
ptr := option.PtrOpt(&port) // Option[*int]
port = 9090
fmt.Println(elem.Unwrap()) // 9090
fmt.Println(ptr.Unwrap() == &port) // true
ElemOpt[T](ptr *T) uses the caller’s pointer as its storage, so unwrapping reads the current value of port. The signature of PtrOpt is PtrOpt[U any, T *U](ptr T) Option[T]. Here Go infers T as *int and U as int. Gust stores the address of its copy of ptr, and unwrapping returns the pointer the caller passed in. Pointer constructors
What a zero Result tells you
Gust’s Result[T] holds an Option[T] for success and a separate error box for failure. IsErr() checks whether the option is absent. IsValid() checks whether either field carries information. For a freshly declared result, both the option and the error box are empty. Result implementation
var r result.Result[int]
v, err := r.Split()
fmt.Println(r.IsErr(), r.IsValid()) // true false
fmt.Println(v, err) // 0 <nil>
That matters if a result is a field in a larger struct. Checking only the error returned by Split loses the distinction between an uninitialized field and a successful zero. When adapting an ordinary Go function that returns (T, error), result.Ret preserves the supplied value on success.
How TryErr chooses a default
result.TryErr[T](nil) also succeeds, but the caller hasn’t supplied a value. Gust starts with a variable of type T and passes its address to a reflection helper. For int, the value stays zero. For *int, the helper finds a settable nil pointer and uses reflect.New to allocate an integer. Gust’s default-value helper · Go’s reflect.New documentation
p := result.TryErr[*int](nil).Unwrap()
fmt.Println(p == nil, *p) // false 0
The helper follows successive pointer levels, so a **int gets both levels initialized. It stops when it reaches the integer. For a pointer to a struct it stops at the struct itself, leaving any pointer fields inside at their ordinary zero values. If a successful nil pointer has meaning in your API, result.Ok((*int)(nil)) preserves it.