-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncmap.go
63 lines (53 loc) · 1.67 KB
/
syncmap.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
package genmap
import "sync"
type SyncMap interface {
getMap() *sync.Map
}
type genSyncMap struct {
m *sync.Map
}
func (gsm *genSyncMap) getMap() *sync.Map {
return gsm.m
}
// NewSyncMap returns a type-safe generic map built upon a sync.Map.
func NewSyncMap() SyncMap {
var m sync.Map
return &genSyncMap{
m: &m,
}
}
// DeleteSync deletes the value for a key.
func DeleteSync[T any, K comparable](m SyncMap, key Key[T, K]) {
m.getMap().Delete(key.Key())
}
// StoreSync sets the value for a key.
func StoreSync[T any, K comparable](m SyncMap, key Key[T, K], value T) {
m.getMap().Store(key.Key(), value)
}
// LoadSync returns the value stored in the map for a key, or nil if no value is present. The ok result indicates
// whether value was found in the map.
func LoadSync[T any, K comparable](m SyncMap, key Key[T, K]) (value T, ok bool) {
val, ok := m.getMap().Load(key.Key())
if val != nil {
value = val.(T)
}
return value, ok
}
// LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the
// key was present.
func LoadAndDelete[T any, K comparable](m SyncMap, key Key[T, K]) (value T, loaded bool) {
val, loaded := m.getMap().LoadAndDelete(key.Key())
if val != nil {
value = val.(T)
}
return value, loaded
}
// LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The
// loaded result is true if the value was loaded, false if stored.
func LoadOrStore[T any, K comparable](m SyncMap, key Key[T, K], value T) (actual T, loaded bool) {
val, loaded := m.getMap().LoadOrStore(key.Key(), value)
if val != nil {
value = val.(T)
}
return value, loaded
}