mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-06 04:42:42 +00:00
types/views: add LenIter method to slice view types
This is basically https://github.com/bradfitz/iter which was a joke but now that Go's adding range over int soonish, might as well. It simplies our code elsewher that uses slice views. Updates #8948 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
af2e4909b6
commit
261cc498d3
@@ -73,6 +73,11 @@ func (v SliceView[T, V]) IsNil() bool { return v.ж == nil }
|
||||
// Len returns the length of the slice.
|
||||
func (v SliceView[T, V]) Len() int { return len(v.ж) }
|
||||
|
||||
// LenIter returns a slice the same length as the v.Len().
|
||||
// The caller can then range over it to get the valid indexes.
|
||||
// It does not allocate.
|
||||
func (v SliceView[T, V]) LenIter() []struct{} { return make([]struct{}, len(v.ж)) }
|
||||
|
||||
// At returns a View of the element at index `i` of the slice.
|
||||
func (v SliceView[T, V]) At(i int) V { return v.ж[i].View() }
|
||||
|
||||
@@ -129,6 +134,11 @@ func (v Slice[T]) IsNil() bool { return v.ж == nil }
|
||||
// Len returns the length of the slice.
|
||||
func (v Slice[T]) Len() int { return len(v.ж) }
|
||||
|
||||
// LenIter returns a slice the same length as the v.Len().
|
||||
// The caller can then range over it to get the valid indexes.
|
||||
// It does not allocate.
|
||||
func (v Slice[T]) LenIter() []struct{} { return make([]struct{}, len(v.ж)) }
|
||||
|
||||
// At returns the element at index `i` of the slice.
|
||||
func (v Slice[T]) At(i int) T { return v.ж[i] }
|
||||
|
||||
@@ -233,6 +243,11 @@ func (v IPPrefixSlice) IsNil() bool { return v.ж.IsNil() }
|
||||
// Len returns the length of the slice.
|
||||
func (v IPPrefixSlice) Len() int { return v.ж.Len() }
|
||||
|
||||
// LenIter returns a slice the same length as the v.Len().
|
||||
// The caller can then range over it to get the valid indexes.
|
||||
// It does not allocate.
|
||||
func (v IPPrefixSlice) LenIter() []struct{} { return make([]struct{}, v.ж.Len()) }
|
||||
|
||||
// At returns the IPPrefix at index `i` of the slice.
|
||||
func (v IPPrefixSlice) At(i int) netip.Prefix { return v.ж.At(i) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user