]> Cypherpunks repositories - gostls13.git/commitdiff
all: use unsafe.{Slice, SliceData, String, StringData} to simplify code
authorhopehook <hopehook.com@gmail.com>
Wed, 7 Sep 2022 14:34:48 +0000 (22:34 +0800)
committerGopher Robot <gobot@golang.org>
Thu, 8 Sep 2022 21:16:39 +0000 (21:16 +0000)
Because most of these APIs are recently supported, we can only do some
advancement work as much as possible under the premise of compatibility.

For #54854.

Change-Id: Id15d11288bf23902570d54eaf2704a5264210b2e
Reviewed-on: https://go-review.googlesource.com/c/go/+/429115
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: hopehook <hopehook@golangcn.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/cmd/go/internal/modindex/read.go
src/hash/maphash/maphash.go
src/internal/fuzz/counters_supported.go

index 707f17e1ab56147069d8dccbe078eaf236a0fa03..83a54c3ef8ed21b6df89c2c71591751cbac54b02 100644 (file)
@@ -14,7 +14,6 @@ import (
        "go/token"
        "internal/godebug"
        "internal/goroot"
-       "internal/unsafeheader"
        "path"
        "path/filepath"
        "runtime"
@@ -948,14 +947,7 @@ func (sf *sourceFile) embeds() []embed {
 }
 
 func asString(b []byte) string {
-       p := (*unsafeheader.Slice)(unsafe.Pointer(&b)).Data
-
-       var s string
-       hdr := (*unsafeheader.String)(unsafe.Pointer(&s))
-       hdr.Data = p
-       hdr.Len = len(b)
-
-       return s
+       return unsafe.String(unsafe.SliceData(b), len(b))
 }
 
 // A decoder helps decode the index format.
index dfacd021db25d9c725248fff1a3631a554758786..690068a70a29a8b1b32f0ad5b4d6d560c4000563 100644 (file)
@@ -13,7 +13,6 @@
 package maphash
 
 import (
-       "internal/unsafeheader"
        "unsafe"
 )
 
@@ -72,11 +71,11 @@ func String(seed Seed, s string) uint64 {
                panic("maphash: use of uninitialized Seed")
        }
        for len(s) > bufSize {
-               p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
+               p := (*byte)(unsafe.StringData(s))
                state = rthash(p, bufSize, state)
                s = s[bufSize:]
        }
-       p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
+       p := (*byte)(unsafe.StringData(s))
        return rthash(p, len(s), state)
 }
 
@@ -190,7 +189,7 @@ func (h *Hash) WriteString(s string) (int, error) {
        if len(s) > bufSize {
                h.initSeed()
                for len(s) > bufSize {
-                       ptr := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
+                       ptr := (*byte)(unsafe.StringData(s))
                        h.state.s = rthash(ptr, bufSize, h.state.s)
                        s = s[bufSize:]
                }
index 7ef553aaf2eded679ea1f0574d91572c3198c470..79e27d27e1af287ef57bcd6bd52a8a6d1fabd509 100644 (file)
@@ -7,7 +7,6 @@
 package fuzz
 
 import (
-       "internal/unsafeheader"
        "unsafe"
 )
 
@@ -18,12 +17,5 @@ import (
 func coverage() []byte {
        addr := unsafe.Pointer(&_counters)
        size := uintptr(unsafe.Pointer(&_ecounters)) - uintptr(addr)
-
-       var res []byte
-       *(*unsafeheader.Slice)(unsafe.Pointer(&res)) = unsafeheader.Slice{
-               Data: addr,
-               Len:  int(size),
-               Cap:  int(size),
-       }
-       return res
+       return unsafe.Slice((*byte)(addr), int(size))
 }