2 Star 2 Fork 7

王布衣 / gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
unsafe_type.go 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
package reflect2
import (
"reflect"
"unsafe"
)
type unsafeType struct {
safeType
rtype unsafe.Pointer
ptrRType unsafe.Pointer
}
func newUnsafeType(cfg *frozenConfig, type1 reflect.Type) *unsafeType {
return &unsafeType{
safeType: safeType{
Type: type1,
cfg: cfg,
},
rtype: unpackEFace(type1).data,
ptrRType: unpackEFace(reflect.PtrTo(type1)).data,
}
}
func (type2 *unsafeType) Set(obj interface{}, val interface{}) {
objEFace := unpackEFace(obj)
assertType("Type.Set argument 1", type2.ptrRType, objEFace.rtype)
valEFace := unpackEFace(val)
assertType("Type.Set argument 2", type2.ptrRType, valEFace.rtype)
type2.UnsafeSet(objEFace.data, valEFace.data)
}
func (type2 *unsafeType) UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer) {
typedmemmove(type2.rtype, ptr, val)
}
func (type2 *unsafeType) IsNil(obj interface{}) bool {
objEFace := unpackEFace(obj)
assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype)
return type2.UnsafeIsNil(objEFace.data)
}
func (type2 *unsafeType) UnsafeIsNil(ptr unsafe.Pointer) bool {
return ptr == nil
}
func (type2 *unsafeType) UnsafeNew() unsafe.Pointer {
return unsafe_New(type2.rtype)
}
func (type2 *unsafeType) New() interface{} {
return packEFace(type2.ptrRType, type2.UnsafeNew())
}
func (type2 *unsafeType) PackEFace(ptr unsafe.Pointer) interface{} {
return packEFace(type2.ptrRType, ptr)
}
func (type2 *unsafeType) RType() uintptr {
return uintptr(type2.rtype)
}
func (type2 *unsafeType) Indirect(obj interface{}) interface{} {
objEFace := unpackEFace(obj)
assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype)
return type2.UnsafeIndirect(objEFace.data)
}
func (type2 *unsafeType) UnsafeIndirect(obj unsafe.Pointer) interface{} {
return packEFace(type2.rtype, obj)
}
func (type2 *unsafeType) LikePtr() bool {
return false
}
func assertType(where string, expectRType unsafe.Pointer, actualRType unsafe.Pointer) {
if expectRType != actualRType {
expectType := reflect.TypeOf(0)
(*iface)(unsafe.Pointer(&expectType)).data = expectRType
actualType := reflect.TypeOf(0)
(*iface)(unsafe.Pointer(&actualType)).data = actualRType
panic(where + ": expect " + expectType.String() + ", actual " + actualType.String())
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.21.2

搜索帮助