types/ptr: move all the ptrTo funcs to one new package's ptr.To

Change-Id: Ia0b820ffe7aa72897515f19bd415204b6fe743c7
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2022-11-30 17:33:29 -08:00 committed by Brad Fitzpatrick
parent a277eb4dcf
commit 197a4f1ae8
11 changed files with 56 additions and 45 deletions

View File

@ -69,6 +69,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
tailscale.com/types/opt from tailscale.com/client/tailscale+ tailscale.com/types/opt from tailscale.com/client/tailscale+
tailscale.com/types/persist from tailscale.com/ipn tailscale.com/types/persist from tailscale.com/ipn
tailscale.com/types/preftype from tailscale.com/ipn tailscale.com/types/preftype from tailscale.com/ipn
tailscale.com/types/ptr from tailscale.com/hostinfo
tailscale.com/types/structs from tailscale.com/ipn+ tailscale.com/types/structs from tailscale.com/ipn+
tailscale.com/types/tkatype from tailscale.com/types/key+ tailscale.com/types/tkatype from tailscale.com/types/key+
tailscale.com/types/views from tailscale.com/ipn/ipnstate+ tailscale.com/types/views from tailscale.com/ipn/ipnstate+

View File

@ -11,10 +11,9 @@
"tailscale.com/ipn" "tailscale.com/ipn"
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/types/ptr"
) )
func ptrTo[T any](v T) *T { return &v }
func TestCalcAdvertiseRoutesForSet(t *testing.T) { func TestCalcAdvertiseRoutesForSet(t *testing.T) {
pfx := netip.MustParsePrefix pfx := netip.MustParsePrefix
tests := []struct { tests := []struct {
@ -29,80 +28,80 @@ func TestCalcAdvertiseRoutesForSet(t *testing.T) {
}, },
{ {
name: "advertise-exit", name: "advertise-exit",
setExit: ptrTo(true), setExit: ptr.To(true),
want: tsaddr.ExitRoutes(), want: tsaddr.ExitRoutes(),
}, },
{ {
name: "advertise-exit/already-routes", name: "advertise-exit/already-routes",
was: []netip.Prefix{pfx("34.0.0.0/16")}, was: []netip.Prefix{pfx("34.0.0.0/16")},
setExit: ptrTo(true), setExit: ptr.To(true),
want: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, want: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
}, },
{ {
name: "advertise-exit/already-exit", name: "advertise-exit/already-exit",
was: tsaddr.ExitRoutes(), was: tsaddr.ExitRoutes(),
setExit: ptrTo(true), setExit: ptr.To(true),
want: tsaddr.ExitRoutes(), want: tsaddr.ExitRoutes(),
}, },
{ {
name: "stop-advertise-exit", name: "stop-advertise-exit",
was: tsaddr.ExitRoutes(), was: tsaddr.ExitRoutes(),
setExit: ptrTo(false), setExit: ptr.To(false),
want: nil, want: nil,
}, },
{ {
name: "stop-advertise-exit/with-routes", name: "stop-advertise-exit/with-routes",
was: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, was: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
setExit: ptrTo(false), setExit: ptr.To(false),
want: []netip.Prefix{pfx("34.0.0.0/16")}, want: []netip.Prefix{pfx("34.0.0.0/16")},
}, },
{ {
name: "advertise-routes", name: "advertise-routes",
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")},
}, },
{ {
name: "advertise-routes/already-exit", name: "advertise-routes/already-exit",
was: tsaddr.ExitRoutes(), was: tsaddr.ExitRoutes(),
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
}, },
{ {
name: "advertise-routes/already-diff-routes", name: "advertise-routes/already-diff-routes",
was: []netip.Prefix{pfx("34.0.0.0/16")}, was: []netip.Prefix{pfx("34.0.0.0/16")},
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")},
}, },
{ {
name: "stop-advertise-routes", name: "stop-advertise-routes",
was: []netip.Prefix{pfx("34.0.0.0/16")}, was: []netip.Prefix{pfx("34.0.0.0/16")},
setRoutes: ptrTo(""), setRoutes: ptr.To(""),
want: nil, want: nil,
}, },
{ {
name: "stop-advertise-routes/already-exit", name: "stop-advertise-routes/already-exit",
was: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, was: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
setRoutes: ptrTo(""), setRoutes: ptr.To(""),
want: tsaddr.ExitRoutes(), want: tsaddr.ExitRoutes(),
}, },
{ {
name: "advertise-routes-and-exit", name: "advertise-routes-and-exit",
setExit: ptrTo(true), setExit: ptr.To(true),
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
}, },
{ {
name: "advertise-routes-and-exit/already-exit", name: "advertise-routes-and-exit/already-exit",
was: tsaddr.ExitRoutes(), was: tsaddr.ExitRoutes(),
setExit: ptrTo(true), setExit: ptr.To(true),
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
}, },
{ {
name: "advertise-routes-and-exit/already-routes", name: "advertise-routes-and-exit/already-routes",
was: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")}, was: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")},
setExit: ptrTo(true), setExit: ptr.To(true),
setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), setRoutes: ptr.To("10.0.0.0/24,192.168.0.0/16"),
want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, want: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()},
}, },
} }

View File

@ -98,6 +98,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
tailscale.com/types/opt from tailscale.com/net/netcheck+ tailscale.com/types/opt from tailscale.com/net/netcheck+
tailscale.com/types/persist from tailscale.com/ipn tailscale.com/types/persist from tailscale.com/ipn
tailscale.com/types/preftype from tailscale.com/cmd/tailscale/cli+ tailscale.com/types/preftype from tailscale.com/cmd/tailscale/cli+
tailscale.com/types/ptr from tailscale.com/hostinfo
tailscale.com/types/structs from tailscale.com/ipn+ tailscale.com/types/structs from tailscale.com/ipn+
tailscale.com/types/tkatype from tailscale.com/types/key+ tailscale.com/types/tkatype from tailscale.com/types/key+
tailscale.com/types/views from tailscale.com/tailcfg+ tailscale.com/types/views from tailscale.com/tailcfg+

View File

@ -277,6 +277,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/types/opt from tailscale.com/control/controlclient+ tailscale.com/types/opt from tailscale.com/control/controlclient+
tailscale.com/types/persist from tailscale.com/control/controlclient+ tailscale.com/types/persist from tailscale.com/control/controlclient+
tailscale.com/types/preftype from tailscale.com/ipn+ tailscale.com/types/preftype from tailscale.com/ipn+
tailscale.com/types/ptr from tailscale.com/hostinfo+
tailscale.com/types/structs from tailscale.com/control/controlclient+ tailscale.com/types/structs from tailscale.com/control/controlclient+
tailscale.com/types/tkatype from tailscale.com/tka+ tailscale.com/types/tkatype from tailscale.com/tka+
tailscale.com/types/views from tailscale.com/ipn/ipnlocal+ tailscale.com/types/views from tailscale.com/ipn/ipnlocal+

View File

@ -17,6 +17,7 @@
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
"tailscale.com/types/opt" "tailscale.com/types/opt"
"tailscale.com/types/ptr"
"tailscale.com/util/must" "tailscale.com/util/must"
) )
@ -201,7 +202,7 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
Key: ptrTo(key.NodePublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))), Key: ptr.To(key.NodePublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
@ -229,7 +230,7 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
DiscoKey: ptrTo(key.DiscoPublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))), DiscoKey: ptr.To(key.DiscoPublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
@ -243,12 +244,12 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
Online: ptrTo(true), Online: ptr.To(true),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
Name: "foo", Name: "foo",
Online: ptrTo(true), Online: ptr.To(true),
}), }),
}, },
{ {
@ -257,12 +258,12 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
LastSeen: ptrTo(time.Unix(123, 0).UTC()), LastSeen: ptr.To(time.Unix(123, 0).UTC()),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
Name: "foo", Name: "foo",
LastSeen: ptrTo(time.Unix(123, 0).UTC()), LastSeen: ptr.To(time.Unix(123, 0).UTC()),
}), }),
}, },
{ {
@ -271,7 +272,7 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
KeyExpiry: ptrTo(time.Unix(123, 0).UTC()), KeyExpiry: ptr.To(time.Unix(123, 0).UTC()),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
@ -285,7 +286,7 @@ func TestUndeltaPeers(t *testing.T) {
mapRes: &tailcfg.MapResponse{ mapRes: &tailcfg.MapResponse{
PeersChangedPatch: []*tailcfg.PeerChange{{ PeersChangedPatch: []*tailcfg.PeerChange{{
NodeID: 1, NodeID: 1,
Capabilities: ptrTo([]string{"foo"}), Capabilities: ptr.To([]string{"foo"}),
}}, }},
}, want: peers(&tailcfg.Node{ }, want: peers(&tailcfg.Node{
ID: 1, ID: 1,
@ -307,10 +308,6 @@ func TestUndeltaPeers(t *testing.T) {
} }
} }
func ptrTo[T any](v T) *T {
return &v
}
func formatNodes(nodes []*tailcfg.Node) string { func formatNodes(nodes []*tailcfg.Node) string {
var sb strings.Builder var sb strings.Builder
for i, n := range nodes { for i, n := range nodes {

View File

@ -20,6 +20,7 @@
"tailscale.com/envknob" "tailscale.com/envknob"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/opt" "tailscale.com/types/opt"
"tailscale.com/types/ptr"
"tailscale.com/util/cloudenv" "tailscale.com/util/cloudenv"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"tailscale.com/util/lineread" "tailscale.com/util/lineread"
@ -70,11 +71,9 @@ func condCall[T any](fn func() T) T {
} }
var ( var (
lazyInContainer = &lazyAtomicValue[opt.Bool]{f: ptrTo(inContainer)} lazyInContainer = &lazyAtomicValue[opt.Bool]{f: ptr.To(inContainer)}
) )
func ptrTo[T any](v T) *T { return &v }
type lazyAtomicValue[T any] struct { type lazyAtomicValue[T any] struct {
// f is a pointer to a fill function. If it's nil or points // f is a pointer to a fill function. If it's nil or points
// to nil, then Get returns the zero value for T. // to nil, then Get returns the zero value for T.

View File

@ -12,6 +12,7 @@
"os/exec" "os/exec"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"tailscale.com/types/ptr"
"tailscale.com/version/distro" "tailscale.com/version/distro"
) )
@ -22,8 +23,8 @@ func init() {
} }
var ( var (
lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptrTo(freebsdVersionMeta)} lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptr.To(freebsdVersionMeta)}
lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionFreeBSD)} lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionFreeBSD)}
) )
func distroNameFreeBSD() string { func distroNameFreeBSD() string {

View File

@ -12,6 +12,7 @@
"strings" "strings"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"tailscale.com/types/ptr"
"tailscale.com/util/lineread" "tailscale.com/util/lineread"
"tailscale.com/util/strs" "tailscale.com/util/strs"
"tailscale.com/version/distro" "tailscale.com/version/distro"
@ -29,8 +30,8 @@ func init() {
} }
var ( var (
lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptrTo(linuxVersionMeta)} lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptr.To(linuxVersionMeta)}
lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionLinux)} lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionLinux)}
) )
type versionMeta struct { type versionMeta struct {

View File

@ -11,6 +11,7 @@
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry" "golang.org/x/sys/windows/registry"
"tailscale.com/types/ptr"
"tailscale.com/util/winutil" "tailscale.com/util/winutil"
) )
@ -20,8 +21,8 @@ func init() {
} }
var ( var (
lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionWindows)} lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionWindows)}
lazyPackageType = &lazyAtomicValue[string]{f: ptrTo(packageTypeWindows)} lazyPackageType = &lazyAtomicValue[string]{f: ptr.To(packageTypeWindows)}
) )
func osVersionWindows() string { func osVersionWindows() string {

11
types/ptr/ptr.go Normal file
View File

@ -0,0 +1,11 @@
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package ptr contains the ptr.To function.
package ptr
// To returns a pointer to a shallow copy of v.
func To[T any](v T) *T {
return &v
}

View File

@ -27,6 +27,7 @@
"tailscale.com/types/dnstype" "tailscale.com/types/dnstype"
"tailscale.com/types/ipproto" "tailscale.com/types/ipproto"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/ptr"
"tailscale.com/util/deephash/testtype" "tailscale.com/util/deephash/testtype"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"tailscale.com/version" "tailscale.com/version"
@ -485,7 +486,7 @@ func TestGetTypeHasher(t *testing.T) {
}, },
{ {
name: "time_ptr", // addressable, as opposed to "time" test above name: "time_ptr", // addressable, as opposed to "time" test above
val: ptrTo(time.Unix(1234, 5678).In(time.UTC)), val: ptr.To(time.Unix(1234, 5678).In(time.UTC)),
out: u8(1) + u64(1234) + u32(5678) + u32(0), out: u8(1) + u64(1234) + u32(5678) + u32(0),
}, },
{ {
@ -515,7 +516,7 @@ func TestGetTypeHasher(t *testing.T) {
}, },
{ {
name: "array_ptr_memhash", name: "array_ptr_memhash",
val: ptrTo([4]byte{1, 2, 3, 4}), val: ptr.To([4]byte{1, 2, 3, 4}),
out: "\x01\x01\x02\x03\x04", out: "\x01\x01\x02\x03\x04",
}, },
{ {
@ -742,8 +743,6 @@ func BenchmarkHash(b *testing.B) {
} }
} }
func ptrTo[T any](v T) *T { return &v }
// filterRules is a packet filter that has both everything populated (in its // filterRules is a packet filter that has both everything populated (in its
// first element) and also a few entries that are the typical shape for regular // first element) and also a few entries that are the typical shape for regular
// packet filters as sent to clients. // packet filters as sent to clients.
@ -753,7 +752,7 @@ func ptrTo[T any](v T) *T { return &v }
SrcBits: []int{1, 2, 3}, SrcBits: []int{1, 2, 3},
DstPorts: []tailcfg.NetPortRange{{ DstPorts: []tailcfg.NetPortRange{{
IP: "1.2.3.4/32", IP: "1.2.3.4/32",
Bits: ptrTo(32), Bits: ptr.To(32),
Ports: tailcfg.PortRange{First: 1, Last: 2}, Ports: tailcfg.PortRange{First: 1, Last: 2},
}}, }},
IPProto: []int{1, 2, 3, 4}, IPProto: []int{1, 2, 3, 4},
@ -936,7 +935,7 @@ type mapResponseOut struct {
SSHPolicy: &sshPolicyOut{ SSHPolicy: &sshPolicyOut{
Rules: []tailcfg.SSHRuleView{ Rules: []tailcfg.SSHRuleView{
(&tailcfg.SSHRule{ (&tailcfg.SSHRule{
RuleExpires: ptrTo(time.Unix(123, 0)), RuleExpires: ptr.To(time.Unix(123, 0)),
}).View(), }).View(),
}, },
}, },