diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-26 12:23:15 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-26 12:46:07 -0700 |
commit | 716dd31f7cf52d9772fd4ed687f9cdc921443a35 (patch) | |
tree | ded6651a3b6e240f60759465af9abccd52d5984d /map.go | |
parent | 774689062b4ac1921434a6c7a2ac78b8f29ac85a (diff) |
Set up as a separate repov0.0.1
Diffstat (limited to 'map.go')
-rw-r--r-- | map.go | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -0,0 +1,53 @@ +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package typedsync + +import ( + "sync" +) + +type Map[K comparable, V any] struct { + inner sync.Map +} + +func (m *Map[K, V]) Delete(key K) { + m.inner.Delete(key) +} + +func (m *Map[K, V]) Load(key K) (value V, ok bool) { + _value, ok := m.inner.Load(key) + if ok { + //nolint:forcetypeassert // Typed wrapper around untyped lib. + value = _value.(V) + } + return value, ok +} + +func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) { + _value, ok := m.inner.LoadAndDelete(key) + if ok { + //nolint:forcetypeassert // Typed wrapper around untyped lib. + value = _value.(V) + } + return value, ok +} + +func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { + _actual, loaded := m.inner.LoadOrStore(key, value) + //nolint:forcetypeassert // Typed wrapper around untyped lib. + actual = _actual.(V) + return actual, loaded +} + +func (m *Map[K, V]) Range(f func(key K, value V) bool) { + m.inner.Range(func(key, value any) bool { + //nolint:forcetypeassert // Typed wrapper around untyped lib. + return f(key.(K), value.(V)) + }) +} + +func (m *Map[K, V]) Store(key K, value V) { + m.inner.Store(key, value) +} |