safe_slice.go raw

   1  package reflect2
   2  
   3  import (
   4  	"reflect"
   5  	"unsafe"
   6  )
   7  
   8  type safeSliceType struct {
   9  	safeType
  10  }
  11  
  12  func (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  
  18  func (type2 *safeSliceType) UnsafeSetIndex(obj unsafe.Pointer, index int, value unsafe.Pointer) {
  19  	panic("does not support unsafe operation")
  20  }
  21  
  22  func (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  
  30  func (type2 *safeSliceType) UnsafeGetIndex(obj unsafe.Pointer, index int) unsafe.Pointer {
  31  	panic("does not support unsafe operation")
  32  }
  33  
  34  func (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  
  41  func (type2 *safeSliceType) UnsafeMakeSlice(length int, cap int) unsafe.Pointer {
  42  	panic("does not support unsafe operation")
  43  }
  44  
  45  func (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  
  54  func (type2 *safeSliceType) UnsafeGrow(ptr unsafe.Pointer, newLength int) {
  55  	panic("does not support unsafe operation")
  56  }
  57  
  58  func (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  
  65  func (type2 *safeSliceType) UnsafeAppend(obj unsafe.Pointer, elem unsafe.Pointer) {
  66  	panic("does not support unsafe operation")
  67  }
  68  
  69  func (type2 *safeSliceType) SetNil(obj interface{}) {
  70  	val := reflect.ValueOf(obj).Elem()
  71  	val.Set(reflect.Zero(val.Type()))
  72  }
  73  
  74  func (type2 *safeSliceType) UnsafeSetNil(ptr unsafe.Pointer) {
  75  	panic("does not support unsafe operation")
  76  }
  77  
  78  func (type2 *safeSliceType) LengthOf(obj interface{}) int {
  79  	return reflect.ValueOf(obj).Elem().Len()
  80  }
  81  
  82  func (type2 *safeSliceType) UnsafeLengthOf(ptr unsafe.Pointer) int {
  83  	panic("does not support unsafe operation")
  84  }
  85  
  86  func (type2 *safeSliceType) Cap(obj interface{}) int {
  87  	return reflect.ValueOf(obj).Elem().Cap()
  88  }
  89  
  90  func (type2 *safeSliceType) UnsafeCap(ptr unsafe.Pointer) int {
  91  	panic("does not support unsafe operation")
  92  }
  93