diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 21:22:14 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 21:39:42 -0600 |
commit | 7a2a4b9af7b3526059750224900964b112f58947 (patch) | |
tree | 64b79b0b8b96cc55d243ab986aecc564c3fb78da /lib/containers/syncmap.go | |
parent | fca01a917345729bd6ca0399f90fb620c125fe6a (diff) |
Move the remaining former-generic.go parts out of lib/util/
Diffstat (limited to 'lib/containers/syncmap.go')
-rw-r--r-- | lib/containers/syncmap.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/containers/syncmap.go b/lib/containers/syncmap.go new file mode 100644 index 0000000..6c26b85 --- /dev/null +++ b/lib/containers/syncmap.go @@ -0,0 +1,40 @@ +// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package containers + +import ( + "sync" +) + +type SyncMap[K comparable, V any] struct { + inner sync.Map +} + +func (m *SyncMap[K, V]) Delete(key K) { m.inner.Delete(key) } +func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { + _value, ok := m.inner.Load(key) + if ok { + value = _value.(V) + } + return value, ok +} +func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { + _value, ok := m.inner.LoadAndDelete(key) + if ok { + value = _value.(V) + } + return value, ok +} +func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { + _actual, loaded := m.inner.LoadOrStore(key, value) + actual = _actual.(V) + return actual, loaded +} +func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { + m.inner.Range(func(key, value any) bool { + return f(key.(K), value.(V)) + }) +} +func (m *SyncMap[K, V]) Store(key K, value V) { m.inner.Store(key, value) } |