github.com/visualfc/xtype@v0.2.0/xtype.go (about) 1 //go:build !js || (js && wasm) 2 // +build !js js,wasm 3 4 package xtype 5 6 import ( 7 "reflect" 8 "unsafe" 9 ) 10 11 type eface struct { 12 typ unsafe.Pointer 13 word unsafe.Pointer 14 } 15 16 type Type unsafe.Pointer 17 18 func TypeOf(i interface{}) Type { 19 p := (*eface)(unsafe.Pointer(&i)) 20 return Type(p.typ) 21 } 22 23 func TypeOfType(typ reflect.Type) Type { 24 e := (*eface)(unsafe.Pointer(&typ)) 25 return Type(e.word) 26 } 27 28 func Bytes(i interface{}) []byte { 29 return *(*[]byte)((*eface)(unsafe.Pointer(&i)).word) 30 } 31 32 func Runes(i interface{}) []rune { 33 return *(*[]rune)((*eface)(unsafe.Pointer(&i)).word) 34 } 35 36 func Bool(i interface{}) bool { 37 return *(*bool)((*eface)(unsafe.Pointer(&i)).word) 38 } 39 40 func Int(i interface{}) int { 41 return *(*int)((*eface)(unsafe.Pointer(&i)).word) 42 } 43 44 func Int8(i interface{}) int8 { 45 return *(*int8)((*eface)(unsafe.Pointer(&i)).word) 46 } 47 48 func Int16(i interface{}) int16 { 49 return *(*int16)((*eface)(unsafe.Pointer(&i)).word) 50 } 51 52 func Int32(i interface{}) int32 { 53 return *(*int32)((*eface)(unsafe.Pointer(&i)).word) 54 } 55 56 func Int64(i interface{}) int64 { 57 return *(*int64)((*eface)(unsafe.Pointer(&i)).word) 58 } 59 60 func Uint(i interface{}) uint { 61 return *(*uint)((*eface)(unsafe.Pointer(&i)).word) 62 } 63 64 func Uint8(i interface{}) uint8 { 65 return *(*uint8)((*eface)(unsafe.Pointer(&i)).word) 66 } 67 68 func Uint16(i interface{}) uint16 { 69 return *(*uint16)((*eface)(unsafe.Pointer(&i)).word) 70 } 71 72 func Uint32(i interface{}) uint32 { 73 return *(*uint32)((*eface)(unsafe.Pointer(&i)).word) 74 } 75 76 func Uint64(i interface{}) uint64 { 77 return *(*uint64)((*eface)(unsafe.Pointer(&i)).word) 78 } 79 80 func Uintptr(i interface{}) uintptr { 81 return *(*uintptr)((*eface)(unsafe.Pointer(&i)).word) 82 } 83 84 func Float32(i interface{}) float32 { 85 return *(*float32)((*eface)(unsafe.Pointer(&i)).word) 86 } 87 88 func Float64(i interface{}) float64 { 89 return *(*float64)((*eface)(unsafe.Pointer(&i)).word) 90 } 91 92 func Complex64(i interface{}) complex64 { 93 return *(*complex64)((*eface)(unsafe.Pointer(&i)).word) 94 } 95 96 func Complex128(i interface{}) complex128 { 97 return *(*complex128)((*eface)(unsafe.Pointer(&i)).word) 98 } 99 100 func String(i interface{}) string { 101 return *(*string)((*eface)(unsafe.Pointer(&i)).word) 102 } 103 104 func Pointer(i interface{}) unsafe.Pointer { 105 return (*eface)(unsafe.Pointer(&i)).word 106 } 107 108 // Make change interface type and return 109 func Make(typ Type, i interface{}) interface{} { 110 p := (*eface)(unsafe.Pointer(&i)) 111 p.typ = unsafe.Pointer(typ) 112 return i 113 } 114 115 func ConvertPtr(typ Type, i interface{}) interface{} { 116 p := (*eface)(unsafe.Pointer(&i)) 117 return *(*interface{})(unsafe.Pointer(&eface{ 118 typ: unsafe.Pointer(typ), 119 word: p.word, 120 })) 121 } 122 123 //go:linkname typedmemmove reflect.typedmemmove 124 func typedmemmove(t Type, dst unsafe.Pointer, src unsafe.Pointer) 125 126 //go:linkname unsafe_New reflect.unsafe_New 127 func unsafe_New(t Type) unsafe.Pointer 128 129 // convert copy 130 func ConvertDirect(typ Type, i interface{}) interface{} { 131 p := (*eface)(unsafe.Pointer(&i)) 132 c := unsafe_New(typ) 133 typedmemmove(typ, c, p.word) 134 return *(*interface{})(unsafe.Pointer(&eface{ 135 typ: unsafe.Pointer(typ), 136 word: c, 137 })) 138 } 139 140 func ConvertBool(typ Type, i interface{}) interface{} { 141 p := (*eface)(unsafe.Pointer(&i)) 142 v := *(*bool)(p.word) 143 return *(*interface{})(unsafe.Pointer(&eface{ 144 typ: unsafe.Pointer(typ), 145 word: unsafe.Pointer(&v), 146 })) 147 } 148 149 func ConvertInt(typ Type, i interface{}) interface{} { 150 p := (*eface)(unsafe.Pointer(&i)) 151 v := *(*int)(p.word) 152 return *(*interface{})(unsafe.Pointer(&eface{ 153 typ: unsafe.Pointer(typ), 154 word: unsafe.Pointer(&v), 155 })) 156 } 157 158 func ConvertInt8(typ Type, i interface{}) interface{} { 159 p := (*eface)(unsafe.Pointer(&i)) 160 v := *(*int8)(p.word) 161 return *(*interface{})(unsafe.Pointer(&eface{ 162 typ: unsafe.Pointer(typ), 163 word: unsafe.Pointer(&v), 164 })) 165 } 166 167 func ConvertInt16(typ Type, i interface{}) interface{} { 168 p := (*eface)(unsafe.Pointer(&i)) 169 v := *(*int16)(p.word) 170 return *(*interface{})(unsafe.Pointer(&eface{ 171 typ: unsafe.Pointer(typ), 172 word: unsafe.Pointer(&v), 173 })) 174 } 175 176 func ConvertInt32(typ Type, i interface{}) interface{} { 177 p := (*eface)(unsafe.Pointer(&i)) 178 v := *(*int32)(p.word) 179 return *(*interface{})(unsafe.Pointer(&eface{ 180 typ: unsafe.Pointer(typ), 181 word: unsafe.Pointer(&v), 182 })) 183 } 184 185 func ConvertInt64(typ Type, i interface{}) interface{} { 186 p := (*eface)(unsafe.Pointer(&i)) 187 v := *(*int64)(p.word) 188 return *(*interface{})(unsafe.Pointer(&eface{ 189 typ: unsafe.Pointer(typ), 190 word: unsafe.Pointer(&v), 191 })) 192 } 193 194 func ConvertUint(typ Type, i interface{}) interface{} { 195 p := (*eface)(unsafe.Pointer(&i)) 196 v := *(*uint)(p.word) 197 return *(*interface{})(unsafe.Pointer(&eface{ 198 typ: unsafe.Pointer(typ), 199 word: unsafe.Pointer(&v), 200 })) 201 } 202 203 func ConvertUint8(typ Type, i interface{}) interface{} { 204 p := (*eface)(unsafe.Pointer(&i)) 205 v := *(*uint8)(p.word) 206 return *(*interface{})(unsafe.Pointer(&eface{ 207 typ: unsafe.Pointer(typ), 208 word: unsafe.Pointer(&v), 209 })) 210 } 211 212 func ConvertUint16(typ Type, i interface{}) interface{} { 213 p := (*eface)(unsafe.Pointer(&i)) 214 v := *(*uint16)(p.word) 215 return *(*interface{})(unsafe.Pointer(&eface{ 216 typ: unsafe.Pointer(typ), 217 word: unsafe.Pointer(&v), 218 })) 219 } 220 221 func ConvertUint32(typ Type, i interface{}) interface{} { 222 p := (*eface)(unsafe.Pointer(&i)) 223 v := *(*uint32)(p.word) 224 return *(*interface{})(unsafe.Pointer(&eface{ 225 typ: unsafe.Pointer(typ), 226 word: unsafe.Pointer(&v), 227 })) 228 } 229 230 func ConvertUint64(typ Type, i interface{}) interface{} { 231 p := (*eface)(unsafe.Pointer(&i)) 232 v := *(*uint64)(p.word) 233 return *(*interface{})(unsafe.Pointer(&eface{ 234 typ: unsafe.Pointer(typ), 235 word: unsafe.Pointer(&v), 236 })) 237 } 238 239 func ConvertUintptr(typ Type, i interface{}) interface{} { 240 p := (*eface)(unsafe.Pointer(&i)) 241 v := *(*uintptr)(p.word) 242 return *(*interface{})(unsafe.Pointer(&eface{ 243 typ: unsafe.Pointer(typ), 244 word: unsafe.Pointer(&v), 245 })) 246 } 247 248 func ConvertFloat32(typ Type, i interface{}) interface{} { 249 p := (*eface)(unsafe.Pointer(&i)) 250 v := *(*float32)(p.word) 251 return *(*interface{})(unsafe.Pointer(&eface{ 252 typ: unsafe.Pointer(typ), 253 word: unsafe.Pointer(&v), 254 })) 255 } 256 257 func ConvertFloat64(typ Type, i interface{}) interface{} { 258 p := (*eface)(unsafe.Pointer(&i)) 259 v := *(*float64)(p.word) 260 return *(*interface{})(unsafe.Pointer(&eface{ 261 typ: unsafe.Pointer(typ), 262 word: unsafe.Pointer(&v), 263 })) 264 } 265 266 func ConvertComplex64(typ Type, i interface{}) interface{} { 267 p := (*eface)(unsafe.Pointer(&i)) 268 v := *(*complex64)(p.word) 269 return *(*interface{})(unsafe.Pointer(&eface{ 270 typ: unsafe.Pointer(typ), 271 word: unsafe.Pointer(&v), 272 })) 273 } 274 275 func ConvertComplex128(typ Type, i interface{}) interface{} { 276 p := (*eface)(unsafe.Pointer(&i)) 277 v := *(*complex128)(p.word) 278 return *(*interface{})(unsafe.Pointer(&eface{ 279 typ: unsafe.Pointer(typ), 280 word: unsafe.Pointer(&v), 281 })) 282 } 283 284 func ConvertString(typ Type, i interface{}) interface{} { 285 p := (*eface)(unsafe.Pointer(&i)) 286 v := *(*string)(p.word) 287 return *(*interface{})(unsafe.Pointer(&eface{ 288 typ: unsafe.Pointer(typ), 289 word: unsafe.Pointer(&v), 290 })) 291 } 292 293 func Not(i interface{}) interface{} { 294 p := (*eface)(unsafe.Pointer(&i)) 295 v := !*(*bool)(p.word) 296 return *(*interface{})(unsafe.Pointer(&eface{ 297 typ: p.typ, 298 word: unsafe.Pointer(&v), 299 })) 300 } 301 302 func NegInt(i interface{}) interface{} { 303 p := (*eface)(unsafe.Pointer(&i)) 304 v := -*(*int)(p.word) 305 return *(*interface{})(unsafe.Pointer(&eface{ 306 typ: p.typ, 307 word: unsafe.Pointer(&v), 308 })) 309 } 310 311 func NegInt8(i interface{}) interface{} { 312 p := (*eface)(unsafe.Pointer(&i)) 313 v := -*(*int8)(p.word) 314 return *(*interface{})(unsafe.Pointer(&eface{ 315 typ: p.typ, 316 word: unsafe.Pointer(&v), 317 })) 318 } 319 320 func NegInt16(i interface{}) interface{} { 321 p := (*eface)(unsafe.Pointer(&i)) 322 v := -*(*int16)(p.word) 323 return *(*interface{})(unsafe.Pointer(&eface{ 324 typ: p.typ, 325 word: unsafe.Pointer(&v), 326 })) 327 } 328 329 func NegInt32(i interface{}) interface{} { 330 p := (*eface)(unsafe.Pointer(&i)) 331 v := -*(*int32)(p.word) 332 return *(*interface{})(unsafe.Pointer(&eface{ 333 typ: p.typ, 334 word: unsafe.Pointer(&v), 335 })) 336 } 337 338 func NegInt64(i interface{}) interface{} { 339 p := (*eface)(unsafe.Pointer(&i)) 340 v := -*(*int64)(p.word) 341 return *(*interface{})(unsafe.Pointer(&eface{ 342 typ: p.typ, 343 word: unsafe.Pointer(&v), 344 })) 345 } 346 347 func NegUint(i interface{}) interface{} { 348 p := (*eface)(unsafe.Pointer(&i)) 349 v := -*(*uint)(p.word) 350 return *(*interface{})(unsafe.Pointer(&eface{ 351 typ: p.typ, 352 word: unsafe.Pointer(&v), 353 })) 354 } 355 356 func NegUint8(i interface{}) interface{} { 357 p := (*eface)(unsafe.Pointer(&i)) 358 v := -*(*uint8)(p.word) 359 return *(*interface{})(unsafe.Pointer(&eface{ 360 typ: p.typ, 361 word: unsafe.Pointer(&v), 362 })) 363 } 364 365 func NegUint16(i interface{}) interface{} { 366 p := (*eface)(unsafe.Pointer(&i)) 367 v := -*(*uint16)(p.word) 368 return *(*interface{})(unsafe.Pointer(&eface{ 369 typ: p.typ, 370 word: unsafe.Pointer(&v), 371 })) 372 } 373 374 func NegUint32(i interface{}) interface{} { 375 p := (*eface)(unsafe.Pointer(&i)) 376 v := -*(*uint32)(p.word) 377 return *(*interface{})(unsafe.Pointer(&eface{ 378 typ: p.typ, 379 word: unsafe.Pointer(&v), 380 })) 381 } 382 383 func NegUint64(i interface{}) interface{} { 384 p := (*eface)(unsafe.Pointer(&i)) 385 v := -*(*uint64)(p.word) 386 return *(*interface{})(unsafe.Pointer(&eface{ 387 typ: p.typ, 388 word: unsafe.Pointer(&v), 389 })) 390 } 391 392 func NegUintptr(i interface{}) interface{} { 393 p := (*eface)(unsafe.Pointer(&i)) 394 v := -*(*uintptr)(p.word) 395 return *(*interface{})(unsafe.Pointer(&eface{ 396 typ: p.typ, 397 word: unsafe.Pointer(&v), 398 })) 399 } 400 401 func NegFloat32(i interface{}) interface{} { 402 p := (*eface)(unsafe.Pointer(&i)) 403 v := -*(*float32)(p.word) 404 return *(*interface{})(unsafe.Pointer(&eface{ 405 typ: p.typ, 406 word: unsafe.Pointer(&v), 407 })) 408 } 409 410 func NegFloat64(i interface{}) interface{} { 411 p := (*eface)(unsafe.Pointer(&i)) 412 v := -*(*float64)(p.word) 413 return *(*interface{})(unsafe.Pointer(&eface{ 414 typ: p.typ, 415 word: unsafe.Pointer(&v), 416 })) 417 } 418 419 func NegComplex64(i interface{}) interface{} { 420 p := (*eface)(unsafe.Pointer(&i)) 421 v := -*(*complex64)(p.word) 422 return *(*interface{})(unsafe.Pointer(&eface{ 423 typ: p.typ, 424 word: unsafe.Pointer(&v), 425 })) 426 } 427 428 func NegComplex128(i interface{}) interface{} { 429 p := (*eface)(unsafe.Pointer(&i)) 430 v := -*(*complex128)(p.word) 431 return *(*interface{})(unsafe.Pointer(&eface{ 432 typ: p.typ, 433 word: unsafe.Pointer(&v), 434 })) 435 } 436 437 func XorInt(i interface{}) interface{} { 438 p := (*eface)(unsafe.Pointer(&i)) 439 v := ^*(*int)(p.word) 440 return *(*interface{})(unsafe.Pointer(&eface{ 441 typ: p.typ, 442 word: unsafe.Pointer(&v), 443 })) 444 } 445 446 func XorInt8(i interface{}) interface{} { 447 p := (*eface)(unsafe.Pointer(&i)) 448 v := ^*(*int8)(p.word) 449 return *(*interface{})(unsafe.Pointer(&eface{ 450 typ: p.typ, 451 word: unsafe.Pointer(&v), 452 })) 453 } 454 455 func XorInt16(i interface{}) interface{} { 456 p := (*eface)(unsafe.Pointer(&i)) 457 v := ^*(*int16)(p.word) 458 return *(*interface{})(unsafe.Pointer(&eface{ 459 typ: p.typ, 460 word: unsafe.Pointer(&v), 461 })) 462 } 463 464 func XorInt32(i interface{}) interface{} { 465 p := (*eface)(unsafe.Pointer(&i)) 466 v := ^*(*int32)(p.word) 467 return *(*interface{})(unsafe.Pointer(&eface{ 468 typ: p.typ, 469 word: unsafe.Pointer(&v), 470 })) 471 } 472 473 func XorInt64(i interface{}) interface{} { 474 p := (*eface)(unsafe.Pointer(&i)) 475 v := ^*(*int64)(p.word) 476 return *(*interface{})(unsafe.Pointer(&eface{ 477 typ: p.typ, 478 word: unsafe.Pointer(&v), 479 })) 480 } 481 482 func XorUint(i interface{}) interface{} { 483 p := (*eface)(unsafe.Pointer(&i)) 484 v := ^*(*uint)(p.word) 485 return *(*interface{})(unsafe.Pointer(&eface{ 486 typ: p.typ, 487 word: unsafe.Pointer(&v), 488 })) 489 } 490 491 func XorUint8(i interface{}) interface{} { 492 p := (*eface)(unsafe.Pointer(&i)) 493 v := ^*(*uint8)(p.word) 494 return *(*interface{})(unsafe.Pointer(&eface{ 495 typ: p.typ, 496 word: unsafe.Pointer(&v), 497 })) 498 } 499 500 func XorUint16(i interface{}) interface{} { 501 p := (*eface)(unsafe.Pointer(&i)) 502 v := ^*(*uint16)(p.word) 503 return *(*interface{})(unsafe.Pointer(&eface{ 504 typ: p.typ, 505 word: unsafe.Pointer(&v), 506 })) 507 } 508 509 func XorUint32(i interface{}) interface{} { 510 p := (*eface)(unsafe.Pointer(&i)) 511 v := ^*(*uint32)(p.word) 512 return *(*interface{})(unsafe.Pointer(&eface{ 513 typ: p.typ, 514 word: unsafe.Pointer(&v), 515 })) 516 } 517 518 func XorUint64(i interface{}) interface{} { 519 p := (*eface)(unsafe.Pointer(&i)) 520 v := ^*(*uint64)(p.word) 521 return *(*interface{})(unsafe.Pointer(&eface{ 522 typ: p.typ, 523 word: unsafe.Pointer(&v), 524 })) 525 } 526 527 func XorUintptr(i interface{}) interface{} { 528 p := (*eface)(unsafe.Pointer(&i)) 529 v := ^*(*uintptr)(p.word) 530 return *(*interface{})(unsafe.Pointer(&eface{ 531 typ: p.typ, 532 word: unsafe.Pointer(&v), 533 })) 534 } 535 536 func MakeBool(typ Type, v bool) interface{} { 537 return *(*interface{})(unsafe.Pointer(&eface{ 538 typ: unsafe.Pointer(typ), 539 word: unsafe.Pointer(&v), 540 })) 541 } 542 543 func MakeInt(typ Type, v int) interface{} { 544 return *(*interface{})(unsafe.Pointer(&eface{ 545 typ: unsafe.Pointer(typ), 546 word: unsafe.Pointer(&v), 547 })) 548 } 549 550 func MakeInt8(typ Type, v int8) interface{} { 551 return *(*interface{})(unsafe.Pointer(&eface{ 552 typ: unsafe.Pointer(typ), 553 word: unsafe.Pointer(&v), 554 })) 555 } 556 557 func MakeInt16(typ Type, v int16) interface{} { 558 return *(*interface{})(unsafe.Pointer(&eface{ 559 typ: unsafe.Pointer(typ), 560 word: unsafe.Pointer(&v), 561 })) 562 } 563 564 func MakeInt32(typ Type, v int32) interface{} { 565 return *(*interface{})(unsafe.Pointer(&eface{ 566 typ: unsafe.Pointer(typ), 567 word: unsafe.Pointer(&v), 568 })) 569 } 570 571 func MakeInt64(typ Type, v int64) interface{} { 572 return *(*interface{})(unsafe.Pointer(&eface{ 573 typ: unsafe.Pointer(typ), 574 word: unsafe.Pointer(&v), 575 })) 576 } 577 578 func MakeUint(typ Type, v uint) interface{} { 579 return *(*interface{})(unsafe.Pointer(&eface{ 580 typ: unsafe.Pointer(typ), 581 word: unsafe.Pointer(&v), 582 })) 583 } 584 585 func MakeUint8(typ Type, v uint8) interface{} { 586 return *(*interface{})(unsafe.Pointer(&eface{ 587 typ: unsafe.Pointer(typ), 588 word: unsafe.Pointer(&v), 589 })) 590 } 591 592 func MakeUint16(typ Type, v uint16) interface{} { 593 return *(*interface{})(unsafe.Pointer(&eface{ 594 typ: unsafe.Pointer(typ), 595 word: unsafe.Pointer(&v), 596 })) 597 } 598 599 func MakeUint32(typ Type, v uint32) interface{} { 600 return *(*interface{})(unsafe.Pointer(&eface{ 601 typ: unsafe.Pointer(typ), 602 word: unsafe.Pointer(&v), 603 })) 604 } 605 606 func MakeUint64(typ Type, v uint64) interface{} { 607 return *(*interface{})(unsafe.Pointer(&eface{ 608 typ: unsafe.Pointer(typ), 609 word: unsafe.Pointer(&v), 610 })) 611 } 612 613 func MakeUintptr(typ Type, v uintptr) interface{} { 614 return *(*interface{})(unsafe.Pointer(&eface{ 615 typ: unsafe.Pointer(typ), 616 word: unsafe.Pointer(&v), 617 })) 618 } 619 620 func MakeFloat32(typ Type, v float32) interface{} { 621 return *(*interface{})(unsafe.Pointer(&eface{ 622 typ: unsafe.Pointer(typ), 623 word: unsafe.Pointer(&v), 624 })) 625 } 626 627 func MakeFloat64(typ Type, v float64) interface{} { 628 return *(*interface{})(unsafe.Pointer(&eface{ 629 typ: unsafe.Pointer(typ), 630 word: unsafe.Pointer(&v), 631 })) 632 } 633 634 func MakeComplex64(typ Type, v complex64) interface{} { 635 return *(*interface{})(unsafe.Pointer(&eface{ 636 typ: unsafe.Pointer(typ), 637 word: unsafe.Pointer(&v), 638 })) 639 } 640 641 func MakeComplex128(typ Type, v complex128) interface{} { 642 return *(*interface{})(unsafe.Pointer(&eface{ 643 typ: unsafe.Pointer(typ), 644 word: unsafe.Pointer(&v), 645 })) 646 } 647 648 func MakeString(typ Type, v string) interface{} { 649 return *(*interface{})(unsafe.Pointer(&eface{ 650 typ: unsafe.Pointer(typ), 651 word: unsafe.Pointer(&v), 652 })) 653 } 654 655 func Alloc(typ Type) interface{} { 656 ptr := unsafe_New(typ) 657 return *(*interface{})(unsafe.Pointer(&eface{ 658 typ: unsafe.Pointer(typ), 659 word: ptr, 660 })) 661 } 662 663 func New(typ, ptrto Type) interface{} { 664 ptr := unsafe_New(typ) 665 return *(*interface{})(unsafe.Pointer(&eface{ 666 typ: unsafe.Pointer(ptrto), 667 word: ptr, 668 })) 669 } 670 671 func NewPointer(typ Type) unsafe.Pointer { 672 return unsafe_New(typ) 673 } 674 675 func SetPointer(i interface{}, word unsafe.Pointer) interface{} { 676 p := (*eface)(unsafe.Pointer(&i)) 677 p.word = word 678 return i 679 } 680 681 func SetType(i interface{}, typ Type) interface{} { 682 p := (*eface)(unsafe.Pointer(&i)) 683 p.typ = unsafe.Pointer(typ) 684 return i 685 } 686 687 func ConvertFunc(fn reflect.Value, typ Type) reflect.Value { 688 (*struct { 689 typ Type 690 ptr unsafe.Pointer 691 })(unsafe.Pointer(&fn)).typ = typ 692 return fn 693 }