Browse Source

syncs: add Map.Len to get the length of the Map

I need this for a corp change where I have a set as a queue, and make a
different decisison if the set is empty.

Updates tailscale/corp#10344

Signed-off-by: James Tucker <[email protected]>
James Tucker 2 years ago
parent
commit
b3c3a9f174
1 changed files with 7 additions and 0 deletions
  1. 7 0
      syncs/syncs.go

+ 7 - 0
syncs/syncs.go

@@ -220,6 +220,13 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
 	}
 }
 
+// Len returns the length of the map.
+func (m *Map[K, V]) Len() int {
+	m.mu.RLock()
+	defer m.mu.RUnlock()
+	return len(m.m)
+}
+
 // WaitGroup is identical to [sync.WaitGroup],
 // but provides a Go method to start a goroutine.
 type WaitGroup struct{ sync.WaitGroup }