all: use any instead of interface{}

My favorite part of generics.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder
2022-03-16 16:27:57 -07:00
committed by Josh Bleecher Snyder
parent 5f176f24db
commit 0868329936
88 changed files with 204 additions and 204 deletions
+1 -1
View File
@@ -256,7 +256,7 @@ func EncodeLogTailMetricsDelta() string {
}
var deltaPool = &sync.Pool{
New: func() interface{} {
New: func() any {
return new(deltaEncBuf)
},
}
+1 -1
View File
@@ -79,7 +79,7 @@ func NamedTypes(pkg *packages.Package) map[string]*types.Named {
// for each package path that the caller must import for the returned code to compile.
func AssertStructUnchanged(t *types.Struct, thisPkg *types.Package, tname, ctx string, imports map[string]struct{}) []byte {
buf := new(bytes.Buffer)
w := func(format string, args ...interface{}) {
w := func(format string, args ...any) {
fmt.Fprintf(buf, format+"\n", args...)
}
w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
+4 -4
View File
@@ -113,11 +113,11 @@ func (h *hasher) sum() (s Sum) {
}
var hasherPool = &sync.Pool{
New: func() interface{} { return new(hasher) },
New: func() any { return new(hasher) },
}
// Hash returns the hash of v.
func Hash(v interface{}) (s Sum) {
func Hash(v any) (s Sum) {
h := hasherPool.Get().(*hasher)
defer hasherPool.Put(h)
h.reset()
@@ -130,7 +130,7 @@ func Hash(v interface{}) (s Sum) {
}
// Update sets last to the hash of v and reports whether its value changed.
func Update(last *Sum, v ...interface{}) (changed bool) {
func Update(last *Sum, v ...any) (changed bool) {
sum := Hash(v)
if sum == *last {
// unchanged.
@@ -304,7 +304,7 @@ type mapHasher struct {
}
var mapHasherPool = &sync.Pool{
New: func() interface{} { return new(mapHasher) },
New: func() any { return new(mapHasher) },
}
type valueCache map[reflect.Type]reflect.Value
+6 -6
View File
@@ -35,8 +35,8 @@ func (p appendBytes) AppendTo(b []byte) []byte {
}
func TestHash(t *testing.T) {
type tuple [2]interface{}
type iface struct{ X interface{} }
type tuple [2]any
type iface struct{ X any }
type scalars struct {
I8 int8
I16 int16
@@ -132,8 +132,8 @@ func TestDeepHash(t *testing.T) {
}
}
func getVal() []interface{} {
return []interface{}{
func getVal() []any {
return []any{
&wgcfg.Config{
Name: "foo",
Addresses: []netaddr.IPPrefix{netaddr.IPPrefixFrom(netaddr.IPFrom16([16]byte{3: 3}), 5)},
@@ -321,10 +321,10 @@ func TestExhaustive(t *testing.T) {
// verify this doesn't loop forever, as it used to (Issue 2340)
func TestMapCyclicFallback(t *testing.T) {
type T struct {
M map[string]interface{}
M map[string]any
}
v := &T{
M: map[string]interface{}{},
M: map[string]any{},
}
v.M["m"] = v.M
Hash(v)
+3 -3
View File
@@ -20,13 +20,13 @@ type decoder struct {
}
var readerPool = sync.Pool{
New: func() interface{} {
New: func() any {
return bytes.NewReader(nil)
},
}
var decoderPool = sync.Pool{
New: func() interface{} {
New: func() any {
var d decoder
d.r = readerPool.Get().(*bytes.Reader)
d.dec = json.NewDecoder(d.r)
@@ -47,7 +47,7 @@ var decoderPool = sync.Pool{
// don't use this Unmarshal.
//
// This Unmarshal allocates considerably less memory.
func Unmarshal(b []byte, v interface{}) error {
func Unmarshal(b []byte, v any) error {
d := decoderPool.Get().(*decoder)
d.r.Reset(b)
off := d.dec.InputOffset()
+3 -3
View File
@@ -27,7 +27,7 @@ func TestCompareToStd(t *testing.T) {
for _, test := range tests {
b := []byte(test)
var ourV, stdV interface{}
var ourV, stdV any
ourErr := Unmarshal(b, &ourV)
stdErr := json.Unmarshal(b, &stdV)
if (ourErr == nil) != (stdErr == nil) {
@@ -47,7 +47,7 @@ func TestCompareToStd(t *testing.T) {
}
func BenchmarkUnmarshal(b *testing.B) {
var m interface{}
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
@@ -56,7 +56,7 @@ func BenchmarkUnmarshal(b *testing.B) {
}
func BenchmarkStdUnmarshal(b *testing.B) {
var m interface{}
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
+1 -1
View File
@@ -78,7 +78,7 @@ func (e Error) Is(target error) bool {
// As finds the first error in e that matches target, and if any is found,
// sets target to that error value and returns true. Otherwise, it returns false.
func (e Error) As(target interface{}) bool {
func (e Error) As(target any) bool {
for _, err := range e.errs {
if ok := errors.As(err, target); ok {
return true
+2 -2
View File
@@ -25,7 +25,7 @@ type logOnce struct {
sync.Once
}
func (l *logOnce) logf(format string, args ...interface{}) {
func (l *logOnce) logf(format string, args ...any) {
l.Once.Do(func() {
log.Printf(format, args...)
})
@@ -71,7 +71,7 @@ func Ready() {
// CPU: 2min 38.469s
// CGroup: /system.slice/tailscale.service
// └─26741 /nix/store/sv6cj4mw2jajm9xkbwj07k29dj30lh0n-tailscale-date.20200727/bin/tailscaled --port 41641
func Status(format string, args ...interface{}) {
func Status(format string, args ...any) {
err := notifier().Notify(sdnotify.Statusf(format, args...))
if err != nil {
statusOnce.logf("systemd: error notifying: %v", err)
+2 -2
View File
@@ -7,5 +7,5 @@
package systemd
func Ready() {}
func Status(string, ...interface{}) {}
func Ready() {}
func Status(string, ...any) {}
+1 -1
View File
@@ -23,7 +23,7 @@ func (e badTypeError) Error() string {
// It adjusts the length of the slice appropriately and zeros the tail.
// eq reports whether (*sliceptr)[i] and (*sliceptr)[j] are equal.
// ModifySlice does O(len(*sliceptr)) operations.
func ModifySlice(sliceptr interface{}, eq func(i, j int) bool) {
func ModifySlice(sliceptr any, eq func(i, j int) bool) {
rvp := reflect.ValueOf(sliceptr)
if rvp.Type().Kind() != reflect.Ptr {
panic(badTypeError{rvp.Type()})