summaryrefslogtreecommitdiff
path: root/lib/containers/syncmap.go
blob: fb7f59b12bfcc5cc46e404ba156e49552a29b8b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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)
}