From 197a4f1ae86170720a4233c16478fea9d359d0e9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 30 Nov 2022 17:33:29 -0800 Subject: [PATCH] types/ptr: move all the ptrTo funcs to one new package's ptr.To Change-Id: Ia0b820ffe7aa72897515f19bd415204b6fe743c7 Signed-off-by: Brad Fitzpatrick --- cmd/derper/depaware.txt | 1 + cmd/tailscale/cli/set_test.go | 35 +++++++++++++++---------------- cmd/tailscale/depaware.txt | 1 + cmd/tailscaled/depaware.txt | 1 + control/controlclient/map_test.go | 21 ++++++++----------- hostinfo/hostinfo.go | 5 ++--- hostinfo/hostinfo_freebsd.go | 5 +++-- hostinfo/hostinfo_linux.go | 5 +++-- hostinfo/hostinfo_windows.go | 5 +++-- types/ptr/ptr.go | 11 ++++++++++ util/deephash/deephash_test.go | 11 +++++----- 11 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 types/ptr/ptr.go diff --git a/cmd/derper/depaware.txt b/cmd/derper/depaware.txt index ab5c4e405..cff4bdf52 100644 --- a/cmd/derper/depaware.txt +++ b/cmd/derper/depaware.txt @@ -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/persist 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/tkatype from tailscale.com/types/key+ tailscale.com/types/views from tailscale.com/ipn/ipnstate+ diff --git a/cmd/tailscale/cli/set_test.go b/cmd/tailscale/cli/set_test.go index 013896a4a..d74f0cc43 100644 --- a/cmd/tailscale/cli/set_test.go +++ b/cmd/tailscale/cli/set_test.go @@ -11,10 +11,9 @@ import ( "tailscale.com/ipn" "tailscale.com/net/tsaddr" + "tailscale.com/types/ptr" ) -func ptrTo[T any](v T) *T { return &v } - func TestCalcAdvertiseRoutesForSet(t *testing.T) { pfx := netip.MustParsePrefix tests := []struct { @@ -29,80 +28,80 @@ func TestCalcAdvertiseRoutesForSet(t *testing.T) { }, { name: "advertise-exit", - setExit: ptrTo(true), + setExit: ptr.To(true), want: tsaddr.ExitRoutes(), }, { name: "advertise-exit/already-routes", 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()}, }, { name: "advertise-exit/already-exit", was: tsaddr.ExitRoutes(), - setExit: ptrTo(true), + setExit: ptr.To(true), want: tsaddr.ExitRoutes(), }, { name: "stop-advertise-exit", was: tsaddr.ExitRoutes(), - setExit: ptrTo(false), + setExit: ptr.To(false), want: nil, }, { name: "stop-advertise-exit/with-routes", 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")}, }, { 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")}, }, { name: "advertise-routes/already-exit", 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()}, }, { name: "advertise-routes/already-diff-routes", 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")}, }, { name: "stop-advertise-routes", was: []netip.Prefix{pfx("34.0.0.0/16")}, - setRoutes: ptrTo(""), + setRoutes: ptr.To(""), want: nil, }, { name: "stop-advertise-routes/already-exit", was: []netip.Prefix{pfx("34.0.0.0/16"), tsaddr.AllIPv4(), tsaddr.AllIPv6()}, - setRoutes: ptrTo(""), + setRoutes: ptr.To(""), want: tsaddr.ExitRoutes(), }, { name: "advertise-routes-and-exit", - setExit: ptrTo(true), - setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), + setExit: ptr.To(true), + 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()}, }, { name: "advertise-routes-and-exit/already-exit", was: tsaddr.ExitRoutes(), - setExit: ptrTo(true), - setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), + setExit: ptr.To(true), + 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()}, }, { name: "advertise-routes-and-exit/already-routes", was: []netip.Prefix{pfx("10.0.0.0/24"), pfx("192.168.0.0/16")}, - setExit: ptrTo(true), - setRoutes: ptrTo("10.0.0.0/24,192.168.0.0/16"), + setExit: ptr.To(true), + 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()}, }, } diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 286a9238c..d0933cdb1 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -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/persist from tailscale.com/ipn 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/tkatype from tailscale.com/types/key+ tailscale.com/types/views from tailscale.com/tailcfg+ diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 051bdc0bd..edf3b331c 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -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/persist from tailscale.com/control/controlclient+ 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/tkatype from tailscale.com/tka+ tailscale.com/types/views from tailscale.com/ipn/ipnlocal+ diff --git a/control/controlclient/map_test.go b/control/controlclient/map_test.go index af1895878..07e0caa3b 100644 --- a/control/controlclient/map_test.go +++ b/control/controlclient/map_test.go @@ -17,6 +17,7 @@ import ( "tailscale.com/types/key" "tailscale.com/types/netmap" "tailscale.com/types/opt" + "tailscale.com/types/ptr" "tailscale.com/util/must" ) @@ -201,7 +202,7 @@ func TestUndeltaPeers(t *testing.T) { mapRes: &tailcfg.MapResponse{ PeersChangedPatch: []*tailcfg.PeerChange{{ 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{ ID: 1, @@ -229,7 +230,7 @@ func TestUndeltaPeers(t *testing.T) { mapRes: &tailcfg.MapResponse{ PeersChangedPatch: []*tailcfg.PeerChange{{ 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{ ID: 1, @@ -243,12 +244,12 @@ func TestUndeltaPeers(t *testing.T) { mapRes: &tailcfg.MapResponse{ PeersChangedPatch: []*tailcfg.PeerChange{{ NodeID: 1, - Online: ptrTo(true), + Online: ptr.To(true), }}, }, want: peers(&tailcfg.Node{ ID: 1, Name: "foo", - Online: ptrTo(true), + Online: ptr.To(true), }), }, { @@ -257,12 +258,12 @@ func TestUndeltaPeers(t *testing.T) { mapRes: &tailcfg.MapResponse{ PeersChangedPatch: []*tailcfg.PeerChange{{ NodeID: 1, - LastSeen: ptrTo(time.Unix(123, 0).UTC()), + LastSeen: ptr.To(time.Unix(123, 0).UTC()), }}, }, want: peers(&tailcfg.Node{ ID: 1, 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{ PeersChangedPatch: []*tailcfg.PeerChange{{ NodeID: 1, - KeyExpiry: ptrTo(time.Unix(123, 0).UTC()), + KeyExpiry: ptr.To(time.Unix(123, 0).UTC()), }}, }, want: peers(&tailcfg.Node{ ID: 1, @@ -285,7 +286,7 @@ func TestUndeltaPeers(t *testing.T) { mapRes: &tailcfg.MapResponse{ PeersChangedPatch: []*tailcfg.PeerChange{{ NodeID: 1, - Capabilities: ptrTo([]string{"foo"}), + Capabilities: ptr.To([]string{"foo"}), }}, }, want: peers(&tailcfg.Node{ 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 { var sb strings.Builder for i, n := range nodes { diff --git a/hostinfo/hostinfo.go b/hostinfo/hostinfo.go index 8d626400c..60cf3edc0 100644 --- a/hostinfo/hostinfo.go +++ b/hostinfo/hostinfo.go @@ -20,6 +20,7 @@ import ( "tailscale.com/envknob" "tailscale.com/tailcfg" "tailscale.com/types/opt" + "tailscale.com/types/ptr" "tailscale.com/util/cloudenv" "tailscale.com/util/dnsname" "tailscale.com/util/lineread" @@ -70,11 +71,9 @@ func condCall[T any](fn func() T) T { } 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 { // f is a pointer to a fill function. If it's nil or points // to nil, then Get returns the zero value for T. diff --git a/hostinfo/hostinfo_freebsd.go b/hostinfo/hostinfo_freebsd.go index e5d84aab3..e3383d4f4 100644 --- a/hostinfo/hostinfo_freebsd.go +++ b/hostinfo/hostinfo_freebsd.go @@ -12,6 +12,7 @@ import ( "os/exec" "golang.org/x/sys/unix" + "tailscale.com/types/ptr" "tailscale.com/version/distro" ) @@ -22,8 +23,8 @@ func init() { } var ( - lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptrTo(freebsdVersionMeta)} - lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionFreeBSD)} + lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptr.To(freebsdVersionMeta)} + lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionFreeBSD)} ) func distroNameFreeBSD() string { diff --git a/hostinfo/hostinfo_linux.go b/hostinfo/hostinfo_linux.go index 724fe62f9..d83bfa903 100644 --- a/hostinfo/hostinfo_linux.go +++ b/hostinfo/hostinfo_linux.go @@ -12,6 +12,7 @@ import ( "strings" "golang.org/x/sys/unix" + "tailscale.com/types/ptr" "tailscale.com/util/lineread" "tailscale.com/util/strs" "tailscale.com/version/distro" @@ -29,8 +30,8 @@ func init() { } var ( - lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptrTo(linuxVersionMeta)} - lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionLinux)} + lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptr.To(linuxVersionMeta)} + lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionLinux)} ) type versionMeta struct { diff --git a/hostinfo/hostinfo_windows.go b/hostinfo/hostinfo_windows.go index b41aa230f..1a4b50f45 100644 --- a/hostinfo/hostinfo_windows.go +++ b/hostinfo/hostinfo_windows.go @@ -11,6 +11,7 @@ import ( "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" + "tailscale.com/types/ptr" "tailscale.com/util/winutil" ) @@ -20,8 +21,8 @@ func init() { } var ( - lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionWindows)} - lazyPackageType = &lazyAtomicValue[string]{f: ptrTo(packageTypeWindows)} + lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionWindows)} + lazyPackageType = &lazyAtomicValue[string]{f: ptr.To(packageTypeWindows)} ) func osVersionWindows() string { diff --git a/types/ptr/ptr.go b/types/ptr/ptr.go new file mode 100644 index 000000000..8a454d1f6 --- /dev/null +++ b/types/ptr/ptr.go @@ -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 +} diff --git a/util/deephash/deephash_test.go b/util/deephash/deephash_test.go index c864f829f..089e2b62d 100644 --- a/util/deephash/deephash_test.go +++ b/util/deephash/deephash_test.go @@ -27,6 +27,7 @@ import ( "tailscale.com/types/dnstype" "tailscale.com/types/ipproto" "tailscale.com/types/key" + "tailscale.com/types/ptr" "tailscale.com/util/deephash/testtype" "tailscale.com/util/dnsname" "tailscale.com/version" @@ -485,7 +486,7 @@ func TestGetTypeHasher(t *testing.T) { }, { 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), }, { @@ -515,7 +516,7 @@ func TestGetTypeHasher(t *testing.T) { }, { 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", }, { @@ -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 // first element) and also a few entries that are the typical shape for regular // packet filters as sent to clients. @@ -753,7 +752,7 @@ var filterRules = []tailcfg.FilterRule{ SrcBits: []int{1, 2, 3}, DstPorts: []tailcfg.NetPortRange{{ IP: "1.2.3.4/32", - Bits: ptrTo(32), + Bits: ptr.To(32), Ports: tailcfg.PortRange{First: 1, Last: 2}, }}, IPProto: []int{1, 2, 3, 4}, @@ -936,7 +935,7 @@ func TestHashThroughView(t *testing.T) { SSHPolicy: &sshPolicyOut{ Rules: []tailcfg.SSHRuleView{ (&tailcfg.SSHRule{ - RuleExpires: ptrTo(time.Unix(123, 0)), + RuleExpires: ptr.To(time.Unix(123, 0)), }).View(), }, },