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 <james@tailscale.com>
This commit is contained in:
James Tucker 2023-04-27 17:09:30 -07:00 committed by James Tucker
parent 042f82ea32
commit b3c3a9f174
1 changed files with 7 additions and 0 deletions

View File

@ -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 }