v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/slice/index.go (about)

     1  package slice
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // UnsafeIndex returns nth element of s.
     9  //nolint:unsafeptr
    10  func UnsafeIndex[T any](s []T, n uintptr) T {
    11  	d := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data)
    12  	var zero T
    13  	return *(*T)(unsafe.Add(d, n*unsafe.Sizeof(zero)))
    14  }