]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: use slice-to-array conversions
authorTobias Klauser <tklauser@distanz.ch>
Thu, 22 Sep 2022 13:09:49 +0000 (15:09 +0200)
committerGopher Robot <gobot@golang.org>
Fri, 23 Sep 2022 20:32:51 +0000 (20:32 +0000)
Use slice-to-array conversions in AddrFromSlice and
(*Addr).UnmarshalBinary. This allows allows to use AddrFrom16 and drop
the redundant ipv6Slice helper.

For #46505

Change-Id: I0e3a7d8825ad438115b7f23ee97cc74eec41a826
Reviewed-on: https://go-review.googlesource.com/c/go/+/432735
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/netip/netip.go

index b5d55acdb36d1547b55ea65ce0249cec9cdc510c..adb4117d4a0ec265272fc50e9fb62aa3c0120ba0 100644 (file)
@@ -102,18 +102,6 @@ func AddrFrom16(addr [16]byte) Addr {
        }
 }
 
-// ipv6Slice is like IPv6Raw, but operates on a 16-byte slice. Assumes
-// slice is 16 bytes, caller must enforce this.
-func ipv6Slice(addr []byte) Addr {
-       return Addr{
-               addr: uint128{
-                       beUint64(addr[:8]),
-                       beUint64(addr[8:]),
-               },
-               z: z6noz,
-       }
-}
-
 // ParseAddr parses s as an IP address, returning the result. The string
 // s can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"),
 // or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
@@ -345,9 +333,9 @@ func parseIPv6(in string) (Addr, error) {
 func AddrFromSlice(slice []byte) (ip Addr, ok bool) {
        switch len(slice) {
        case 4:
-               return AddrFrom4(*(*[4]byte)(slice)), true
+               return AddrFrom4([4]byte(slice)), true
        case 16:
-               return ipv6Slice(slice), true
+               return AddrFrom16([16]byte(slice)), true
        }
        return Addr{}, false
 }
@@ -1020,10 +1008,10 @@ func (ip *Addr) UnmarshalBinary(b []byte) error {
                *ip = AddrFrom4(*(*[4]byte)(b))
                return nil
        case n == 16:
-               *ip = ipv6Slice(b)
+               *ip = AddrFrom16([16]byte(b))
                return nil
        case n > 16:
-               *ip = ipv6Slice(b[:16]).WithZone(string(b[16:]))
+               *ip = AddrFrom16([16]byte(b[:16])).WithZone(string(b[16:]))
                return nil
        }
        return errors.New("unexpected slice size")