helper_unsafe.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // +build !safe
  2. // +build !appengine
  3. // +build go1.7
  4. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  5. // Use of this source code is governed by a MIT license found in the LICENSE file.
  6. package codec
  7. import (
  8. "reflect"
  9. "sync/atomic"
  10. "time"
  11. "unsafe"
  12. )
  13. // This file has unsafe variants of some helper methods.
  14. // NOTE: See helper_not_unsafe.go for the usage information.
  15. // var zeroRTv [4]uintptr
  16. const safeMode = false
  17. const unsafeFlagIndir = 1 << 7 // keep in sync with GO_ROOT/src/reflect/value.go
  18. type unsafeString struct {
  19. Data uintptr
  20. Len int
  21. }
  22. type unsafeSlice struct {
  23. Data uintptr
  24. Len int
  25. Cap int
  26. }
  27. type unsafeIntf struct {
  28. typ unsafe.Pointer
  29. word unsafe.Pointer
  30. }
  31. type unsafeReflectValue struct {
  32. typ unsafe.Pointer
  33. ptr unsafe.Pointer
  34. flag uintptr
  35. }
  36. func stringView(v []byte) string {
  37. if len(v) == 0 {
  38. return ""
  39. }
  40. bx := (*unsafeSlice)(unsafe.Pointer(&v))
  41. sx := unsafeString{bx.Data, bx.Len}
  42. return *(*string)(unsafe.Pointer(&sx))
  43. }
  44. func bytesView(v string) []byte {
  45. if len(v) == 0 {
  46. return zeroByteSlice
  47. }
  48. sx := (*unsafeString)(unsafe.Pointer(&v))
  49. bx := unsafeSlice{sx.Data, sx.Len, sx.Len}
  50. return *(*[]byte)(unsafe.Pointer(&bx))
  51. }
  52. func definitelyNil(v interface{}) bool {
  53. // There is no global way of checking if an interface is nil.
  54. // For true references (map, ptr, func, chan), you can just look
  55. // at the word of the interface. However, for slices, you have to dereference
  56. // the word, and get a pointer to the 3-word interface value.
  57. // var ui *unsafeIntf = (*unsafeIntf)(unsafe.Pointer(&v))
  58. // var word unsafe.Pointer = ui.word
  59. // // fmt.Printf(">>>> definitely nil: isnil: %v, TYPE: \t%T, word: %v, *word: %v, type: %v, nil: %v\n", v == nil, v, word, *((*unsafe.Pointer)(word)), ui.typ, nil)
  60. // return word == nil // || *((*unsafe.Pointer)(word)) == nil
  61. return ((*unsafeIntf)(unsafe.Pointer(&v))).word == nil
  62. }
  63. // func keepAlive4BytesView(v string) {
  64. // runtime.KeepAlive(v)
  65. // }
  66. // func keepAlive4StringView(v []byte) {
  67. // runtime.KeepAlive(v)
  68. // }
  69. // TODO: consider a more generally-known optimization for reflect.Value ==> Interface
  70. //
  71. // Currently, we use this fragile method that taps into implememtation details from
  72. // the source go stdlib reflect/value.go,
  73. // and trims the implementation.
  74. func rv2i(rv reflect.Value) interface{} {
  75. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  76. // true references (map, func, chan, ptr - NOT slice) may be double-referenced as flagIndir
  77. var ptr unsafe.Pointer
  78. // kk := reflect.Kind(urv.flag & (1<<5 - 1))
  79. // if (kk == reflect.Map || kk == reflect.Ptr || kk == reflect.Chan || kk == reflect.Func) && urv.flag&unsafeFlagIndir != 0 {
  80. if refBitset.isset(byte(urv.flag&(1<<5-1))) && urv.flag&unsafeFlagIndir != 0 {
  81. ptr = *(*unsafe.Pointer)(urv.ptr)
  82. } else {
  83. ptr = urv.ptr
  84. }
  85. return *(*interface{})(unsafe.Pointer(&unsafeIntf{typ: urv.typ, word: ptr}))
  86. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  87. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  88. }
  89. func rt2id(rt reflect.Type) uintptr {
  90. return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  91. }
  92. func rv2rtid(rv reflect.Value) uintptr {
  93. return uintptr((*unsafeReflectValue)(unsafe.Pointer(&rv)).typ)
  94. }
  95. // func rv0t(rt reflect.Type) reflect.Value {
  96. // ut := (*unsafeIntf)(unsafe.Pointer(&rt))
  97. // // we need to determine whether ifaceIndir, and then whether to just pass 0 as the ptr
  98. // uv := unsafeReflectValue{ut.word, &zeroRTv, flag(rt.Kind())}
  99. // return *(*reflect.Value)(unsafe.Pointer(&uv})
  100. // }
  101. // --------------------------
  102. type atomicTypeInfoSlice struct {
  103. v unsafe.Pointer
  104. }
  105. func (x *atomicTypeInfoSlice) load() *[]rtid2ti {
  106. return (*[]rtid2ti)(atomic.LoadPointer(&x.v))
  107. }
  108. func (x *atomicTypeInfoSlice) store(p *[]rtid2ti) {
  109. atomic.StorePointer(&x.v, unsafe.Pointer(p))
  110. }
  111. // --------------------------
  112. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  113. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  114. // if urv.flag&unsafeFlagIndir != 0 {
  115. // urv.ptr = *(*unsafe.Pointer)(urv.ptr)
  116. // }
  117. *(*[]byte)(urv.ptr) = d.rawBytes()
  118. }
  119. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  120. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  121. *(*string)(urv.ptr) = d.d.DecodeString()
  122. }
  123. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  124. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  125. *(*bool)(urv.ptr) = d.d.DecodeBool()
  126. }
  127. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  128. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  129. *(*time.Time)(urv.ptr) = d.d.DecodeTime()
  130. }
  131. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  132. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  133. *(*float32)(urv.ptr) = float32(d.d.DecodeFloat(true))
  134. }
  135. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  136. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  137. *(*float64)(urv.ptr) = d.d.DecodeFloat(false)
  138. }
  139. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  140. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  141. *(*int)(urv.ptr) = int(d.d.DecodeInt(intBitsize))
  142. }
  143. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  144. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  145. *(*int8)(urv.ptr) = int8(d.d.DecodeInt(8))
  146. }
  147. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  148. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  149. *(*int16)(urv.ptr) = int16(d.d.DecodeInt(16))
  150. }
  151. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  152. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  153. *(*int32)(urv.ptr) = int32(d.d.DecodeInt(32))
  154. }
  155. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  156. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  157. *(*int64)(urv.ptr) = d.d.DecodeInt(64)
  158. }
  159. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  160. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  161. *(*uint)(urv.ptr) = uint(d.d.DecodeUint(uintBitsize))
  162. }
  163. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  164. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  165. *(*uintptr)(urv.ptr) = uintptr(d.d.DecodeUint(uintBitsize))
  166. }
  167. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  168. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  169. *(*uint8)(urv.ptr) = uint8(d.d.DecodeUint(8))
  170. }
  171. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  172. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  173. *(*uint16)(urv.ptr) = uint16(d.d.DecodeUint(16))
  174. }
  175. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  176. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  177. *(*uint32)(urv.ptr) = uint32(d.d.DecodeUint(32))
  178. }
  179. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  180. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  181. *(*uint64)(urv.ptr) = d.d.DecodeUint(64)
  182. }
  183. // ------------
  184. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  185. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  186. e.e.EncodeBool(*(*bool)(v.ptr))
  187. }
  188. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  189. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  190. e.e.EncodeTime(*(*time.Time)(v.ptr))
  191. }
  192. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  193. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  194. e.e.EncodeString(cUTF8, *(*string)(v.ptr))
  195. }
  196. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  197. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  198. e.e.EncodeFloat64(*(*float64)(v.ptr))
  199. }
  200. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  201. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  202. e.e.EncodeFloat32(*(*float32)(v.ptr))
  203. }
  204. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  205. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  206. e.e.EncodeInt(int64(*(*int)(v.ptr)))
  207. }
  208. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  209. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  210. e.e.EncodeInt(int64(*(*int8)(v.ptr)))
  211. }
  212. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  213. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  214. e.e.EncodeInt(int64(*(*int16)(v.ptr)))
  215. }
  216. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  217. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  218. e.e.EncodeInt(int64(*(*int32)(v.ptr)))
  219. }
  220. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  221. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  222. e.e.EncodeInt(int64(*(*int64)(v.ptr)))
  223. }
  224. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  225. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  226. e.e.EncodeUint(uint64(*(*uint)(v.ptr)))
  227. }
  228. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  229. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  230. e.e.EncodeUint(uint64(*(*uint8)(v.ptr)))
  231. }
  232. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  233. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  234. e.e.EncodeUint(uint64(*(*uint16)(v.ptr)))
  235. }
  236. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  237. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  238. e.e.EncodeUint(uint64(*(*uint32)(v.ptr)))
  239. }
  240. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  241. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  242. e.e.EncodeUint(uint64(*(*uint64)(v.ptr)))
  243. }
  244. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  245. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  246. e.e.EncodeUint(uint64(*(*uintptr)(v.ptr)))
  247. }
  248. // ------------
  249. // func rt2id(rt reflect.Type) uintptr {
  250. // return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  251. // // var i interface{} = rt
  252. // // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  253. // // return ((*unsafeIntf)(unsafe.Pointer(&i))).word
  254. // }
  255. // func rv2i(rv reflect.Value) interface{} {
  256. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  257. // // non-reference type: already indir
  258. // // reference type: depend on flagIndir property ('cos maybe was double-referenced)
  259. // // const (unsafeRvFlagKindMask = 1<<5 - 1 , unsafeRvFlagIndir = 1 << 7 )
  260. // // rvk := reflect.Kind(urv.flag & (1<<5 - 1))
  261. // // if (rvk == reflect.Chan ||
  262. // // rvk == reflect.Func ||
  263. // // rvk == reflect.Interface ||
  264. // // rvk == reflect.Map ||
  265. // // rvk == reflect.Ptr ||
  266. // // rvk == reflect.UnsafePointer) && urv.flag&(1<<8) != 0 {
  267. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  268. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  269. // // }
  270. // if urv.flag&(1<<5-1) == uintptr(reflect.Map) && urv.flag&(1<<7) != 0 {
  271. // // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type())
  272. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  273. // }
  274. // // fmt.Printf(">>>>> ++++ direct reference: %v, %v\n", rvk, rv.Type())
  275. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  276. // }
  277. // const (
  278. // unsafeRvFlagKindMask = 1<<5 - 1
  279. // unsafeRvKindDirectIface = 1 << 5
  280. // unsafeRvFlagIndir = 1 << 7
  281. // unsafeRvFlagAddr = 1 << 8
  282. // unsafeRvFlagMethod = 1 << 9
  283. // _USE_RV_INTERFACE bool = false
  284. // _UNSAFE_RV_DEBUG = true
  285. // )
  286. // type unsafeRtype struct {
  287. // _ [2]uintptr
  288. // _ uint32
  289. // _ uint8
  290. // _ uint8
  291. // _ uint8
  292. // kind uint8
  293. // _ [2]uintptr
  294. // _ int32
  295. // }
  296. // func _rv2i(rv reflect.Value) interface{} {
  297. // // Note: From use,
  298. // // - it's never an interface
  299. // // - the only calls here are for ifaceIndir types.
  300. // // (though that conditional is wrong)
  301. // // To know for sure, we need the value of t.kind (which is not exposed).
  302. // //
  303. // // Need to validate the path: type is indirect ==> only value is indirect ==> default (value is direct)
  304. // // - Type indirect, Value indirect: ==> numbers, boolean, slice, struct, array, string
  305. // // - Type Direct, Value indirect: ==> map???
  306. // // - Type Direct, Value direct: ==> pointers, unsafe.Pointer, func, chan, map
  307. // //
  308. // // TRANSLATES TO:
  309. // // if typeIndirect { } else if valueIndirect { } else { }
  310. // //
  311. // // Since we don't deal with funcs, then "flagNethod" is unset, and can be ignored.
  312. // if _USE_RV_INTERFACE {
  313. // return rv.Interface()
  314. // }
  315. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  316. // // if urv.flag&unsafeRvFlagMethod != 0 || urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  317. // // println("***** IS flag method or interface: delegating to rv.Interface()")
  318. // // return rv.Interface()
  319. // // }
  320. // // if urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) {
  321. // // println("***** IS Interface: delegate to rv.Interface")
  322. // // return rv.Interface()
  323. // // }
  324. // // if urv.flag&unsafeRvFlagKindMask&unsafeRvKindDirectIface == 0 {
  325. // // if urv.flag&unsafeRvFlagAddr == 0 {
  326. // // println("***** IS ifaceIndir typ")
  327. // // // ui := unsafeIntf{word: urv.ptr, typ: urv.typ}
  328. // // // return *(*interface{})(unsafe.Pointer(&ui))
  329. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  330. // // }
  331. // // } else if urv.flag&unsafeRvFlagIndir != 0 {
  332. // // println("***** IS flagindir")
  333. // // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  334. // // } else {
  335. // // println("***** NOT flagindir")
  336. // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  337. // // }
  338. // // println("***** default: delegate to rv.Interface")
  339. // urt := (*unsafeRtype)(unsafe.Pointer(urv.typ))
  340. // if _UNSAFE_RV_DEBUG {
  341. // fmt.Printf(">>>> start: %v: ", rv.Type())
  342. // fmt.Printf("%v - %v\n", *urv, *urt)
  343. // }
  344. // if urt.kind&unsafeRvKindDirectIface == 0 {
  345. // if _UNSAFE_RV_DEBUG {
  346. // fmt.Printf("**** +ifaceIndir type: %v\n", rv.Type())
  347. // }
  348. // // println("***** IS ifaceIndir typ")
  349. // // if true || urv.flag&unsafeRvFlagAddr == 0 {
  350. // // // println(" ***** IS NOT addr")
  351. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  352. // // }
  353. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  354. // if _UNSAFE_RV_DEBUG {
  355. // fmt.Printf("**** +flagIndir type: %v\n", rv.Type())
  356. // }
  357. // // println("***** IS flagindir")
  358. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ}))
  359. // } else {
  360. // if _UNSAFE_RV_DEBUG {
  361. // fmt.Printf("**** -flagIndir type: %v\n", rv.Type())
  362. // }
  363. // // println("***** NOT flagindir")
  364. // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ}))
  365. // }
  366. // // println("***** default: delegating to rv.Interface()")
  367. // // return rv.Interface()
  368. // }
  369. // var staticM0 = make(map[string]uint64)
  370. // var staticI0 = (int32)(-5)
  371. // func staticRv2iTest() {
  372. // i0 := (int32)(-5)
  373. // m0 := make(map[string]uint16)
  374. // m0["1"] = 1
  375. // for _, i := range []interface{}{
  376. // (int)(7),
  377. // (uint)(8),
  378. // (int16)(-9),
  379. // (uint16)(19),
  380. // (uintptr)(77),
  381. // (bool)(true),
  382. // float32(-32.7),
  383. // float64(64.9),
  384. // complex(float32(19), 5),
  385. // complex(float64(-32), 7),
  386. // [4]uint64{1, 2, 3, 4},
  387. // (chan<- int)(nil), // chan,
  388. // rv2i, // func
  389. // io.Writer(ioutil.Discard),
  390. // make(map[string]uint),
  391. // (map[string]uint)(nil),
  392. // staticM0,
  393. // m0,
  394. // &m0,
  395. // i0,
  396. // &i0,
  397. // &staticI0,
  398. // &staticM0,
  399. // []uint32{6, 7, 8},
  400. // "abc",
  401. // Raw{},
  402. // RawExt{},
  403. // &Raw{},
  404. // &RawExt{},
  405. // unsafe.Pointer(&i0),
  406. // } {
  407. // i2 := rv2i(reflect.ValueOf(i))
  408. // eq := reflect.DeepEqual(i, i2)
  409. // fmt.Printf(">>>> %v == %v? %v\n", i, i2, eq)
  410. // }
  411. // // os.Exit(0)
  412. // }
  413. // func init() {
  414. // staticRv2iTest()
  415. // }
  416. // func rv2i(rv reflect.Value) interface{} {
  417. // if _USE_RV_INTERFACE || rv.Kind() == reflect.Interface || rv.CanAddr() {
  418. // return rv.Interface()
  419. // }
  420. // // var i interface{}
  421. // // ui := (*unsafeIntf)(unsafe.Pointer(&i))
  422. // var ui unsafeIntf
  423. // urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  424. // // fmt.Printf("urv: flag: %b, typ: %b, ptr: %b\n", urv.flag, uintptr(urv.typ), uintptr(urv.ptr))
  425. // if (urv.flag&unsafeRvFlagKindMask)&unsafeRvKindDirectIface == 0 {
  426. // if urv.flag&unsafeRvFlagAddr != 0 {
  427. // println("***** indirect and addressable! Needs typed move - delegate to rv.Interface()")
  428. // return rv.Interface()
  429. // }
  430. // println("****** indirect type/kind")
  431. // ui.word = urv.ptr
  432. // } else if urv.flag&unsafeRvFlagIndir != 0 {
  433. // println("****** unsafe rv flag indir")
  434. // ui.word = *(*unsafe.Pointer)(urv.ptr)
  435. // } else {
  436. // println("****** default: assign prt to word directly")
  437. // ui.word = urv.ptr
  438. // }
  439. // // ui.word = urv.ptr
  440. // ui.typ = urv.typ
  441. // // fmt.Printf("(pointers) ui.typ: %p, word: %p\n", ui.typ, ui.word)
  442. // // fmt.Printf("(binary) ui.typ: %b, word: %b\n", uintptr(ui.typ), uintptr(ui.word))
  443. // return *(*interface{})(unsafe.Pointer(&ui))
  444. // // return i
  445. // }