mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 13:18:53 +00:00
util/uniq,types/lazy,*: delete code that's now in Go std
sync.OnceValue and slices.Compact were both added in Go 1.21. cmp.Or was added in Go 1.22. Updates #8632 Updates #11058 Change-Id: I89ba4c404f40188e1f8a9566c8aaa049be377754 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
5fdb4f83ad
commit
69b90742fe
@@ -120,41 +120,6 @@ func (z *SyncValue[T]) PeekErr() (v T, err error, ok bool) {
|
||||
return zero, nil, false
|
||||
}
|
||||
|
||||
// SyncFunc wraps a function to make it lazy.
|
||||
//
|
||||
// The returned function calls fill the first time it's called, and returns
|
||||
// fill's result on every subsequent call.
|
||||
//
|
||||
// The returned function is safe for concurrent use.
|
||||
func SyncFunc[T any](fill func() T) func() T {
|
||||
var (
|
||||
once sync.Once
|
||||
v T
|
||||
)
|
||||
return func() T {
|
||||
once.Do(func() { v = fill() })
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
// SyncFuncErr wraps a function to make it lazy.
|
||||
//
|
||||
// The returned function calls fill the first time it's called, and returns
|
||||
// fill's results on every subsequent call.
|
||||
//
|
||||
// The returned function is safe for concurrent use.
|
||||
func SyncFuncErr[T any](fill func() (T, error)) func() (T, error) {
|
||||
var (
|
||||
once sync.Once
|
||||
v T
|
||||
err error
|
||||
)
|
||||
return func() (T, error) {
|
||||
once.Do(func() { v, err = fill() })
|
||||
return v, err
|
||||
}
|
||||
}
|
||||
|
||||
// TB is a subset of testing.TB that we use to set up test helpers.
|
||||
// It's defined here to avoid pulling in the testing package.
|
||||
type TB interface {
|
||||
|
@@ -354,46 +354,3 @@ func TestSyncValueSetForTest(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncFunc(t *testing.T) {
|
||||
f := SyncFunc(fortyTwo)
|
||||
|
||||
n := int(testing.AllocsPerRun(1000, func() {
|
||||
got := f()
|
||||
if got != 42 {
|
||||
t.Fatalf("got %v; want 42", got)
|
||||
}
|
||||
}))
|
||||
if n != 0 {
|
||||
t.Errorf("allocs = %v; want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncFuncErr(t *testing.T) {
|
||||
f := SyncFuncErr(func() (int, error) {
|
||||
return 42, nil
|
||||
})
|
||||
n := int(testing.AllocsPerRun(1000, func() {
|
||||
got, err := f()
|
||||
if got != 42 || err != nil {
|
||||
t.Fatalf("got %v, %v; want 42, nil", got, err)
|
||||
}
|
||||
}))
|
||||
if n != 0 {
|
||||
t.Errorf("allocs = %v; want 0", n)
|
||||
}
|
||||
|
||||
wantErr := errors.New("test error")
|
||||
f = SyncFuncErr(func() (int, error) {
|
||||
return 0, wantErr
|
||||
})
|
||||
n = int(testing.AllocsPerRun(1000, func() {
|
||||
got, err := f()
|
||||
if got != 0 || err != wantErr {
|
||||
t.Fatalf("got %v, %v; want 0, %v", got, err, wantErr)
|
||||
}
|
||||
}))
|
||||
if n != 0 {
|
||||
t.Errorf("allocs = %v; want 0", n)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user