-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparable_map.go
More file actions
26 lines (23 loc) · 1.15 KB
/
Copy pathcomparable_map.go
File metadata and controls
26 lines (23 loc) · 1.15 KB
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
package gsyncmap
// ComparableMap is a generic, type-safe concurrent map wrapping [sync.Map].
//
// ComparableMap is a drop-in replacement for [Map] when the value type is
// comparable. It overrides CompareAndDelete and CompareAndSwap with safe
// implementations that cannot panic, because the Value type constraint
// guarantees comparability at compile time.
//
// The zero value of ComparableMap is valid and ready to use. ComparableMap
// must not be copied after first use (same restriction as [sync.Map]).
type ComparableMap[Key, Value comparable] struct {
Map[Key, Value]
}
// CompareAndDelete deletes the entry for key if its value is equal to oldVal.
// Returns true if the entry was deleted.
func (m *ComparableMap[Key, Value]) CompareAndDelete(key Key, oldVal Value) (deleted bool) {
return m.Map.syncMap().CompareAndDelete(key, oldVal)
}
// CompareAndSwap swaps the oldVal and newVal values for key if the value stored in
// the map is equal to oldVal. Returns true if the swap was performed.
func (m *ComparableMap[Key, Value]) CompareAndSwap(key Key, oldVal, newVal Value) (swapped bool) {
return m.Map.syncMap().CompareAndSwap(key, oldVal, newVal)
}