-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
unsafe.go
51 lines (41 loc) · 1.16 KB
/
unsafe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package unsafe
import (
"unsafe"
"golang.org/x/exp/constraints"
)
func Cast[Dst, Src any](x Src) Dst {
return *(*Dst)(unsafe.Pointer(&x))
}
func SliceCast[Dst ~[]DstE, Src ~[]SrcE, DstE, SrcE any](x Src) Dst {
// We don't use our Cast helper in this function because it increases the function complexity, making inlining
// more difficult.
type sliceHeader struct {
data unsafe.Pointer
len int
cap int
}
if cap(x) == 0 {
return nil
}
// This way of getting the pointer has lower inlining complexity than &x[:1][0]
ptrDst := (*sliceHeader)(unsafe.Pointer(&x)).data
sizeSrc := unsafe.Sizeof(*new(SrcE))
sizeDst := unsafe.Sizeof(*new(DstE))
if sizeSrc >= sizeDst {
return *(*Dst)(unsafe.Pointer(&sliceHeader{
data: ptrDst,
len: len(x) * int(sizeSrc/sizeDst),
cap: cap(x) * int(sizeSrc/sizeDst),
}))
} else {
return *(*Dst)(unsafe.Pointer(&sliceHeader{
data: ptrDst,
len: len(x) / int(sizeDst/sizeSrc),
cap: cap(x) / int(sizeDst/sizeSrc),
}))
}
}
func Index[E any, S ~[]E, Int constraints.Integer](ptr S, idx Int) *E {
offset := unsafe.Sizeof(*new(E)) * uintptr(idx)
return (*E)(unsafe.Add(unsafe.Pointer(&ptr[0]), offset))
}