-
Notifications
You must be signed in to change notification settings - Fork 11
/
value.go
64 lines (57 loc) · 1.48 KB
/
value.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
60
61
62
63
64
package async
import (
"sync/atomic"
)
// A Value provides an atomic load and store of a value of any type.
// The behavior is analogous to atomic.Value, except that
// the value is not required to be of the same specific type.
// Can be useful for storing different implementations of an interface.
type Value struct {
p atomic.Pointer[atomic.Value]
}
// CompareAndSwap executes the compare-and-swap operation for the Value.
// The current implementation is not atomic.
func (v *Value) CompareAndSwap(old any, new any) (swapped bool) {
defer func() {
if err := recover(); err != nil {
swapped = false
}
}()
delegate := v.p.Load()
if delegate != nil {
if old == delegate.Load() {
v.p.Store(initValue(new))
return true
}
}
return false
}
// Load returns the value set by the most recent Store.
// It returns nil if there has been no call to Store for this Value.
func (v *Value) Load() (val any) {
delegate := v.p.Load()
if delegate != nil {
return delegate.Load()
}
return nil
}
// Store sets the value of the Value v to val.
// Store(nil) panics.
func (v *Value) Store(val any) {
v.p.Store(initValue(val))
}
// Swap stores new into Value and returns the previous value.
// It returns nil if the Value is empty.
// Swap(nil) panics.
func (v *Value) Swap(new any) (old any) {
oldValue := v.p.Swap(initValue(new))
if oldValue != nil {
return oldValue.Load()
}
return nil
}
func initValue(val any) *atomic.Value {
value := atomic.Value{}
value.Store(val)
return &value
}