blob: 804d9166397bb34b841b3081773a381c0758ca50 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type UnsafeStructType struct {
9 unsafeType
10 likePtr bool
11}
12
13func newUnsafeStructType(cfg *frozenConfig, type1 reflect.Type) *UnsafeStructType {
14 return &UnsafeStructType{
15 unsafeType: *newUnsafeType(cfg, type1),
16 likePtr: likePtrType(type1),
17 }
18}
19
20func (type2 *UnsafeStructType) LikePtr() bool {
21 return type2.likePtr
22}
23
24func (type2 *UnsafeStructType) Indirect(obj interface{}) interface{} {
25 objEFace := unpackEFace(obj)
26 assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype)
27 return type2.UnsafeIndirect(objEFace.data)
28}
29
30func (type2 *UnsafeStructType) UnsafeIndirect(ptr unsafe.Pointer) interface{} {
31 if type2.likePtr {
32 return packEFace(type2.rtype, *(*unsafe.Pointer)(ptr))
33 }
34 return packEFace(type2.rtype, ptr)
35}
36
37func (type2 *UnsafeStructType) FieldByName(name string) StructField {
38 structField, found := type2.Type.FieldByName(name)
39 if !found {
40 return nil
41 }
42 return newUnsafeStructField(type2, structField)
43}
44
45func (type2 *UnsafeStructType) Field(i int) StructField {
46 return newUnsafeStructField(type2, type2.Type.Field(i))
47}
48
49func (type2 *UnsafeStructType) FieldByIndex(index []int) StructField {
50 return newUnsafeStructField(type2, type2.Type.FieldByIndex(index))
51}
52
53func (type2 *UnsafeStructType) FieldByNameFunc(match func(string) bool) StructField {
54 structField, found := type2.Type.FieldByNameFunc(match)
55 if !found {
56 panic("field match condition not found in " + type2.Type.String())
57 }
58 return newUnsafeStructField(type2, structField)
59}