mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-23 17:18:42 +00:00
types/views: add iterators to the three Map view types
Their callers using Range are all kinda clunky feeling. Iterators should make them more readable. Updates #12912 Change-Id: I93461eba8e735276fda4a8558a4ae4bfd6c04922 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
f6d4d03355
commit
94c79659fa
@@ -440,6 +440,17 @@ func (m MapSlice[K, V]) AsMap() map[K][]V {
|
||||
return out
|
||||
}
|
||||
|
||||
// All returns an iterator iterating over the keys and values of m.
|
||||
func (m MapSlice[K, V]) All() iter.Seq2[K, Slice[V]] {
|
||||
return func(yield func(K, Slice[V]) bool) {
|
||||
for k, v := range m.ж {
|
||||
if !yield(k, SliceOf(v)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Map provides a read-only view of a map. It is the caller's responsibility to
|
||||
// make sure V is immutable.
|
||||
type Map[K comparable, V any] struct {
|
||||
@@ -526,6 +537,18 @@ func (m Map[K, V]) Range(f MapRangeFn[K, V]) {
|
||||
}
|
||||
}
|
||||
|
||||
// All returns an iterator iterating over the keys
|
||||
// and values of m.
|
||||
func (m Map[K, V]) All() iter.Seq2[K, V] {
|
||||
return func(yield func(K, V) bool) {
|
||||
for k, v := range m.ж {
|
||||
if !yield(k, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MapFnOf returns a MapFn for m.
|
||||
func MapFnOf[K comparable, T any, V any](m map[K]T, f func(T) V) MapFn[K, T, V] {
|
||||
return MapFn[K, T, V]{
|
||||
@@ -587,6 +610,17 @@ func (m MapFn[K, T, V]) Range(f MapRangeFn[K, V]) {
|
||||
}
|
||||
}
|
||||
|
||||
// All returns an iterator iterating over the keys and value views of m.
|
||||
func (m MapFn[K, T, V]) All() iter.Seq2[K, V] {
|
||||
return func(yield func(K, V) bool) {
|
||||
for k, v := range m.ж {
|
||||
if !yield(k, m.wrapv(v)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ContainsPointers reports whether T contains any pointers,
|
||||
// either explicitly or implicitly.
|
||||
// It has special handling for some types that contain pointers
|
||||
|
Reference in New Issue
Block a user