blob: 5eb53130a20980ff9a150b60d3086b57077d9ddb [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type UnsafeStructField struct {
9 reflect.StructField
10 structType *UnsafeStructType
11 rtype unsafe.Pointer
12 ptrRType unsafe.Pointer
13}
14
15func newUnsafeStructField(structType *UnsafeStructType, structField reflect.StructField) *UnsafeStructField {
16 return &UnsafeStructField{
17 StructField: structField,
18 rtype: unpackEFace(structField.Type).data,
19 ptrRType: unpackEFace(reflect.PtrTo(structField.Type)).data,
20 structType: structType,
21 }
22}
23
24func (field *UnsafeStructField) Offset() uintptr {
25 return field.StructField.Offset
26}
27
28func (field *UnsafeStructField) Name() string {
29 return field.StructField.Name
30}
31
32func (field *UnsafeStructField) PkgPath() string {
33 return field.StructField.PkgPath
34}
35
36func (field *UnsafeStructField) Type() Type {
37 return field.structType.cfg.Type2(field.StructField.Type)
38}
39
40func (field *UnsafeStructField) Tag() reflect.StructTag {
41 return field.StructField.Tag
42}
43
44func (field *UnsafeStructField) Index() []int {
45 return field.StructField.Index
46}
47
48func (field *UnsafeStructField) Anonymous() bool {
49 return field.StructField.Anonymous
50}
51
52func (field *UnsafeStructField) Set(obj interface{}, value interface{}) {
53 objEFace := unpackEFace(obj)
54 assertType("StructField.SetIndex argument 1", field.structType.ptrRType, objEFace.rtype)
55 valueEFace := unpackEFace(value)
56 assertType("StructField.SetIndex argument 2", field.ptrRType, valueEFace.rtype)
57 field.UnsafeSet(objEFace.data, valueEFace.data)
58}
59
60func (field *UnsafeStructField) UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer) {
61 fieldPtr := add(obj, field.StructField.Offset, "same as non-reflect &v.field")
62 typedmemmove(field.rtype, fieldPtr, value)
63}
64
65func (field *UnsafeStructField) Get(obj interface{}) interface{} {
66 objEFace := unpackEFace(obj)
67 assertType("StructField.GetIndex argument 1", field.structType.ptrRType, objEFace.rtype)
68 value := field.UnsafeGet(objEFace.data)
69 return packEFace(field.ptrRType, value)
70}
71
72func (field *UnsafeStructField) UnsafeGet(obj unsafe.Pointer) unsafe.Pointer {
73 return add(obj, field.StructField.Offset, "same as non-reflect &v.field")
74}