-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.go
More file actions
29 lines (22 loc) · 746 Bytes
/
Convert.go
File metadata and controls
29 lines (22 loc) · 746 Bytes
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
//! WARNING: String in golang are immutable
//! Therefor, bytes returned from S2B MUST not be changed
//! and
//! bytes given to B2S MUST not be changed as long as the string is in use
package utils
import "unsafe"
// WARNING: DONOT MUTATE RETURNED VALUE
func S2B(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
// WARNING: DONOT MUTATE THE ENTERED VALUE AS LONG AS THE RETURNED STRING IS IN USE
func B2S(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
// WARNING: Uses unsafe casting
func PtrCast[From any, To any](val *From) *To {
return (*To)(unsafe.Pointer(val))
}
// WARNING: Uses unsafe casting
func BitCast[From any, To any](val From) To {
return *PtrCast[From, To](&val)
}