Skip to content

Commit 0627197

Browse files
author
Dean Karn
authored
Micro optimizations (#18)
1 parent 1358dbe commit 0627197

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

values/option/option.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func (o Option[T]) IsNone() bool {
3434

3535
// Unwrap returns the values if the option is not empty or panics.
3636
func (o Option[T]) Unwrap() T {
37-
if o.IsNone() {
38-
panic("Option.Unwrap: option is None")
37+
if o.isSome {
38+
return o.value
3939
}
40-
return o.value
40+
panic("Option.Unwrap: option is None")
4141
}
4242

4343
// Some creates a new Option with the given values.
@@ -52,10 +52,10 @@ func None[T any]() Option[T] {
5252

5353
// MarshalJSON implements the json.Marshaler interface.
5454
func (o Option[T]) MarshalJSON() ([]byte, error) {
55-
if o.IsNone() {
56-
return []byte("null"), nil
55+
if o.isSome {
56+
return json.Marshal(o.value)
5757
}
58-
return json.Marshal(o.value)
58+
return []byte("null"), nil
5959
}
6060

6161
// UnmarshalJSON implements the json.Unmarshaler interface.
@@ -74,11 +74,11 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error {
7474
}
7575

7676
// Value implements the driver.Valuer interface.
77-
func (o *Option[T]) Value() (driver.Value, error) {
78-
if o.IsNone() {
79-
return nil, nil
77+
func (o Option[T]) Value() (driver.Value, error) {
78+
if o.isSome {
79+
return o.Unwrap(), nil
8080
}
81-
return o.Unwrap(), nil
81+
return nil, nil
8282
}
8383

8484
// Scan implements the sql.Scanner interface.

values/result/result.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func (r Result[T, E]) IsOk() bool {
2727

2828
// IsErr returns true if the result is not successful and has an error.
2929
func (r Result[T, E]) IsErr() bool {
30-
return !r.IsOk()
30+
return !r.isOk
3131
}
3232

3333
// Unwrap returns the values of the result. It panics if there is no result due to not checking for errors.
3434
func (r Result[T, E]) Unwrap() T {
35-
if !r.isOk {
36-
panic("Result.Unwrap(): result is Err")
35+
if r.isOk {
36+
return r.ok
3737
}
38-
return r.ok
38+
panic("Result.Unwrap(): result is Err")
3939
}
4040

4141
// Err returns the error of the result. To be used after calling IsOK()

0 commit comments

Comments
 (0)