1
0
mirror of https://github.com/golang/go synced 2024-11-12 04:00:23 -07:00

net/netip: add Addr.AsSlice() method

We have AddrFrom4, AddrFrom6, AddrFromSlice and As4, As6, but we are
missing AsSlice, so this commit adds the missing function. It also gets
rid of the less ergonomic and inconsistently named IPAddrParts.

Updates #49298.

Change-Id: I1c6a2c32fc6c69b244ab49765412ffe3bbe7e5c2
Reviewed-on: https://go-review.googlesource.com/c/go/+/360874
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Jason A. Donenfeld 2021-11-02 20:44:32 +01:00 committed by Brad Fitzpatrick
parent 35a588109b
commit d3a80c795e

View File

@ -448,31 +448,6 @@ func (ip Addr) Less(ip2 Addr) bool { return ip.Compare(ip2) == -1 }
func (ip Addr) lessOrEq(ip2 Addr) bool { return ip.Compare(ip2) <= 0 }
// ipZone returns the standard library net.IP from ip, as well
// as the zone.
// The optional reuse IP provides memory to reuse.
func (ip Addr) ipZone(reuse []byte) (stdIP []byte, zone string) {
base := reuse[:0]
switch {
case ip.z == z0:
return nil, ""
case ip.Is4():
a4 := ip.As4()
return append(base, a4[:]...), ""
default:
a16 := ip.As16()
return append(base, a16[:]...), ip.Zone()
}
}
// IPAddrParts returns the net.IPAddr representation of an Addr.
//
// The slice will be nil if ip is the zero Addr.
// The zone is the empty string if there is no zone.
func (ip Addr) IPAddrParts() (slice []byte, zone string) {
return ip.ipZone(nil)
}
// Is4 reports whether ip is an IPv4 address.
//
// It returns false for IP4-mapped IPv6 addresses. See IP.Unmap.
@ -718,6 +693,23 @@ func (ip Addr) As4() (a4 [4]byte) {
panic("As4 called on IPv6 address")
}
// AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.
func (ip Addr) AsSlice() []byte {
switch ip.z {
case z0:
return nil
case z4:
var ret [4]byte
bePutUint32(ret[:], uint32(ip.addr.lo))
return ret[:]
default:
var ret [16]byte
bePutUint64(ret[:8], ip.addr.hi)
bePutUint64(ret[8:], ip.addr.lo)
return ret[:]
}
}
// Next returns the address following ip.
// If there is none, it returns the zero Addr.
func (ip Addr) Next() Addr {