github.com/diamondburned/arikawa/v2@v2.1.0/internal/moreatomic/syncmap.go (about) 1 package moreatomic 2 3 import ( 4 "sync/atomic" 5 6 "github.com/diamondburned/arikawa/v2/internal/moreatomic/syncmod" 7 ) 8 9 // Map is a thread-safe map that is a wrapper around sync.Map with slight API 10 // additions. 11 type Map struct { 12 val atomic.Value 13 ctor func() interface{} 14 } 15 16 func NewMap(ctor func() interface{}) *Map { 17 sm := &Map{ctor: ctor} 18 sm.Reset() 19 return sm 20 } 21 22 // Reset swaps the internal map out with a fresh one, dropping the old map. This 23 // method never errors. 24 func (sm *Map) Reset() error { 25 sm.val.Store(&syncmod.Map{New: sm.ctor}) 26 return nil 27 } 28 29 // LoadOrStore loads an existing value or stores a new value created from the 30 // given constructor then return that value. 31 func (sm *Map) LoadOrStore(k interface{}) (lv interface{}, loaded bool) { 32 return sm.val.Load().(*syncmod.Map).LoadOrStore(k) 33 } 34 35 // Load loads an existing value; it returns ok set to false if there is no 36 // value with that key. 37 func (sm *Map) Load(k interface{}) (lv interface{}, ok bool) { 38 return sm.val.Load().(*syncmod.Map).Load(k) 39 }