-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncmap_go1_20.go
31 lines (27 loc) · 1.26 KB
/
syncmap_go1_20.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
//go:build go1.20
package mapz
// Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.
//
// Swap is only available from Go 1.20 as that is when Go added the sync.Map.Swap method.
func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
p, l := m.m.Swap(key, value)
if l && p != nil {
return p.(V), true
}
var zero V
return zero, l
}
// CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.
//
// If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
//
// CompareAndDelete is only available from Go 1.20 as that is when Go added the sync.Map.CompareAndDelete method.
func (m *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
return m.m.CompareAndDelete(key, old)
}
// CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
//
// CompareAndSwap is only available from Go 1.20 as that is when Go added the sync.Map.CompareAndSwap method.
func (m *SyncMap[K, V]) CompareAndSwap(key K, old, new V) bool {
return m.m.CompareAndSwap(key, old, new)
}