lotus/lib/result/result.go

42 lines
901 B
Go
Raw Permalink Normal View History

package result
// Result is a small wrapper type encapsulating Value/Error tuples, mostly for
// use when sending values across channels
// NOTE: Avoid adding any functionality to this, any "nice" things added here will
// make it more difficult to switch to a more standardised Result-like type when
// one gets into the stdlib, or when we will want to switch to a library providing
// those types.
type Result[T any] struct {
Value T
Error error
}
func Ok[T any](value T) Result[T] {
return Result[T]{
Value: value,
}
}
func Err[T any](err error) Result[T] {
return Result[T]{
Error: err,
}
}
func Wrap[T any](value T, err error) Result[T] {
return Result[T]{
Value: value,
Error: err,
}
}
func (r Result[T]) Unwrap() (T, error) {
return r.Value, r.Error
}
func (r Result[T]) Assert(noErrFn func(err error, msgAndArgs ...interface{})) T {
noErrFn(r.Error)
return r.Value
}