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 <andrew@du.nham.ca>
Change-Id: I4be3ac63d466c05eb7a0babb25cb0d41816fbd53
pull/10650/head
Andrew Dunham 11 months ago
parent 945cf836ee
commit a661287c4b

@ -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+

@ -25,6 +25,7 @@
package main
import (
"cmp"
"encoding/base64"
"encoding/json"
"flag"
@ -45,7 +46,6 @@ import (
"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 {

@ -4,6 +4,7 @@
package cli
import (
"cmp"
"context"
"errors"
"flag"
@ -17,7 +18,6 @@ import (
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)
})
}

@ -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
}

@ -4,6 +4,7 @@
package ipnlocal
import (
"cmp"
"encoding/json"
"errors"
"fmt"
@ -17,7 +18,6 @@ import (
"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
}

@ -5,6 +5,7 @@
package netmap
import (
"cmp"
"encoding/json"
"fmt"
"net/netip"
@ -16,7 +17,6 @@ import (
"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

@ -4,6 +4,7 @@
package netmap
import (
"cmp"
"fmt"
"net/netip"
"reflect"
@ -13,7 +14,6 @@ import (
"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
}

@ -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
}

Loading…
Cancel
Save