Created
July 18, 2023 09:44
-
-
Save lartie/0fd1495dd0b7f2fc25a1e78536ba4e6d to your computer and use it in GitHub Desktop.
SyncMap With Generict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sync | |
import "sync" | |
type Map[K, V any] struct { | |
s sync.Map | |
} | |
func NewMap[K, V any]() Map[K, V] { | |
return Map[K, V]{ | |
s: sync.Map{}, | |
} | |
} | |
func (m *Map[K, V]) CompareAndDelete(key K, old V) { | |
m.s.CompareAndDelete(key, old) | |
} | |
func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool) { | |
return m.s.CompareAndSwap(key, old, new) | |
} | |
func (m *Map[K, V]) Delete(key K) { | |
m.s.Delete(key) | |
} | |
func (m *Map[K, V]) Load(key K) (value V, ok bool) { | |
return m.s.Load(key) | |
} | |
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) { | |
return m.s.LoadAndDelete(key) | |
} | |
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { | |
return m.s.LoadOrStore(key, value) | |
} | |
func (m *Map[K, V]) Range(f func(key K, value V) bool) { | |
m.s.Range(func(key, value interface{}) bool { | |
return f(key.(K), value.(V)) | |
}) | |
} | |
func (m *Map[K, V]) Store(key K, value V) { | |
m.s.Store(key, value) | |
} | |
func (m *Map[K, V]) Swap(key K, value V) (previous any, loaded bool) { | |
return m.s.Swap(key, value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment