| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 |
|
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #[doc = codegen_section!(
|
| 67 | header = "h2",
|
| 68 | bench = "transmute",
|
| 69 | format = "coco_static_size",
|
| 70 | )]
|
| 71 | #[macro_export]
|
| 72 | macro_rules! transmute {
|
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | (#![allow(shrink)] $e:expr) => {{
|
| 78 | let mut e = $e;
|
| 79 | if false {
|
| 80 | |
| 81 | |
| 82 | |
| 83 |
|
| 84 | fn transmute<Src, Dst>(src: Src) -> Dst
|
| 85 | where
|
| 86 | Src: $crate::IntoBytes,
|
| 87 | Dst: $crate::FromBytes,
|
| 88 | {
|
| 89 | let _ = src;
|
| 90 | loop {}
|
| 91 | }
|
| 92 | loop {}
|
| 93 | #[allow(unreachable_code)]
|
| 94 | transmute(e)
|
| 95 | } else {
|
| 96 | use $crate::util::macro_util::core_reexport::mem::ManuallyDrop;
|
| 97 |
|
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | #[repr(C, packed)]
|
| 107 | union Transmute<Src, Dst> {
|
| 108 | src: ManuallyDrop<Src>,
|
| 109 | dst: ManuallyDrop<Dst>,
|
| 110 | }
|
| 111 |
|
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | let u: Transmute<_, _> = unsafe {
|
| 125 | |
| 126 | |
| 127 | #[allow(clippy::missing_transmute_annotations)]
|
| 128 | $crate::util::macro_util::core_reexport::mem::transmute(e)
|
| 129 | };
|
| 130 |
|
| 131 | if false {
|
| 132 | |
| 133 | e = ManuallyDrop::into_inner(unsafe { u.src });
|
| 134 | |
| 135 | let _ = e;
|
| 136 | loop {}
|
| 137 | } else {
|
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | let dst = unsafe { u.dst };
|
| 157 | $crate::util::macro_util::must_use(ManuallyDrop::into_inner(dst))
|
| 158 | }
|
| 159 | }
|
| 160 | }};
|
| 161 | ($e:expr) => {{
|
| 162 | let e = $e;
|
| 163 | if false {
|
| 164 | |
| 165 | |
| 166 | |
| 167 |
|
| 168 | fn transmute<Src, Dst>(src: Src) -> Dst
|
| 169 | where
|
| 170 | Src: $crate::IntoBytes,
|
| 171 | Dst: $crate::FromBytes,
|
| 172 | {
|
| 173 | let _ = src;
|
| 174 | loop {}
|
| 175 | }
|
| 176 | loop {}
|
| 177 | #[allow(unreachable_code)]
|
| 178 | transmute(e)
|
| 179 | } else {
|
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | let u = unsafe {
|
| 185 | |
| 186 | |
| 187 | #[allow(clippy::missing_transmute_annotations, unnecessary_transmutes)]
|
| 188 | $crate::util::macro_util::core_reexport::mem::transmute(e)
|
| 189 | };
|
| 190 | $crate::util::macro_util::must_use(u)
|
| 191 | }
|
| 192 | }};
|
| 193 | }
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | #[doc = codegen_section!(
|
| 325 | header = "h2",
|
| 326 | bench = "transmute_ref",
|
| 327 | format = "coco",
|
| 328 | arity = 2,
|
| 329 | [
|
| 330 | open
|
| 331 | @index 1
|
| 332 | @title "Sized"
|
| 333 | @variant "static_size"
|
| 334 | ],
|
| 335 | [
|
| 336 | @index 2
|
| 337 | @title "Unsized"
|
| 338 | @variant "dynamic_size"
|
| 339 | ]
|
| 340 | )]
|
| 341 | #[macro_export]
|
| 342 | macro_rules! transmute_ref {
|
| 343 | ($e:expr) => {{
|
| 344 | |
| 345 | |
| 346 | |
| 347 |
|
| 348 | |
| 349 | |
| 350 | let e: &_ = $e;
|
| 351 |
|
| 352 | #[allow(unused, clippy::diverging_sub_expression)]
|
| 353 | if false {
|
| 354 | |
| 355 | |
| 356 | |
| 357 |
|
| 358 | struct AssertSrcIsIntoBytes<'a, T: ?::core::marker::Sized + $crate::IntoBytes>(&'a T);
|
| 359 | struct AssertSrcIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
|
| 360 | struct AssertDstIsFromBytes<'a, U: ?::core::marker::Sized + $crate::FromBytes>(&'a U);
|
| 361 | struct AssertDstIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
|
| 362 |
|
| 363 | let _ = AssertSrcIsIntoBytes(e);
|
| 364 | let _ = AssertSrcIsImmutable(e);
|
| 365 |
|
| 366 | if true {
|
| 367 | #[allow(unused, unreachable_code)]
|
| 368 | let u = AssertDstIsFromBytes(loop {});
|
| 369 | u.0
|
| 370 | } else {
|
| 371 | #[allow(unused, unreachable_code)]
|
| 372 | let u = AssertDstIsImmutable(loop {});
|
| 373 | u.0
|
| 374 | }
|
| 375 | } else {
|
| 376 | use $crate::util::macro_util::TransmuteRefDst;
|
| 377 | let t = $crate::util::macro_util::Wrap::new(e);
|
| 378 |
|
| 379 | if false {
|
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | t.transmute_ref_inference_helper()
|
| 397 | } else {
|
| 398 | |
| 399 | |
| 400 | |
| 401 | unsafe {
|
| 402 | t.transmute_ref()
|
| 403 | }
|
| 404 | }
|
| 405 | }
|
| 406 | }}
|
| 407 | }
|
| 408 |
|
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | #[macro_export]
|
| 539 | macro_rules! transmute_mut {
|
| 540 | ($e:expr) => {{
|
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 |
|
| 548 | |
| 549 | let e: &mut _ = $e;
|
| 550 |
|
| 551 | #[allow(unused)]
|
| 552 | use $crate::util::macro_util::TransmuteMutDst as _;
|
| 553 | let t = $crate::util::macro_util::Wrap::new(e);
|
| 554 | if false {
|
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | t.transmute_mut_inference_helper()
|
| 571 | } else {
|
| 572 | t.transmute_mut()
|
| 573 | }
|
| 574 | }}
|
| 575 | }
|
| 576 |
|
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | #[doc = codegen_section!(
|
| 623 | header = "h2",
|
| 624 | bench = "try_transmute",
|
| 625 | format = "coco_static_size",
|
| 626 | )]
|
| 627 | #[macro_export]
|
| 628 | macro_rules! try_transmute {
|
| 629 | ($e:expr) => {{
|
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 |
|
| 635 | let e = $e;
|
| 636 | if false {
|
| 637 | |
| 638 | |
| 639 |
|
| 640 | |
| 641 | Ok(unsafe {
|
| 642 | |
| 643 | |
| 644 | #[allow(clippy::missing_transmute_annotations)]
|
| 645 | $crate::util::macro_util::core_reexport::mem::transmute(e)
|
| 646 | })
|
| 647 | } else {
|
| 648 | $crate::util::macro_util::try_transmute::<_, _>(e)
|
| 649 | }
|
| 650 | }}
|
| 651 | }
|
| 652 |
|
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | #[doc = codegen_section!(
|
| 750 | header = "h2",
|
| 751 | bench = "try_transmute_ref",
|
| 752 | format = "coco",
|
| 753 | arity = 2,
|
| 754 | [
|
| 755 | open
|
| 756 | @index 1
|
| 757 | @title "Sized"
|
| 758 | @variant "static_size"
|
| 759 | ],
|
| 760 | [
|
| 761 | @index 2
|
| 762 | @title "Unsized"
|
| 763 | @variant "dynamic_size"
|
| 764 | ]
|
| 765 | )]
|
| 766 | #[macro_export]
|
| 767 | macro_rules! try_transmute_ref {
|
| 768 | ($e:expr) => {{
|
| 769 | |
| 770 | |
| 771 | let e: &_ = $e;
|
| 772 |
|
| 773 | #[allow(unused_imports)]
|
| 774 | use $crate::util::macro_util::TryTransmuteRefDst as _;
|
| 775 | let t = $crate::util::macro_util::Wrap::new(e);
|
| 776 | if false {
|
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | Ok(t.transmute_ref_inference_helper())
|
| 794 | } else {
|
| 795 | t.try_transmute_ref()
|
| 796 | }
|
| 797 | }}
|
| 798 | }
|
| 799 |
|
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | #[macro_export]
|
| 901 | macro_rules! try_transmute_mut {
|
| 902 | ($e:expr) => {{
|
| 903 | |
| 904 | let e: &mut _ = $e;
|
| 905 |
|
| 906 | #[allow(unused_imports)]
|
| 907 | use $crate::util::macro_util::TryTransmuteMutDst as _;
|
| 908 | let t = $crate::util::macro_util::Wrap::new(e);
|
| 909 | if false {
|
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | Ok(t.transmute_mut_inference_helper())
|
| 927 | } else {
|
| 928 | t.try_transmute_mut()
|
| 929 | }
|
| 930 | }}
|
| 931 | }
|
| 932 |
|
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | #[doc(alias("include_bytes", "include_data", "include_type"))]
|
| 981 | #[macro_export]
|
| 982 | macro_rules! include_value {
|
| 983 | ($file:expr $(,)?) => {
|
| 984 | $crate::transmute!(*::core::include_bytes!($file))
|
| 985 | };
|
| 986 | }
|
| 987 |
|
| 988 | #[doc(hidden)]
|
| 989 | #[macro_export]
|
| 990 | macro_rules! cryptocorrosion_derive_traits {
|
| 991 | (
|
| 992 | #[repr($repr:ident)]
|
| 993 | $(#[$attr:meta])*
|
| 994 | $vis:vis struct $name:ident $(<$($tyvar:ident),*>)?
|
| 995 | $(
|
| 996 | (
|
| 997 | $($tuple_field_vis:vis $tuple_field_ty:ty),*
|
| 998 | );
|
| 999 | )?
|
| 1000 |
|
| 1001 | $(
|
| 1002 | {
|
| 1003 | $($field_vis:vis $field_name:ident: $field_ty:ty,)*
|
| 1004 | }
|
| 1005 | )?
|
| 1006 | ) => {
|
| 1007 | $crate::cryptocorrosion_derive_traits!(@assert_allowed_struct_repr #[repr($repr)]);
|
| 1008 |
|
| 1009 | $(#[$attr])*
|
| 1010 | #[repr($repr)]
|
| 1011 | $vis struct $name $(<$($tyvar),*>)?
|
| 1012 | $(
|
| 1013 | (
|
| 1014 | $($tuple_field_vis $tuple_field_ty),*
|
| 1015 | );
|
| 1016 | )?
|
| 1017 |
|
| 1018 | $(
|
| 1019 | {
|
| 1020 | $($field_vis $field_name: $field_ty,)*
|
| 1021 | }
|
| 1022 | )?
|
| 1023 |
|
| 1024 | // SAFETY: See inline.
|
| 1025 | unsafe impl $(<$($tyvar),*>)? $crate::TryFromBytes for $name$(<$($tyvar),*>)?
|
| 1026 | where
|
| 1027 | $(
|
| 1028 | $($tuple_field_ty: $crate::FromBytes,)*
|
| 1029 | )?
|
| 1030 |
|
| 1031 | $(
|
| 1032 | $($field_ty: $crate::FromBytes,)*
|
| 1033 | )?
|
| 1034 | {
|
| 1035 | #[inline(always)]
|
| 1036 | fn is_bit_valid<A>(_: $crate::Maybe<'_, Self, A>) -> bool
|
| 1037 | where
|
| 1038 | A: $crate::invariant::Alignment,
|
| 1039 | {
|
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | true
|
| 1046 | }
|
| 1047 |
|
| 1048 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1049 | }
|
| 1050 |
|
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | unsafe impl $(<$($tyvar),*>)? $crate::FromZeros for $name$(<$($tyvar),*>)?
|
| 1055 | where
|
| 1056 | $(
|
| 1057 | $($tuple_field_ty: $crate::FromBytes,)*
|
| 1058 | )?
|
| 1059 |
|
| 1060 | $(
|
| 1061 | $($field_ty: $crate::FromBytes,)*
|
| 1062 | )?
|
| 1063 | {
|
| 1064 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1065 | }
|
| 1066 |
|
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | unsafe impl $(<$($tyvar),*>)? $crate::FromBytes for $name$(<$($tyvar),*>)?
|
| 1071 | where
|
| 1072 | $(
|
| 1073 | $($tuple_field_ty: $crate::FromBytes,)*
|
| 1074 | )?
|
| 1075 |
|
| 1076 | $(
|
| 1077 | $($field_ty: $crate::FromBytes,)*
|
| 1078 | )?
|
| 1079 | {
|
| 1080 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1081 | }
|
| 1082 |
|
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | unsafe impl $(<$($tyvar),*>)? $crate::IntoBytes for $name$(<$($tyvar),*>)?
|
| 1088 | where
|
| 1089 | $(
|
| 1090 | $($tuple_field_ty: $crate::IntoBytes,)*
|
| 1091 | )?
|
| 1092 |
|
| 1093 | $(
|
| 1094 | $($field_ty: $crate::IntoBytes,)*
|
| 1095 | )?
|
| 1096 |
|
| 1097 | (): $crate::util::macro_util::PaddingFree<
|
| 1098 | Self,
|
| 1099 | {
|
| 1100 | $crate::cryptocorrosion_derive_traits!(
|
| 1101 | @struct_padding_check #[repr($repr)]
|
| 1102 | $(($($tuple_field_ty),*))?
|
| 1103 | $({$($field_ty),*})?
|
| 1104 | )
|
| 1105 | },
|
| 1106 | >,
|
| 1107 | {
|
| 1108 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1109 | }
|
| 1110 |
|
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | unsafe impl $(<$($tyvar),*>)? $crate::Immutable for $name$(<$($tyvar),*>)?
|
| 1115 | where
|
| 1116 | $(
|
| 1117 | $($tuple_field_ty: $crate::Immutable,)*
|
| 1118 | )?
|
| 1119 |
|
| 1120 | $(
|
| 1121 | $($field_ty: $crate::Immutable,)*
|
| 1122 | )?
|
| 1123 | {
|
| 1124 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1125 | }
|
| 1126 | };
|
| 1127 | (@assert_allowed_struct_repr #[repr(transparent)]) => {};
|
| 1128 | (@assert_allowed_struct_repr #[repr(C)]) => {};
|
| 1129 | (@assert_allowed_struct_repr #[$_attr:meta]) => {
|
| 1130 | compile_error!("repr must be `#[repr(transparent)]` or `#[repr(C)]`");
|
| 1131 | };
|
| 1132 | (
|
| 1133 | @struct_padding_check #[repr(transparent)]
|
| 1134 | $(($($tuple_field_ty:ty),*))?
|
| 1135 | $({$($field_ty:ty),*})?
|
| 1136 | ) => {
|
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | 0
|
| 1141 | };
|
| 1142 | (
|
| 1143 | @struct_padding_check #[repr(C)]
|
| 1144 | $(($($tuple_field_ty:ty),*))?
|
| 1145 | $({$($field_ty:ty),*})?
|
| 1146 | ) => {
|
| 1147 | $crate::struct_padding!(
|
| 1148 | Self,
|
| 1149 | None,
|
| 1150 | None,
|
| 1151 | [
|
| 1152 | $($($tuple_field_ty),*)?
|
| 1153 | $($($field_ty),*)?
|
| 1154 | ]
|
| 1155 | )
|
| 1156 | };
|
| 1157 | (
|
| 1158 | #[repr(C)]
|
| 1159 | $(#[$attr:meta])*
|
| 1160 | $vis:vis union $name:ident {
|
| 1161 | $(
|
| 1162 | $field_name:ident: $field_ty:ty,
|
| 1163 | )*
|
| 1164 | }
|
| 1165 | ) => {
|
| 1166 | $(#[$attr])*
|
| 1167 | #[repr(C)]
|
| 1168 | $vis union $name {
|
| 1169 | $(
|
| 1170 | $field_name: $field_ty,
|
| 1171 | )*
|
| 1172 | }
|
| 1173 |
|
| 1174 | |
| 1175 | unsafe impl $crate::TryFromBytes for $name
|
| 1176 | where
|
| 1177 | $(
|
| 1178 | $field_ty: $crate::FromBytes,
|
| 1179 | )*
|
| 1180 | {
|
| 1181 | #[inline(always)]
|
| 1182 | fn is_bit_valid<A>(_: $crate::Maybe<'_, Self, A>) -> bool
|
| 1183 | where
|
| 1184 | A: $crate::invariant::Alignment,
|
| 1185 | {
|
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | true
|
| 1191 | }
|
| 1192 |
|
| 1193 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1194 | }
|
| 1195 |
|
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | unsafe impl $crate::FromZeros for $name
|
| 1200 | where
|
| 1201 | $(
|
| 1202 | $field_ty: $crate::FromBytes,
|
| 1203 | )*
|
| 1204 | {
|
| 1205 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1206 | }
|
| 1207 |
|
| 1208 | |
| 1209 | |
| 1210 | unsafe impl $crate::FromBytes for $name
|
| 1211 | where
|
| 1212 | $(
|
| 1213 | $field_ty: $crate::FromBytes,
|
| 1214 | )*
|
| 1215 | {
|
| 1216 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1217 | }
|
| 1218 |
|
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | unsafe impl $crate::IntoBytes for $name
|
| 1224 | where
|
| 1225 | $(
|
| 1226 | $field_ty: $crate::IntoBytes,
|
| 1227 | )*
|
| 1228 | (): $crate::util::macro_util::PaddingFree<
|
| 1229 | Self,
|
| 1230 | {
|
| 1231 | $crate::union_padding!(
|
| 1232 | Self,
|
| 1233 | None::<usize>,
|
| 1234 | None::<usize>,
|
| 1235 | [$($field_ty),*]
|
| 1236 | )
|
| 1237 | },
|
| 1238 | >,
|
| 1239 | {
|
| 1240 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1241 | }
|
| 1242 |
|
| 1243 | |
| 1244 | |
| 1245 | unsafe impl $crate::Immutable for $name
|
| 1246 | where
|
| 1247 | $(
|
| 1248 | $field_ty: $crate::Immutable,
|
| 1249 | )*
|
| 1250 | {
|
| 1251 | fn only_derive_is_allowed_to_implement_this_trait() {}
|
| 1252 | }
|
| 1253 | };
|
| 1254 | }
|
| 1255 |
|
| 1256 | #[cfg(test)]
|
| 1257 | mod tests {
|
| 1258 | use crate::{
|
| 1259 | byteorder::native_endian::{U16, U32},
|
| 1260 | util::testutil::*,
|
| 1261 | *,
|
| 1262 | };
|
| 1263 |
|
| 1264 | #[derive(KnownLayout, Immutable, FromBytes, IntoBytes, PartialEq, Debug)]
|
| 1265 | #[repr(C)]
|
| 1266 | struct SliceDst<T, U> {
|
| 1267 | a: T,
|
| 1268 | b: [U],
|
| 1269 | }
|
| 1270 |
|
| 1271 | #[test]
|
| 1272 | fn test_transmute() {
|
| 1273 | |
| 1274 | let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1275 | let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1276 | let x: [[u8; 2]; 4] = transmute!(array_of_u8s);
|
| 1277 | assert_eq!(x, array_of_arrays);
|
| 1278 | let x: [u8; 8] = transmute!(array_of_arrays);
|
| 1279 | assert_eq!(x, array_of_u8s);
|
| 1280 |
|
| 1281 | |
| 1282 | let x: [[u8; 2]; 3] = transmute!(#![allow(shrink)] array_of_u8s);
|
| 1283 | assert_eq!(x, [[0u8, 1], [2, 3], [4, 5]]);
|
| 1284 |
|
| 1285 | |
| 1286 | |
| 1287 | #[derive(IntoBytes)]
|
| 1288 | #[repr(transparent)]
|
| 1289 | struct PanicOnDrop(());
|
| 1290 | impl Drop for PanicOnDrop {
|
| 1291 | fn drop(&mut self) {
|
| 1292 | panic!("PanicOnDrop::drop");
|
| 1293 | }
|
| 1294 | }
|
| 1295 | #[allow(clippy::let_unit_value)]
|
| 1296 | let _: () = transmute!(PanicOnDrop(()));
|
| 1297 | #[allow(clippy::let_unit_value)]
|
| 1298 | let _: () = transmute!(#![allow(shrink)] PanicOnDrop(()));
|
| 1299 |
|
| 1300 | |
| 1301 | const ARRAY_OF_U8S: [u8; 8] = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1302 | const ARRAY_OF_ARRAYS: [[u8; 2]; 4] = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1303 | const X: [[u8; 2]; 4] = transmute!(ARRAY_OF_U8S);
|
| 1304 | assert_eq!(X, ARRAY_OF_ARRAYS);
|
| 1305 | const X_SHRINK: [[u8; 2]; 3] = transmute!(#![allow(shrink)] ARRAY_OF_U8S);
|
| 1306 | assert_eq!(X_SHRINK, [[0u8, 1], [2, 3], [4, 5]]);
|
| 1307 |
|
| 1308 | |
| 1309 | let x: usize = transmute!(UnsafeCell::new(1usize));
|
| 1310 | assert_eq!(x, 1);
|
| 1311 | let x: UnsafeCell<usize> = transmute!(1usize);
|
| 1312 | assert_eq!(x.into_inner(), 1);
|
| 1313 | let x: UnsafeCell<isize> = transmute!(UnsafeCell::new(1usize));
|
| 1314 | assert_eq!(x.into_inner(), 1);
|
| 1315 | }
|
| 1316 |
|
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | #[derive(FromBytes, IntoBytes, Immutable, PartialEq, Eq, Debug)]
|
| 1326 | #[repr(transparent)]
|
| 1327 | struct Nkl<T>(T);
|
| 1328 |
|
| 1329 | #[test]
|
| 1330 | fn test_transmute_ref() {
|
| 1331 | |
| 1332 | let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1333 | let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1334 | let x: &[[u8; 2]; 4] = transmute_ref!(&array_of_u8s);
|
| 1335 | assert_eq!(*x, array_of_arrays);
|
| 1336 | let x: &[u8; 8] = transmute_ref!(&array_of_arrays);
|
| 1337 | assert_eq!(*x, array_of_u8s);
|
| 1338 |
|
| 1339 | |
| 1340 | const ARRAY_OF_U8S: [u8; 8] = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1341 | const ARRAY_OF_ARRAYS: [[u8; 2]; 4] = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1342 | #[allow(clippy::redundant_static_lifetimes)]
|
| 1343 | const X: &'static [[u8; 2]; 4] = transmute_ref!(&ARRAY_OF_U8S);
|
| 1344 | assert_eq!(*X, ARRAY_OF_ARRAYS);
|
| 1345 |
|
| 1346 | |
| 1347 | let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1348 | let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1349 | let slice_of_arrays = &array_of_arrays[..];
|
| 1350 | let x: &[[u8; 2]] = transmute_ref!(&array_of_u8s);
|
| 1351 | assert_eq!(x, slice_of_arrays);
|
| 1352 |
|
| 1353 | |
| 1354 | |
| 1355 | #[cfg(no_zerocopy_generic_bounds_in_const_fn_1_61_0)]
|
| 1356 | {
|
| 1357 | |
| 1358 | |
| 1359 | const ARRAY_OF_NKL_U8S: Nkl<[u8; 8]> = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
|
| 1360 | const ARRAY_OF_NKL_ARRAYS: Nkl<[[u8; 2]; 4]> = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
|
| 1361 | const X_NKL: &Nkl<[[u8; 2]; 4]> = transmute_ref!(&ARRAY_OF_NKL_U8S);
|
| 1362 | assert_eq!(*X_NKL, ARRAY_OF_NKL_ARRAYS);
|
| 1363 | }
|
| 1364 |
|
| 1365 | #[cfg(not(no_zerocopy_generic_bounds_in_const_fn_1_61_0))]
|
| 1366 | {
|
| 1367 | |
| 1368 | |
| 1369 | const fn transmute_ref<T, U>(t: &T) -> &U
|
| 1370 | where
|
| 1371 | T: IntoBytes + Immutable,
|
| 1372 | U: FromBytes + Immutable,
|
| 1373 | {
|
| 1374 | transmute_ref!(t)
|
| 1375 | }
|
| 1376 |
|
| 1377 | |
| 1378 | |
| 1379 | const ARRAY_OF_NKL_U8S: Nkl<[u8; 8]> = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
|
| 1380 | const ARRAY_OF_NKL_ARRAYS: Nkl<[[u8; 2]; 4]> = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
|
| 1381 | const X_NKL: &Nkl<[[u8; 2]; 4]> = transmute_ref(&ARRAY_OF_NKL_U8S);
|
| 1382 | assert_eq!(*X_NKL, ARRAY_OF_NKL_ARRAYS);
|
| 1383 | }
|
| 1384 |
|
| 1385 | |
| 1386 | |
| 1387 | let slice_dst_of_u8s =
|
| 1388 | SliceDst::<U16, [u8; 2]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1389 | let slice_dst_of_u16s =
|
| 1390 | SliceDst::<U16, U16>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1391 | let x: &SliceDst<U16, U16> = transmute_ref!(slice_dst_of_u8s);
|
| 1392 | assert_eq!(x, slice_dst_of_u16s);
|
| 1393 |
|
| 1394 | let slice_dst_of_u8s =
|
| 1395 | SliceDst::<U16, u8>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1396 | let x: &[u8] = transmute_ref!(slice_dst_of_u8s);
|
| 1397 | assert_eq!(x, [0, 1, 2, 3, 4, 5]);
|
| 1398 |
|
| 1399 | let x: &[u8] = transmute_ref!(slice_dst_of_u16s);
|
| 1400 | assert_eq!(x, [0, 1, 2, 3, 4, 5]);
|
| 1401 |
|
| 1402 | let x: &[U16] = transmute_ref!(slice_dst_of_u16s);
|
| 1403 | let slice_of_u16s: &[U16] = <[U16]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1404 | assert_eq!(x, slice_of_u16s);
|
| 1405 |
|
| 1406 | |
| 1407 | |
| 1408 | let bytes = &[0, 1, 2, 3, 4, 5, 6, 7][..];
|
| 1409 | let slice_dst_big = SliceDst::<U32, U16>::ref_from_bytes(bytes).unwrap();
|
| 1410 | let slice_dst_small = SliceDst::<U16, u8>::ref_from_bytes(bytes).unwrap();
|
| 1411 | let x: &SliceDst<U16, u8> = transmute_ref!(slice_dst_big);
|
| 1412 | assert_eq!(x, slice_dst_small);
|
| 1413 |
|
| 1414 | |
| 1415 | |
| 1416 | let x: &[u8; 8] = transmute_ref!(X);
|
| 1417 | assert_eq!(*x, ARRAY_OF_U8S);
|
| 1418 |
|
| 1419 | |
| 1420 | let u = AU64(0);
|
| 1421 | let array = [0, 0, 0, 0, 0, 0, 0, 0];
|
| 1422 | let x: &[u8; 8] = transmute_ref!(&u);
|
| 1423 | assert_eq!(*x, array);
|
| 1424 |
|
| 1425 | |
| 1426 | let mut x = 0u8;
|
| 1427 | #[allow(clippy::useless_transmute)]
|
| 1428 | let y: &u8 = transmute_ref!(&mut x);
|
| 1429 | assert_eq!(*y, 0);
|
| 1430 | }
|
| 1431 |
|
| 1432 | #[test]
|
| 1433 | fn test_try_transmute() {
|
| 1434 | |
| 1435 | let array_of_bools = [false, true, false, true, false, true, false, true];
|
| 1436 | let array_of_arrays = [[0, 1], [0, 1], [0, 1], [0, 1]];
|
| 1437 | let x: Result<[[u8; 2]; 4], _> = try_transmute!(array_of_bools);
|
| 1438 | assert_eq!(x, Ok(array_of_arrays));
|
| 1439 | let x: Result<[bool; 8], _> = try_transmute!(array_of_arrays);
|
| 1440 | assert_eq!(x, Ok(array_of_bools));
|
| 1441 |
|
| 1442 | |
| 1443 | let x: Result<usize, _> = try_transmute!(UnsafeCell::new(1usize));
|
| 1444 | assert_eq!(x.unwrap(), 1);
|
| 1445 | let x: Result<UnsafeCell<usize>, _> = try_transmute!(1usize);
|
| 1446 | assert_eq!(x.unwrap().into_inner(), 1);
|
| 1447 | let x: Result<UnsafeCell<isize>, _> = try_transmute!(UnsafeCell::new(1usize));
|
| 1448 | assert_eq!(x.unwrap().into_inner(), 1);
|
| 1449 |
|
| 1450 | #[derive(FromBytes, IntoBytes, Debug, PartialEq)]
|
| 1451 | #[repr(transparent)]
|
| 1452 | struct PanicOnDrop<T>(T);
|
| 1453 |
|
| 1454 | impl<T> Drop for PanicOnDrop<T> {
|
| 1455 | fn drop(&mut self) {
|
| 1456 | panic!("PanicOnDrop dropped");
|
| 1457 | }
|
| 1458 | }
|
| 1459 |
|
| 1460 | |
| 1461 | |
| 1462 | let x: Result<usize, _> = try_transmute!(PanicOnDrop(1usize));
|
| 1463 | assert_eq!(x, Ok(1));
|
| 1464 |
|
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | let y: Result<bool, _> = try_transmute!(PanicOnDrop(2u8));
|
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | assert_eq!(y.as_ref().map_err(|p| &p.src.0), Err::<&bool, _>(&2u8));
|
| 1473 | mem::forget(y);
|
| 1474 | }
|
| 1475 |
|
| 1476 | #[test]
|
| 1477 | fn test_try_transmute_ref() {
|
| 1478 | |
| 1479 | let array_of_bools = &[false, true, false, true, false, true, false, true];
|
| 1480 | let array_of_arrays = &[[0, 1], [0, 1], [0, 1], [0, 1]];
|
| 1481 | let x: Result<&[[u8; 2]; 4], _> = try_transmute_ref!(array_of_bools);
|
| 1482 | assert_eq!(x, Ok(array_of_arrays));
|
| 1483 | let x: Result<&[bool; 8], _> = try_transmute_ref!(array_of_arrays);
|
| 1484 | assert_eq!(x, Ok(array_of_bools));
|
| 1485 |
|
| 1486 | |
| 1487 | |
| 1488 | {
|
| 1489 | let x: Result<&[[u8; 2]; 4], _> = try_transmute_ref!(array_of_bools);
|
| 1490 | assert_eq!(x, Ok(array_of_arrays));
|
| 1491 | }
|
| 1492 |
|
| 1493 | |
| 1494 | let u = AU64(0);
|
| 1495 | let array = [0u8, 0, 0, 0, 0, 0, 0, 0];
|
| 1496 | let x: Result<&[u8; 8], _> = try_transmute_ref!(&u);
|
| 1497 | assert_eq!(x, Ok(&array));
|
| 1498 |
|
| 1499 | |
| 1500 | let mut x = 0u8;
|
| 1501 | #[allow(clippy::useless_transmute)]
|
| 1502 | let y: Result<&u8, _> = try_transmute_ref!(&mut x);
|
| 1503 | assert_eq!(y, Ok(&0));
|
| 1504 |
|
| 1505 | |
| 1506 | let array_of_nkl_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
|
| 1507 | let array_of_nkl_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
|
| 1508 | let x: Result<&Nkl<[[u8; 2]; 4]>, _> = try_transmute_ref!(&array_of_nkl_u8s);
|
| 1509 | assert_eq!(x, Ok(&array_of_nkl_arrays));
|
| 1510 |
|
| 1511 | |
| 1512 | let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1513 | let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1514 | let slice_of_arrays = &array_of_arrays[..];
|
| 1515 | let x: Result<&[[u8; 2]], _> = try_transmute_ref!(&array_of_u8s);
|
| 1516 | assert_eq!(x, Ok(slice_of_arrays));
|
| 1517 |
|
| 1518 | |
| 1519 | let slice_dst_of_u8s =
|
| 1520 | SliceDst::<U16, [u8; 2]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1521 | let slice_dst_of_u16s =
|
| 1522 | SliceDst::<U16, U16>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
|
| 1523 | let x: Result<&SliceDst<U16, U16>, _> = try_transmute_ref!(slice_dst_of_u8s);
|
| 1524 | assert_eq!(x, Ok(slice_dst_of_u16s));
|
| 1525 | }
|
| 1526 |
|
| 1527 | #[test]
|
| 1528 | fn test_try_transmute_mut() {
|
| 1529 | |
| 1530 | let array_of_u8s = &mut [0u8, 1, 0, 1, 0, 1, 0, 1];
|
| 1531 | let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
|
| 1532 | let x: Result<&mut [[u8; 2]; 4], _> = try_transmute_mut!(array_of_u8s);
|
| 1533 | assert_eq!(x, Ok(array_of_arrays));
|
| 1534 |
|
| 1535 | let array_of_bools = &mut [false, true, false, true, false, true, false, true];
|
| 1536 | let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
|
| 1537 | let x: Result<&mut [bool; 8], _> = try_transmute_mut!(array_of_arrays);
|
| 1538 | assert_eq!(x, Ok(array_of_bools));
|
| 1539 |
|
| 1540 | |
| 1541 | |
| 1542 | let array_of_bools = &mut [false, true, false, true, false, true, false, true];
|
| 1543 | let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
|
| 1544 | {
|
| 1545 | let x: Result<&mut [bool; 8], _> = try_transmute_mut!(array_of_arrays);
|
| 1546 | assert_eq!(x, Ok(array_of_bools));
|
| 1547 | }
|
| 1548 |
|
| 1549 | |
| 1550 | let u = &mut AU64(0);
|
| 1551 | let array = &mut [0u8, 0, 0, 0, 0, 0, 0, 0];
|
| 1552 | let x: Result<&mut [u8; 8], _> = try_transmute_mut!(u);
|
| 1553 | assert_eq!(x, Ok(array));
|
| 1554 |
|
| 1555 | |
| 1556 | let mut x = 0u8;
|
| 1557 | #[allow(clippy::useless_transmute)]
|
| 1558 | let y: Result<&mut u8, _> = try_transmute_mut!(&mut x);
|
| 1559 | assert_eq!(y, Ok(&mut 0));
|
| 1560 |
|
| 1561 | |
| 1562 | let mut array_of_nkl_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
|
| 1563 | let mut array_of_nkl_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
|
| 1564 | let x: Result<&mut Nkl<[[u8; 2]; 4]>, _> = try_transmute_mut!(&mut array_of_nkl_u8s);
|
| 1565 | assert_eq!(x, Ok(&mut array_of_nkl_arrays));
|
| 1566 |
|
| 1567 | |
| 1568 | let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1569 | let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1570 | let slice_of_arrays = &mut array_of_arrays[..];
|
| 1571 | let x: Result<&mut [[u8; 2]], _> = try_transmute_mut!(&mut array_of_u8s);
|
| 1572 | assert_eq!(x, Ok(slice_of_arrays));
|
| 1573 |
|
| 1574 | |
| 1575 | let mut bytes = [0, 1, 2, 3, 4, 5, 6];
|
| 1576 | let slice_dst_of_u8s = SliceDst::<u8, [u8; 2]>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1577 | let mut bytes = [0, 1, 2, 3, 4, 5, 6];
|
| 1578 | let slice_dst_of_u16s = SliceDst::<u8, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1579 | let x: Result<&mut SliceDst<u8, U16>, _> = try_transmute_mut!(slice_dst_of_u8s);
|
| 1580 | assert_eq!(x, Ok(slice_dst_of_u16s));
|
| 1581 | }
|
| 1582 |
|
| 1583 | #[test]
|
| 1584 | fn test_transmute_mut() {
|
| 1585 | |
| 1586 | let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1587 | let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1588 | let x: &mut [[u8; 2]; 4] = transmute_mut!(&mut array_of_u8s);
|
| 1589 | assert_eq!(*x, array_of_arrays);
|
| 1590 | let x: &mut [u8; 8] = transmute_mut!(&mut array_of_arrays);
|
| 1591 | assert_eq!(*x, array_of_u8s);
|
| 1592 |
|
| 1593 | {
|
| 1594 | |
| 1595 | |
| 1596 | let x: &mut [u8; 8] = transmute_mut!(&mut array_of_arrays);
|
| 1597 | assert_eq!(*x, array_of_u8s);
|
| 1598 | }
|
| 1599 |
|
| 1600 | |
| 1601 | let mut array_of_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
|
| 1602 | let mut array_of_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
|
| 1603 | let x: &mut Nkl<[[u8; 2]; 4]> = transmute_mut!(&mut array_of_u8s);
|
| 1604 | assert_eq!(*x, array_of_arrays);
|
| 1605 | let x: &mut Nkl<[u8; 8]> = transmute_mut!(&mut array_of_arrays);
|
| 1606 | assert_eq!(*x, array_of_u8s);
|
| 1607 |
|
| 1608 | |
| 1609 | let mut u = AU64(0);
|
| 1610 | let array = [0, 0, 0, 0, 0, 0, 0, 0];
|
| 1611 | let x: &[u8; 8] = transmute_mut!(&mut u);
|
| 1612 | assert_eq!(*x, array);
|
| 1613 |
|
| 1614 | |
| 1615 | let mut x = 0u8;
|
| 1616 | #[allow(clippy::useless_transmute)]
|
| 1617 | let y: &u8 = transmute_mut!(&mut x);
|
| 1618 | assert_eq!(*y, 0);
|
| 1619 |
|
| 1620 | |
| 1621 | |
| 1622 | let mut bytes = [0, 1, 2, 3, 4, 5, 6];
|
| 1623 | let slice_dst_of_u8s = SliceDst::<u8, [u8; 2]>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1624 | let mut bytes = [0, 1, 2, 3, 4, 5, 6];
|
| 1625 | let slice_dst_of_u16s = SliceDst::<u8, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1626 | let x: &mut SliceDst<u8, U16> = transmute_mut!(slice_dst_of_u8s);
|
| 1627 | assert_eq!(x, slice_dst_of_u16s);
|
| 1628 |
|
| 1629 | |
| 1630 | |
| 1631 | let array_of_u16s: &mut [u16] = &mut [0u16, 1, 2];
|
| 1632 | let array_of_i16s: &mut [i16] = &mut [0i16, 1, 2];
|
| 1633 | let x: &mut [i16] = transmute_mut!(array_of_u16s);
|
| 1634 | assert_eq!(x, array_of_i16s);
|
| 1635 |
|
| 1636 | |
| 1637 | |
| 1638 | let mut bytes = [0, 1, 2, 3, 4, 5, 6, 7];
|
| 1639 | let slice_dst_big = SliceDst::<U32, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1640 | let mut bytes = [0, 1, 2, 3, 4, 5, 6, 7];
|
| 1641 | let slice_dst_small = SliceDst::<U16, u8>::mut_from_bytes(&mut bytes[..]).unwrap();
|
| 1642 | let x: &mut SliceDst<U16, u8> = transmute_mut!(slice_dst_big);
|
| 1643 | assert_eq!(x, slice_dst_small);
|
| 1644 |
|
| 1645 | |
| 1646 | let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
|
| 1647 | let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
|
| 1648 | let slice_of_arrays = &mut array_of_arrays[..];
|
| 1649 | let x: &mut [[u8; 2]] = transmute_mut!(&mut array_of_u8s);
|
| 1650 | assert_eq!(x, slice_of_arrays);
|
| 1651 | }
|
| 1652 |
|
| 1653 | #[test]
|
| 1654 | fn test_macros_evaluate_args_once() {
|
| 1655 | let mut ctr = 0;
|
| 1656 | #[allow(clippy::useless_transmute)]
|
| 1657 | let _: usize = transmute!({
|
| 1658 | ctr += 1;
|
| 1659 | 0usize
|
| 1660 | });
|
| 1661 | assert_eq!(ctr, 1);
|
| 1662 |
|
| 1663 | let mut ctr = 0;
|
| 1664 | let _: &usize = transmute_ref!({
|
| 1665 | ctr += 1;
|
| 1666 | &0usize
|
| 1667 | });
|
| 1668 | assert_eq!(ctr, 1);
|
| 1669 |
|
| 1670 | let mut ctr: usize = 0;
|
| 1671 | let _: &mut usize = transmute_mut!({
|
| 1672 | ctr += 1;
|
| 1673 | &mut ctr
|
| 1674 | });
|
| 1675 | assert_eq!(ctr, 1);
|
| 1676 |
|
| 1677 | let mut ctr = 0;
|
| 1678 | #[allow(clippy::useless_transmute)]
|
| 1679 | let _: usize = try_transmute!({
|
| 1680 | ctr += 1;
|
| 1681 | 0usize
|
| 1682 | })
|
| 1683 | .unwrap();
|
| 1684 | assert_eq!(ctr, 1);
|
| 1685 | }
|
| 1686 |
|
| 1687 | #[test]
|
| 1688 | fn test_include_value() {
|
| 1689 | const AS_U32: u32 = include_value!("../testdata/include_value/data");
|
| 1690 | assert_eq!(AS_U32, u32::from_ne_bytes([b'a', b'b', b'c', b'd']));
|
| 1691 | const AS_I32: i32 = include_value!("../testdata/include_value/data");
|
| 1692 | assert_eq!(AS_I32, i32::from_ne_bytes([b'a', b'b', b'c', b'd']));
|
| 1693 | }
|
| 1694 |
|
| 1695 | #[test]
|
| 1696 | #[allow(non_camel_case_types, unreachable_pub, dead_code)]
|
| 1697 | fn test_cryptocorrosion_derive_traits() {
|
| 1698 | |
| 1699 | |
| 1700 |
|
| 1701 | fn assert_impls<T: FromBytes + IntoBytes + Immutable>() {}
|
| 1702 |
|
| 1703 | cryptocorrosion_derive_traits! {
|
| 1704 | #[repr(C)]
|
| 1705 | #[derive(Clone, Copy)]
|
| 1706 | pub union vec128_storage {
|
| 1707 | d: [u32; 4],
|
| 1708 | q: [u64; 2],
|
| 1709 | }
|
| 1710 | }
|
| 1711 |
|
| 1712 | assert_impls::<vec128_storage>();
|
| 1713 |
|
| 1714 | cryptocorrosion_derive_traits! {
|
| 1715 | #[repr(transparent)]
|
| 1716 | #[derive(Copy, Clone, Debug, PartialEq)]
|
| 1717 | pub struct u32x4_generic([u32; 4]);
|
| 1718 | }
|
| 1719 |
|
| 1720 | assert_impls::<u32x4_generic>();
|
| 1721 |
|
| 1722 | cryptocorrosion_derive_traits! {
|
| 1723 | #[repr(transparent)]
|
| 1724 | #[derive(Copy, Clone, Debug, PartialEq)]
|
| 1725 | pub struct u64x2_generic([u64; 2]);
|
| 1726 | }
|
| 1727 |
|
| 1728 | assert_impls::<u64x2_generic>();
|
| 1729 |
|
| 1730 | cryptocorrosion_derive_traits! {
|
| 1731 | #[repr(transparent)]
|
| 1732 | #[derive(Copy, Clone, Debug, PartialEq)]
|
| 1733 | pub struct u128x1_generic([u128; 1]);
|
| 1734 | }
|
| 1735 |
|
| 1736 | assert_impls::<u128x1_generic>();
|
| 1737 |
|
| 1738 | cryptocorrosion_derive_traits! {
|
| 1739 | #[repr(transparent)]
|
| 1740 | #[derive(Copy, Clone, Default)]
|
| 1741 | #[allow(non_camel_case_types)]
|
| 1742 | pub struct x2<W, G>(pub [W; 2], PhantomData<G>);
|
| 1743 | }
|
| 1744 |
|
| 1745 | enum NotZerocopy {}
|
| 1746 | assert_impls::<x2<(), NotZerocopy>>();
|
| 1747 |
|
| 1748 | cryptocorrosion_derive_traits! {
|
| 1749 | #[repr(transparent)]
|
| 1750 | #[derive(Copy, Clone, Default)]
|
| 1751 | #[allow(non_camel_case_types)]
|
| 1752 | pub struct x4<W>(pub [W; 4]);
|
| 1753 | }
|
| 1754 |
|
| 1755 | assert_impls::<x4<()>>();
|
| 1756 |
|
| 1757 | #[cfg(feature = "simd")]
|
| 1758 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
| 1759 | {
|
| 1760 | #[cfg(target_arch = "x86")]
|
| 1761 | use core::arch::x86::{__m128i, __m256i};
|
| 1762 | #[cfg(target_arch = "x86_64")]
|
| 1763 | use core::arch::x86_64::{__m128i, __m256i};
|
| 1764 |
|
| 1765 | cryptocorrosion_derive_traits! {
|
| 1766 | #[repr(C)]
|
| 1767 | #[derive(Copy, Clone)]
|
| 1768 | pub struct X4(__m128i, __m128i, __m128i, __m128i);
|
| 1769 | }
|
| 1770 |
|
| 1771 | assert_impls::<X4>();
|
| 1772 |
|
| 1773 | cryptocorrosion_derive_traits! {
|
| 1774 | #[repr(C)]
|
| 1775 | |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | #[allow(non_camel_case_types)]
|
| 1780 | #[derive(Copy, Clone)]
|
| 1781 | pub union vec128_storage {
|
| 1782 | u32x4: [u32; 4],
|
| 1783 | u64x2: [u64; 2],
|
| 1784 | u128x1: [u128; 1],
|
| 1785 | sse2: __m128i,
|
| 1786 | }
|
| 1787 | }
|
| 1788 |
|
| 1789 | assert_impls::<vec128_storage>();
|
| 1790 |
|
| 1791 | cryptocorrosion_derive_traits! {
|
| 1792 | #[repr(transparent)]
|
| 1793 | #[allow(non_camel_case_types)]
|
| 1794 | #[derive(Copy, Clone)]
|
| 1795 | pub struct vec<S3, S4, NI> {
|
| 1796 | x: __m128i,
|
| 1797 | s3: PhantomData<S3>,
|
| 1798 | s4: PhantomData<S4>,
|
| 1799 | ni: PhantomData<NI>,
|
| 1800 | }
|
| 1801 | }
|
| 1802 |
|
| 1803 | assert_impls::<vec<NotZerocopy, NotZerocopy, NotZerocopy>>();
|
| 1804 |
|
| 1805 | cryptocorrosion_derive_traits! {
|
| 1806 | #[repr(transparent)]
|
| 1807 | #[derive(Copy, Clone)]
|
| 1808 | pub struct u32x4x2_avx2<NI> {
|
| 1809 | x: __m256i,
|
| 1810 | ni: PhantomData<NI>,
|
| 1811 | }
|
| 1812 | }
|
| 1813 |
|
| 1814 | assert_impls::<u32x4x2_avx2<NotZerocopy>>();
|
| 1815 | }
|
| 1816 |
|
| 1817 | |
| 1818 | |
| 1819 | cryptocorrosion_derive_traits! {
|
| 1820 | #[repr(C)]
|
| 1821 | #[derive(Copy, Clone, Debug, PartialEq)]
|
| 1822 | pub struct ReprC(u8, u8, u16);
|
| 1823 | }
|
| 1824 | }
|
| 1825 | }
|