mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-01 22:15:51 +00:00
a661287c4b
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
23 lines
654 B
Go
23 lines
654 B
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package cmpx has code that will likely land in a future version of Go, but
|
|
// we want sooner.
|
|
package cmpx
|
|
|
|
// Or returns the first non-zero element of list, or else returns the zero T.
|
|
//
|
|
// This is the proposal from
|
|
// https://github.com/golang/go/issues/60204#issuecomment-1581245334.
|
|
func Or[T comparable](list ...T) T {
|
|
// TODO(bradfitz): remove the comparable constraint so we can use this
|
|
// with funcs too and use reflect to see whether they're non-zero? 🤷♂️
|
|
var zero T
|
|
for _, v := range list {
|
|
if v != zero {
|
|
return v
|
|
}
|
|
}
|
|
return zero
|
|
}
|