github.com/segmentio/encoding@v0.4.0/internal/runtime_reflect/slice.go (about) 1 package runtime_reflect 2 3 import "unsafe" 4 5 type Slice struct { 6 data unsafe.Pointer 7 len int 8 cap int 9 } 10 11 func (s *Slice) Cap() int { 12 return s.cap 13 } 14 15 func (s *Slice) Len() int { 16 return s.len 17 } 18 19 func (s *Slice) SetLen(n int) { 20 s.len = n 21 } 22 23 func (s *Slice) Index(i int, elemSize uintptr) unsafe.Pointer { 24 return unsafe.Pointer(uintptr(s.data) + (uintptr(i) * elemSize)) 25 } 26 27 func MakeSlice(elemType unsafe.Pointer, len, cap int) Slice { 28 return Slice{ 29 data: newarray(elemType, cap), 30 len: len, 31 cap: cap, 32 } 33 } 34 35 func CopySlice(elemType unsafe.Pointer, dst, src Slice) int { 36 return typedslicecopy(elemType, dst, src) 37 } 38 39 //go:linkname newarray runtime.newarray 40 func newarray(t unsafe.Pointer, n int) unsafe.Pointer 41 42 //go:linkname typedslicecopy runtime.typedslicecopy 43 //go:noescape 44 func typedslicecopy(t unsafe.Pointer, dst, src Slice) int