byteslice.mx raw
1 // SPDX-License-Identifier: Unlicense OR MIT
2
3 // Byte slice views of typed slices via unsafe.Slice.
4 // Ported from gioui.org/internal/byteslice without reflect.
5
6 package gio
7
8 import "unsafe"
9
10 // Float32sToBytes returns a byte slice view of a float32 slice.
11 func Float32sToBytes(s []float32) []byte {
12 n := len(s)
13 if n == 0 {
14 return nil
15 }
16 blen := n * int(unsafe.Sizeof(s[0]))
17 return unsafe.Slice((*byte)(unsafe.Pointer(&s[0])), blen)
18 }
19
20 // Uint16sToBytes returns a byte slice view of a uint16 slice.
21 func Uint16sToBytes(s []uint16) []byte {
22 n := len(s)
23 if n == 0 {
24 return nil
25 }
26 blen := n * int(unsafe.Sizeof(s[0]))
27 return unsafe.Slice((*byte)(unsafe.Pointer(&s[0])), blen)
28 }
29
30 // Uint32sToBytes returns a byte slice view of a uint32 slice.
31 func Uint32sToBytes(s []uint32) []byte {
32 n := len(s)
33 if n == 0 {
34 return nil
35 }
36 blen := n * int(unsafe.Sizeof(s[0]))
37 return unsafe.Slice((*byte)(unsafe.Pointer(&s[0])), blen)
38 }
39