Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f9b0617e0 | |||
| 417eb2f962 | |||
| c49e33b0c3 | |||
| afcc57aa70 |
31
container/option.go
Normal file
31
container/option.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package ucontainer
|
||||
|
||||
// Option can either hold a value, or nothing.
|
||||
type Option[T any] struct {
|
||||
value T
|
||||
exists bool
|
||||
}
|
||||
|
||||
// O creates a new option with the specified value.
|
||||
func O[T any] (value T) Option[T] {
|
||||
return Option[T] {
|
||||
value: value,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Void returns an option with no value.
|
||||
func Void[T any] () Option[T] {
|
||||
return Option[T] { }
|
||||
}
|
||||
|
||||
// Exists returns if the value is currently set.
|
||||
func (option Option[T]) Exists () bool {
|
||||
return option.exists
|
||||
}
|
||||
|
||||
// Value returns the value and true if the value exists. If not, it returns the
|
||||
// zero value and false.
|
||||
func (option Option[T]) Value () (T, bool) {
|
||||
return option.value, option.exists
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package ucontainer
|
||||
|
||||
// Optional can either hold a value, or nothing.
|
||||
type Optional[T any] struct {
|
||||
value T
|
||||
exists bool
|
||||
}
|
||||
|
||||
// O creates a new optional with the specified value.
|
||||
func O[T any] (value T) Optional[T] {
|
||||
return Optional[T] {
|
||||
value: value,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Void returns an optional with no value.
|
||||
func Void[T any] () Optional[T] {
|
||||
return Optional[T] { }
|
||||
}
|
||||
|
||||
// Exists returns if the value is currently set.
|
||||
func (optional Optional[T]) Exists () bool {
|
||||
return optional.exists
|
||||
}
|
||||
|
||||
// Value returns the value and true if the value exists. If not, it returns the
|
||||
// zero value and false.
|
||||
func (optional Optional[T]) Value () (T, bool) {
|
||||
return optional.value, optional.exists
|
||||
}
|
||||
38
sync/atomic/generic.go
Normal file
38
sync/atomic/generic.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package uatomic
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
// Atom is a generic wrapper for atomic.Value.
|
||||
type Atom[T comparable] struct {
|
||||
value atomic.Value
|
||||
}
|
||||
|
||||
type atomWrapper[T comparable] struct { value T }
|
||||
|
||||
// CompareAndSwap executes the compare-and-swap operation for the Atom.
|
||||
func (atom *Atom[T]) CompareAndSwap(old, neww T) (swapped bool) {
|
||||
return atom.value.CompareAndSwap(
|
||||
atomWrapper[T] { value: old },
|
||||
atomWrapper[T] { value: neww })
|
||||
}
|
||||
|
||||
func (atom *Atom[T]) Load() T {
|
||||
return castSafe[T](atom.value.Load())
|
||||
}
|
||||
|
||||
func (atom *Atom[T]) Store(val T) {
|
||||
atom.value.Store(val)
|
||||
}
|
||||
|
||||
func (atom *Atom[T]) Swap(neww T) T {
|
||||
return castSafe[T](atom.value.Swap(neww))
|
||||
}
|
||||
|
||||
func castSafe[T comparable](value any) T {
|
||||
var zero T
|
||||
if value == nil {
|
||||
return zero
|
||||
} else {
|
||||
return value.(T)
|
||||
}
|
||||
}
|
||||
@@ -39,12 +39,12 @@ func (this *Monitor[T]) Borrow () (T, func ()) {
|
||||
// updated value. The intended use of this function is like this:
|
||||
//
|
||||
// value, done := monitor.BorrowReturn()
|
||||
// defer done(value)
|
||||
func (this *Monitor[T]) BorrowReturn () (T, func (T)) {
|
||||
// defer done(&value)
|
||||
func (this *Monitor[T]) BorrowReturn () (T, func (*T)) {
|
||||
this.mutex.Lock()
|
||||
return this.value, func (value T) {
|
||||
return this.value, func (value *T) {
|
||||
defer this.mutex.Unlock()
|
||||
this.value = value
|
||||
this.value = *value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,12 +86,12 @@ func (this *RWMonitor[T]) Borrow () (T, func ()) {
|
||||
// updated value. The intended use of this function is like this:
|
||||
//
|
||||
// value, done := monitor.BorrowReturn()
|
||||
// defer done(value)
|
||||
func (this *RWMonitor[T]) BorrowReturn () (T, func (T)) {
|
||||
// defer done(&value)
|
||||
func (this *RWMonitor[T]) BorrowReturn () (T, func (*T)) {
|
||||
this.mutex.Lock()
|
||||
return this.value, func (value T) {
|
||||
return this.value, func (value *T) {
|
||||
defer this.mutex.Unlock()
|
||||
this.value = value
|
||||
this.value = *value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ func TestMonitor (test *testing.T) {
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.BorrowReturn()
|
||||
defer done(&value)
|
||||
value += 3
|
||||
defer done(value)
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.Borrow()
|
||||
@@ -56,8 +56,8 @@ func TestRWMonitor (test *testing.T) {
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.BorrowReturn()
|
||||
defer done(&value)
|
||||
value += 3
|
||||
defer done(value)
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.RBorrow()
|
||||
|
||||
Reference in New Issue
Block a user