-
Notifications
You must be signed in to change notification settings - Fork 0
/
ununsafe.go
66 lines (58 loc) · 1.64 KB
/
ununsafe.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ununsafe
import (
"fmt"
"unsafe"
)
// StringToBytes converts string to []byte without copy.
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
// BytesToString converts []byte to string without copy.
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
// SizeOf is a wrapper of unsafe.Sizeof.
func SizeOf[T any]() uint64 {
var t T
return uint64(unsafe.Sizeof(t))
}
func ValueToValue[From, To any](from From) To {
fromSize := SizeOf[From]()
toSize := SizeOf[To]()
if fromSize != toSize {
panic(fmt.Sprintf("ununsafe.ScalarToScalar: size mismatch %d != %d", fromSize, toSize))
}
return *(*To)(unsafe.Pointer(&from))
}
func SliceToValue[From, To any](arr []From) To {
l := uint64(len(arr))
fromSize := SizeOf[From]()
toSize := SizeOf[To]()
if l*fromSize != toSize {
panic(fmt.Sprintf("ununsafe.VectorToScalar: size mismatch %d*%d != %d", l, fromSize, toSize))
}
return *(*To)(unsafe.Pointer(unsafe.SliceData(arr)))
}
func ValueToSlice[From, To any](from From) []To {
fromSize := SizeOf[From]()
toSize := SizeOf[To]()
if fromSize%toSize != 0 {
panic(fmt.Sprintf("ununsafe.ScalarToVector: size mismatch %d%%%d != 0", fromSize, toSize))
}
return unsafe.Slice(
(*To)(unsafe.Pointer(&from)),
fromSize/toSize,
)
}
func SliceToSlice[From, To any](arr []From) []To {
l := uint64(len(arr))
fromSize := SizeOf[From]()
toSize := SizeOf[To]()
if l*fromSize%toSize != 0 {
panic(fmt.Sprintf("ununsafe.VectorToVector: size mismatch %d*%d%%%d != 0", l, fromSize, toSize))
}
return unsafe.Slice(
(*To)(unsafe.Pointer(unsafe.SliceData(arr))),
l*fromSize/toSize,
)
}