diff options
author | 2024-01-02 19:44:52 +0000 | |
---|---|---|
committer | 2024-01-02 19:44:52 +0000 | |
commit | a3759f98a63eb2ab4e0c6620d2e27d3434ee6ec0 (patch) | |
tree | 005d4dc8d8c8b0b178467f18a959b31cb7f2a8dd /android/util.go | |
parent | 64c9e0ce6e4aa183b2fae40a18e3e5435c49229d (diff) | |
parent | 31a674571e53f12dffb97698e65b9eee6ae0ebbc (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.go | 30 |
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 +} |