summaryrefslogtreecommitdiff
path: root/android/util.go
diff options
context:
space:
mode:
author Colin Cross <ccross@android.com> 2024-01-02 19:44:52 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2024-01-02 19:44:52 +0000
commita3759f98a63eb2ab4e0c6620d2e27d3434ee6ec0 (patch)
tree005d4dc8d8c8b0b178467f18a959b31cb7f2a8dd /android/util.go
parent64c9e0ce6e4aa183b2fae40a18e3e5435c49229d (diff)
parent31a674571e53f12dffb97698e65b9eee6ae0ebbc (diff)
Merge "Write raw files to disk instead of the ninja file" into main
Diffstat (limited to 'android/util.go')
-rw-r--r--android/util.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/android/util.go b/android/util.go
index ae1c65756..51313ceec 100644
--- a/android/util.go
+++ b/android/util.go
@@ -22,6 +22,7 @@ import (
"runtime"
"sort"
"strings"
+ "sync"
)
// CopyOf returns a new slice that has the same contents as s.
@@ -597,3 +598,32 @@ func AddToStringSet(set map[string]bool, items []string) {
set[item] = true
}
}
+
+// SyncMap is a wrapper around sync.Map that provides type safety via generics.
+type SyncMap[K comparable, V any] struct {
+ sync.Map
+}
+
+// Load returns the value stored in the map for a key, or the zero value if no
+// value is present.
+// The ok result indicates whether value was found in the map.
+func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
+ v, ok := m.Map.Load(key)
+ if !ok {
+ return *new(V), false
+ }
+ return v.(V), true
+}
+
+// Store sets the value for a key.
+func (m *SyncMap[K, V]) Store(key K, value V) {
+ m.Map.Store(key, value)
+}
+
+// LoadOrStore returns the existing value for the key if present.
+// Otherwise, it stores and returns the given value.
+// The loaded result is true if the value was loaded, false if stored.
+func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
+ v, loaded := m.Map.LoadOrStore(key, value)
+ return v.(V), loaded
+}