util/set: add OfSliceView constructor and Set.AddSliceView

Building a Set from a views.Slice previously required set.Of(v.AsSlice()...),
which allocates an intermediate slice copy before allocating the set. Add
OfSliceView and AddSliceView to populate a set directly from the view,
mirroring the existing AddSlice/AddSeq/AddSet family.

Updates tailscale/corp#45499

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I3f2a9d417c60be8e5f1acd42708e2f9a4d6c1b7e
This commit is contained in:
Brad Fitzpatrick
2026-07-27 16:30:30 -07:00
committed by Brad Fitzpatrick
parent e48e7b730a
commit d0b4d44963
2 changed files with 30 additions and 0 deletions
+14
View File
@@ -7,6 +7,8 @@ import (
"encoding/json"
"slices"
"testing"
"tailscale.com/types/views"
)
func TestSet(t *testing.T) {
@@ -98,6 +100,18 @@ func TestSetOf(t *testing.T) {
}
}
func TestOfSliceView(t *testing.T) {
s := OfSliceView(views.SliceOf([]int{1, 2, 3, 4, 4, 1}))
if s.Len() != 4 {
t.Errorf("wrong len %d; want 4", s.Len())
}
for _, n := range []int{1, 2, 3, 4} {
if !s.Contains(n) {
t.Errorf("should contain %d", n)
}
}
}
func TestEqual(t *testing.T) {
type test struct {
name string