safe_slice.go 2.5 KB

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