From a661287c4bc61d4b0a0b26a32498bddb75fe1f74 Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Mon, 18 Dec 2023 17:43:01 -0500 Subject: [PATCH] util/cmpx: remove code that's in the stdlib now The cmpx.Compare function (and associated interface) are now available in the standard library as cmp.Compare. Remove our version of it and use the version from the standard library. Updates #cleanup Signed-off-by: Andrew Dunham Change-Id: I4be3ac63d466c05eb7a0babb25cb0d41816fbd53 --- cmd/derper/depaware.txt | 2 +- cmd/netlogfmt/main.go | 4 ++-- cmd/tailscale/cli/exitnode.go | 4 ++-- ipn/ipnlocal/local.go | 7 ++++--- ipn/ipnlocal/profiles.go | 4 ++-- types/netmap/netmap.go | 4 ++-- types/netmap/nodemut.go | 4 ++-- util/cmpx/cmpx.go | 37 ----------------------------------- 8 files changed, 15 insertions(+), 51 deletions(-) diff --git a/cmd/derper/depaware.txt b/cmd/derper/depaware.txt index 5191768a2..e25e7b92c 100644 --- a/cmd/derper/depaware.txt +++ b/cmd/derper/depaware.txt @@ -193,7 +193,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa golang.org/x/time/rate from tailscale.com/cmd/derper+ bufio from compress/flate+ bytes from bufio+ - cmp from slices + cmp from slices+ compress/flate from compress/gzip+ compress/gzip from internal/profile+ container/list from crypto/tls+ diff --git a/cmd/netlogfmt/main.go b/cmd/netlogfmt/main.go index e1a5c2cb2..19db2797a 100644 --- a/cmd/netlogfmt/main.go +++ b/cmd/netlogfmt/main.go @@ -25,6 +25,7 @@ package main import ( + "cmp" "encoding/base64" "encoding/json" "flag" @@ -45,7 +46,6 @@ "github.com/go-json-experiment/json/jsontext" "tailscale.com/types/logid" "tailscale.com/types/netlogtype" - "tailscale.com/util/cmpx" "tailscale.com/util/must" ) @@ -155,7 +155,7 @@ func printMessage(msg message) { slices.SortFunc(traffic, func(x, y netlogtype.ConnectionCounts) int { nx := x.TxPackets + x.TxBytes + x.RxPackets + x.RxBytes ny := y.TxPackets + y.TxBytes + y.RxPackets + y.RxBytes - return cmpx.Compare(ny, nx) + return cmp.Compare(ny, nx) }) var sum netlogtype.Counts for _, cc := range traffic { diff --git a/cmd/tailscale/cli/exitnode.go b/cmd/tailscale/cli/exitnode.go index 2eb7a28ab..60fc8dc86 100644 --- a/cmd/tailscale/cli/exitnode.go +++ b/cmd/tailscale/cli/exitnode.go @@ -4,6 +4,7 @@ package cli import ( + "cmp" "context" "errors" "flag" @@ -17,7 +18,6 @@ xmaps "golang.org/x/exp/maps" "tailscale.com/ipn/ipnstate" "tailscale.com/tailcfg" - "tailscale.com/util/cmpx" ) var exitNodeCmd = &ffcli.Command{ @@ -228,7 +228,7 @@ func filterFormatAndSortExitNodes(peers []*ipnstate.PeerStatus, filterBy string) // by location.Priority, in order of highest priority. func sortPeersByPriority(peers []*ipnstate.PeerStatus) { slices.SortStableFunc(peers, func(a, b *ipnstate.PeerStatus) int { - return cmpx.Compare(b.Location.Priority, a.Location.Priority) + return cmp.Compare(b.Location.Priority, a.Location.Priority) }) } diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 5e1fd42b4..1c81ba045 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -4,6 +4,7 @@ package ipnlocal import ( + "cmp" "context" "encoding/base64" "encoding/json" @@ -1332,7 +1333,7 @@ func (b *LocalBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (handled bo nm.Peers = append(nm.Peers, p) } slices.SortFunc(nm.Peers, func(a, b tailcfg.NodeView) int { - return cmpx.Compare(a.ID(), b.ID()) + return cmp.Compare(a.ID(), b.ID()) }) notify = &ipn.Notify{NetMap: nm} } else if testenv.InTest() { @@ -1549,7 +1550,7 @@ func (b *LocalBackend) PeersForTest() []tailcfg.NodeView { defer b.mu.Unlock() ret := xmaps.Values(b.peers) slices.SortFunc(ret, func(a, b tailcfg.NodeView) int { - return cmpx.Compare(a.ID(), b.ID()) + return cmp.Compare(a.ID(), b.ID()) }) return ret } @@ -4904,7 +4905,7 @@ func (b *LocalBackend) FileTargets() ([]*apitype.FileTarget, error) { }) } slices.SortFunc(ret, func(a, b *apitype.FileTarget) int { - return cmpx.Compare(a.Node.Name, b.Node.Name) + return cmp.Compare(a.Node.Name, b.Node.Name) }) return ret, nil } diff --git a/ipn/ipnlocal/profiles.go b/ipn/ipnlocal/profiles.go index ad2482178..a7c6ab944 100644 --- a/ipn/ipnlocal/profiles.go +++ b/ipn/ipnlocal/profiles.go @@ -4,6 +4,7 @@ package ipnlocal import ( + "cmp" "encoding/json" "errors" "fmt" @@ -17,7 +18,6 @@ "tailscale.com/ipn" "tailscale.com/types/logger" "tailscale.com/util/clientmetric" - "tailscale.com/util/cmpx" ) var errAlreadyMigrated = errors.New("profile migration already completed") @@ -113,7 +113,7 @@ func (pm *profileManager) allProfiles() (out []*ipn.LoginProfile) { } } slices.SortFunc(out, func(a, b *ipn.LoginProfile) int { - return cmpx.Compare(a.Name, b.Name) + return cmp.Compare(a.Name, b.Name) }) return out } diff --git a/types/netmap/netmap.go b/types/netmap/netmap.go index 233dcc656..dcfc9604a 100644 --- a/types/netmap/netmap.go +++ b/types/netmap/netmap.go @@ -5,6 +5,7 @@ package netmap import ( + "cmp" "encoding/json" "fmt" "net/netip" @@ -16,7 +17,6 @@ "tailscale.com/tka" "tailscale.com/types/key" "tailscale.com/types/views" - "tailscale.com/util/cmpx" "tailscale.com/wgengine/filter" ) @@ -146,7 +146,7 @@ func (nm *NetworkMap) PeerIndexByNodeID(nodeID tailcfg.NodeID) int { return -1 } idx, ok := sort.Find(len(nm.Peers), func(i int) int { - return cmpx.Compare(nodeID, nm.Peers[i].ID()) + return cmp.Compare(nodeID, nm.Peers[i].ID()) }) if !ok { return -1 diff --git a/types/netmap/nodemut.go b/types/netmap/nodemut.go index e6d414f4f..69f37a162 100644 --- a/types/netmap/nodemut.go +++ b/types/netmap/nodemut.go @@ -4,6 +4,7 @@ package netmap import ( + "cmp" "fmt" "net/netip" "reflect" @@ -13,7 +14,6 @@ "tailscale.com/tailcfg" "tailscale.com/types/ptr" - "tailscale.com/util/cmpx" ) // NodeMutation is the common interface for types that describe @@ -139,7 +139,7 @@ func MutationsFromMapResponse(res *tailcfg.MapResponse, now time.Time) (ret []No } } slices.SortStableFunc(ret, func(a, b NodeMutation) int { - return cmpx.Compare(a.NodeIDBeingMutated(), b.NodeIDBeingMutated()) + return cmp.Compare(a.NodeIDBeingMutated(), b.NodeIDBeingMutated()) }) return ret, true } diff --git a/util/cmpx/cmpx.go b/util/cmpx/cmpx.go index 007d9096a..d747f0a1d 100644 --- a/util/cmpx/cmpx.go +++ b/util/cmpx/cmpx.go @@ -20,40 +20,3 @@ func Or[T comparable](list ...T) T { } return zero } - -// Ordered is cmp.Ordered from Go 1.21. -type Ordered interface { - ~int | ~int8 | ~int16 | ~int32 | ~int64 | - ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | - ~float32 | ~float64 | - ~string -} - -// Compare returns -// -// -1 if x is less than y, -// 0 if x equals y, -// +1 if x is greater than y. -// -// For floating-point types, a NaN is considered less than any non-NaN, -// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0. -func Compare[T Ordered](x, y T) int { - xNaN := isNaN(x) - yNaN := isNaN(y) - if xNaN && yNaN { - return 0 - } - if xNaN || x < y { - return -1 - } - if yNaN || x > y { - return +1 - } - return 0 -} - -// isNaN reports whether x is a NaN without requiring the math package. -// This will always return false if T is not floating-point. -func isNaN[T Ordered](x T) bool { - return x != x -}