diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 20:31:51 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 20:31:51 -0600 |
commit | bee2b5ee5f9217a0cc87c5c026257822b55b9005 (patch) | |
tree | fce00fd08626a8afb28742387d3c2b9964e65e0c /lib/util/syncmap.go | |
parent | 449a2aaadb0764dc542cdb2f7f029c70e60aee8f (diff) |
re-organize some things between files
Diffstat (limited to 'lib/util/syncmap.go')
-rw-r--r-- | lib/util/syncmap.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/util/syncmap.go b/lib/util/syncmap.go new file mode 100644 index 0000000..a281f2d --- /dev/null +++ b/lib/util/syncmap.go @@ -0,0 +1,40 @@ +// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package util + +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) } |