github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/unsafe.go (about)

     1  package don
     2  
     3  import "unsafe"
     4  
     5  //go:linkname unsafe_New reflect.unsafe_New
     6  func unsafe_New(unsafe.Pointer) unsafe.Pointer //nolint:revive
     7  
     8  //go:linkname typedmemmove reflect.typedmemmove
     9  func typedmemmove(t, dst, src unsafe.Pointer)
    10  
    11  // emptyInterface is the header for an interface{} value.
    12  type emptyInterface struct {
    13  	typ unsafe.Pointer
    14  	ptr unsafe.Pointer
    15  }
    16  
    17  func dataOf(v any) unsafe.Pointer {
    18  	return (*emptyInterface)(unsafe.Pointer(&v)).ptr
    19  }
    20  
    21  func packEface(typ, ptr unsafe.Pointer) any {
    22  	var i any
    23  	e := (*emptyInterface)(unsafe.Pointer(&i))
    24  	e.typ = typ
    25  	e.ptr = ptr
    26  	return i
    27  }