diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-05 12:06:30 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-05 13:32:56 -0700 |
commit | 2d939c9c6e62395ed924fe7c5cd4c4b294e391a9 (patch) | |
tree | f292beebe17f48a56550bea1435808b965ce6764 /map.go | |
parent | d69037701f6cdd4f5bb98c20af329c02ba89bb90 (diff) |
Rename to git.lukeshu.com/go/containers, split in to 2 separate packages
Diffstat (limited to 'map.go')
-rw-r--r-- | map.go | 69 |
1 files changed, 0 insertions, 69 deletions
@@ -1,69 +0,0 @@ -// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package typedsync - -import ( - "sync" -) - -// Map is a type-safe equivalent of the standard library's sync.Map. -// -// With versions of Go prior to Go 1.20, Map is specified too loosely, -// as -// -// Map[K any, V any] -// -// while with Go 1.20 and later, Map is specified as -// -// Map[K comparable, V any] -// -// This is because with Go versions prior to 1.20, 'comparable' was -// overly strict, disallowing many types that are valid map-keys (see -// https://github.com/golang/go/issues/56548). The type used as K in -// a Map older versions of Go must be a valid map-key type, even -// though the type specification of Map does not enforce that. -type Map[K mapkey, 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) -} |