blob: bcce6fd20e5cfa99137433e84e033eaf7e162a82 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type safeSliceType struct {
9 safeType
10}
11
12func (type2 *safeSliceType) SetIndex(obj interface{}, index int, value interface{}) {
13 val := reflect.ValueOf(obj).Elem()
14 elem := reflect.ValueOf(value).Elem()
15 val.Index(index).Set(elem)
16}
17
18func (type2 *safeSliceType) UnsafeSetIndex(obj unsafe.Pointer, index int, value unsafe.Pointer) {
19 panic("does not support unsafe operation")
20}
21
22func (type2 *safeSliceType) GetIndex(obj interface{}, index int) interface{} {
23 val := reflect.ValueOf(obj).Elem()
24 elem := val.Index(index)
25 ptr := reflect.New(elem.Type())
26 ptr.Elem().Set(elem)
27 return ptr.Interface()
28}
29
30func (type2 *safeSliceType) UnsafeGetIndex(obj unsafe.Pointer, index int) unsafe.Pointer {
31 panic("does not support unsafe operation")
32}
33
34func (type2 *safeSliceType) MakeSlice(length int, cap int) interface{} {
35 val := reflect.MakeSlice(type2.Type, length, cap)
36 ptr := reflect.New(val.Type())
37 ptr.Elem().Set(val)
38 return ptr.Interface()
39}
40
41func (type2 *safeSliceType) UnsafeMakeSlice(length int, cap int) unsafe.Pointer {
42 panic("does not support unsafe operation")
43}
44
45func (type2 *safeSliceType) Grow(obj interface{}, newLength int) {
46 oldCap := type2.Cap(obj)
47 oldSlice := reflect.ValueOf(obj).Elem()
48 delta := newLength - oldCap
49 deltaVals := make([]reflect.Value, delta)
50 newSlice := reflect.Append(oldSlice, deltaVals...)
51 oldSlice.Set(newSlice)
52}
53
54func (type2 *safeSliceType) UnsafeGrow(ptr unsafe.Pointer, newLength int) {
55 panic("does not support unsafe operation")
56}
57
58func (type2 *safeSliceType) Append(obj interface{}, elem interface{}) {
59 val := reflect.ValueOf(obj).Elem()
60 elemVal := reflect.ValueOf(elem).Elem()
61 newVal := reflect.Append(val, elemVal)
62 val.Set(newVal)
63}
64
65func (type2 *safeSliceType) UnsafeAppend(obj unsafe.Pointer, elem unsafe.Pointer) {
66 panic("does not support unsafe operation")
67}
68
69func (type2 *safeSliceType) SetNil(obj interface{}) {
70 val := reflect.ValueOf(obj).Elem()
71 val.Set(reflect.Zero(val.Type()))
72}
73
74func (type2 *safeSliceType) UnsafeSetNil(ptr unsafe.Pointer) {
75 panic("does not support unsafe operation")
76}
77
78func (type2 *safeSliceType) LengthOf(obj interface{}) int {
79 return reflect.ValueOf(obj).Elem().Len()
80}
81
82func (type2 *safeSliceType) UnsafeLengthOf(ptr unsafe.Pointer) int {
83 panic("does not support unsafe operation")
84}
85
86func (type2 *safeSliceType) Cap(obj interface{}) int {
87 return reflect.ValueOf(obj).Elem().Cap()
88}
89
90func (type2 *safeSliceType) UnsafeCap(ptr unsafe.Pointer) int {
91 panic("does not support unsafe operation")
92}