Skip to content

Commit a9a3982

Browse files
hopehookgopherbot
authored andcommitted
all: use unsafe.{Slice, SliceData, String, StringData} to simplify code
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 <[email protected]> Run-TryBot: hopehook <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 2c3187c commit a9a3982

File tree

3 files changed

+5
-22
lines changed

3 files changed

+5
-22
lines changed

src/cmd/go/internal/modindex/read.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"go/token"
1515
"internal/godebug"
1616
"internal/goroot"
17-
"internal/unsafeheader"
1817
"path"
1918
"path/filepath"
2019
"runtime"
@@ -948,14 +947,7 @@ func (sf *sourceFile) embeds() []embed {
948947
}
949948

950949
func asString(b []byte) string {
951-
p := (*unsafeheader.Slice)(unsafe.Pointer(&b)).Data
952-
953-
var s string
954-
hdr := (*unsafeheader.String)(unsafe.Pointer(&s))
955-
hdr.Data = p
956-
hdr.Len = len(b)
957-
958-
return s
950+
return unsafe.String(unsafe.SliceData(b), len(b))
959951
}
960952

961953
// A decoder helps decode the index format.

src/hash/maphash/maphash.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package maphash
1414

1515
import (
16-
"internal/unsafeheader"
1716
"unsafe"
1817
)
1918

@@ -72,11 +71,11 @@ func String(seed Seed, s string) uint64 {
7271
panic("maphash: use of uninitialized Seed")
7372
}
7473
for len(s) > bufSize {
75-
p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
74+
p := (*byte)(unsafe.StringData(s))
7675
state = rthash(p, bufSize, state)
7776
s = s[bufSize:]
7877
}
79-
p := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
78+
p := (*byte)(unsafe.StringData(s))
8079
return rthash(p, len(s), state)
8180
}
8281

@@ -190,7 +189,7 @@ func (h *Hash) WriteString(s string) (int, error) {
190189
if len(s) > bufSize {
191190
h.initSeed()
192191
for len(s) > bufSize {
193-
ptr := (*byte)((*unsafeheader.String)(unsafe.Pointer(&s)).Data)
192+
ptr := (*byte)(unsafe.StringData(s))
194193
h.state.s = rthash(ptr, bufSize, h.state.s)
195194
s = s[bufSize:]
196195
}

src/internal/fuzz/counters_supported.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package fuzz
88

99
import (
10-
"internal/unsafeheader"
1110
"unsafe"
1211
)
1312

@@ -18,12 +17,5 @@ import (
1817
func coverage() []byte {
1918
addr := unsafe.Pointer(&_counters)
2019
size := uintptr(unsafe.Pointer(&_ecounters)) - uintptr(addr)
21-
22-
var res []byte
23-
*(*unsafeheader.Slice)(unsafe.Pointer(&res)) = unsafeheader.Slice{
24-
Data: addr,
25-
Len: int(size),
26-
Cap: int(size),
27-
}
28-
return res
20+
return unsafe.Slice((*byte)(addr), int(size))
2921
}

0 commit comments

Comments
 (0)