rend/lib.rs
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 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 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 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 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 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 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
//! # rend
//!
//! rend provides cross-platform, endian-aware primitives for Rust.
//!
//! rend does not provide cross-platform alternatives for types that are
//! inherently cross-platform, such as `bool` and `u8`. It also does not provide
//! cross-platform alternatives for types that have an architecture-dependent
//! size, such as `isize` and `usize`. rend does not support custom types.
//!
//! rend is intended to be used to build portable types that can be shared
//! between different architectures.
//!
//! ## Features
//!
//! - `bytecheck`: Enables support for validating types using `bytecheck`.
//!
//! ## Crates
//!
//! - `zerocopy-0_8`
//!
//! ## Example:
#![doc = include_str!("../example.md")]
#![no_std]
#![deny(
future_incompatible,
missing_docs,
nonstandard_style,
unsafe_op_in_unsafe_fn,
unused,
warnings,
clippy::all,
clippy::missing_safety_doc,
clippy::undocumented_unsafe_blocks,
rustdoc::broken_intra_doc_links,
rustdoc::missing_crate_level_docs
)]
#![cfg_attr(all(docsrs, not(doctest)), feature(doc_cfg, doc_auto_cfg))]
#[macro_use]
mod common;
#[cfg(feature = "bytecheck")]
mod context;
#[macro_use]
mod traits;
#[macro_use]
mod util;
pub mod unaligned;
#[cfg(target_has_atomic = "16")]
use core::sync::atomic::{AtomicI16, AtomicU16};
#[cfg(target_has_atomic = "32")]
use core::sync::atomic::{AtomicI32, AtomicU32};
#[cfg(target_has_atomic = "64")]
use core::sync::atomic::{AtomicI64, AtomicU64};
use core::{
num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64,
},
sync::atomic::Ordering,
};
// `rustfmt` keeps changing the indentation of the attributes in this macro.
#[rustfmt::skip]
macro_rules! define_newtype {
(
$(#[$attr:meta])*
$name:ident: $endian:ident $size_align:literal $prim:ty
) => {
#[allow(non_camel_case_types)]
#[doc = concat!(
"A ",
endian_name!($endian),
"-endian `",
stringify!($prim),
"` with a guaranteed size and alignment of `",
stringify!($size_align),
"`.",
)]
$(#[$attr])*
#[repr(C, align($size_align))]
pub struct $name($prim);
};
}
macro_rules! define_signed_integer {
($name:ident: $endian:ident $size_align:literal $prim:ident) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
zerocopy_derive::FromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::Immutable,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian $size_align $prim
);
impl_integer!($name: $endian $prim);
impl_signed_integer_traits!($name: $endian $prim);
};
}
macro_rules! define_signed_integers {
($($le:ident $be:ident: $size_align:literal $prim:ident),* $(,)?) => {
$(
define_signed_integer!($le: little $size_align $prim);
define_signed_integer!($be: big $size_align $prim);
)*
};
}
define_signed_integers! {
i16_le i16_be: 2 i16,
i32_le i32_be: 4 i32,
i64_le i64_be: 8 i64,
i128_le i128_be: 16 i128,
}
macro_rules! define_unsigned_integer {
($name:ident: $endian:ident $size_align:literal $prim:ident) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
zerocopy_derive::FromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::Immutable,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian $size_align $prim
);
impl_integer!($name: $endian $prim);
impl_unsigned_integer_traits!($name: $endian $prim);
}
}
macro_rules! define_unsigned_integers {
($($le:ident $be:ident: $size_align:literal $prim:ident),* $(,)?) => {
$(
define_unsigned_integer!($le: little $size_align $prim);
define_unsigned_integer!($be: big $size_align $prim);
)*
};
}
define_unsigned_integers! {
u16_le u16_be: 2 u16,
u32_le u32_be: 4 u32,
u64_le u64_be: 8 u64,
u128_le u128_be: 16 u128,
}
macro_rules! define_float {
(
$name:ident:
$endian:ident $size_align:literal $prim:ty as $prim_int:ty
) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
zerocopy_derive::FromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::Immutable,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian $size_align $prim
);
impl_float!($name: $endian $prim as $prim_int);
};
}
macro_rules! define_floats {
($(
$le:ident $be:ident:
$size_align:literal $prim:ty as $prim_int:ty
),* $(,)?) => {
$(
define_float!($le: little $size_align $prim as $prim_int);
define_float!($be: big $size_align $prim as $prim_int);
)*
};
}
define_floats! {
f32_le f32_be: 4 f32 as u32,
f64_le f64_be: 8 f64 as u64,
}
macro_rules! define_char {
($name:ident: $endian:ident) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
// The generated impl for `zerocopy::TryFromBytes` is overly
// permissive. The derive macro doesn't understand that even
// though this struct only contains a `u32`, it still has a
// restricted set of valid bit patterns. Because
// `zerocopy::TryFromBytes` has hidden, semver-breaking
// members, I can't write a manual impl. So no impl for you.
//
// zerocopy_derive::TryFromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::Immutable,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian 4 u32
);
impl_char!($name: $endian);
};
}
define_char!(char_le: little);
define_char!(char_be: big);
macro_rules! define_nonzero {
(
$name:ident:
$endian:ident $size_align:literal $prim:ty as $prim_int:ty
) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
zerocopy_derive::TryFromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::Immutable,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian $size_align $prim
);
impl_nonzero!($name: $endian $prim as $prim_int);
#[cfg(feature = "bytecheck")]
// SAFETY: `check_bytes` only returns `Ok` if `value` points to a valid
// non-zero value, which is the only requirement for `NonZero` integers.
unsafe impl<C> bytecheck::CheckBytes<C> for $name
where
C: bytecheck::rancor::Fallible + ?Sized,
C::Error: bytecheck::rancor::Trace,
$prim: bytecheck::CheckBytes<C>,
{
#[inline]
unsafe fn check_bytes(
value: *const Self,
context: &mut C,
) -> Result<(), C::Error> {
use bytecheck::rancor::ResultExt as _;
// SAFETY: `value` points to a `Self`, which has the same size
// as a `$prim` and is at least as aligned as one. Note that the
// bit pattern for 0 is always the same regardless of
// endianness.
unsafe {
<$prim>::check_bytes(value.cast(), context)
.with_trace(|| $crate::context::ValueCheckContext {
inner_name: core::stringify!($prim),
outer_name: core::stringify!($name),
})
}
}
}
};
}
macro_rules! define_nonzeros {
($(
$le:ident $be:ident:
$size_align:literal $prim:ty as $prim_int:ty
),* $(,)?) => {
$(
define_nonzero!($le: little $size_align $prim as $prim_int);
define_nonzero!($be: big $size_align $prim as $prim_int);
)*
}
}
define_nonzeros! {
NonZeroI16_le NonZeroI16_be: 2 NonZeroI16 as i16,
NonZeroI32_le NonZeroI32_be: 4 NonZeroI32 as i32,
NonZeroI64_le NonZeroI64_be: 8 NonZeroI64 as i64,
NonZeroI128_le NonZeroI128_be: 16 NonZeroI128 as i128,
NonZeroU16_le NonZeroU16_be: 2 NonZeroU16 as u16,
NonZeroU32_le NonZeroU32_be: 4 NonZeroU32 as u32,
NonZeroU64_le NonZeroU64_be: 8 NonZeroU64 as u64,
NonZeroU128_le NonZeroU128_be: 16 NonZeroU128 as u128,
}
#[allow(dead_code)]
const fn fetch_ordering(order: Ordering) -> Ordering {
match order {
Ordering::Relaxed => Ordering::Relaxed,
Ordering::Release => Ordering::Relaxed,
Ordering::Acquire => Ordering::Acquire,
Ordering::AcqRel => Ordering::Acquire,
Ordering::SeqCst => Ordering::SeqCst,
order => order,
}
}
#[cfg(any(
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
))]
macro_rules! define_atomic {
(
$name:ident:
$endian:ident $size_align:literal $prim:ty as $prim_int:ty
) => {
define_newtype!(
#[cfg_attr(
feature = "zerocopy-0_8",
derive(
zerocopy_derive::FromBytes,
zerocopy_derive::IntoBytes,
zerocopy_derive::KnownLayout,
),
)]
$name: $endian $size_align $prim
);
impl $name {
#[doc = concat!(
"Returns a `",
stringify!($name),
"` containing `value`.",
)]
#[inline]
pub const fn new(value: $prim_int) -> Self {
Self(<$prim>::new(swap_endian!($endian value)))
}
}
// SAFETY: An impl of `CheckBytes` with a `check_bytes` function that is
// a no-op is sound for atomic integers.
unsafe_impl_check_bytes_noop!(for $name);
impl $name {
/// Stores a value into the atomic integer if the current value is
/// the same as the `current` value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::compare_exchange`] for more information.",
)]
#[inline]
pub fn compare_exchange(
&self,
current: $prim_int,
new: $prim_int,
success: Ordering,
failure: Ordering,
) -> Result<$prim_int, $prim_int> {
match self.0.compare_exchange(
swap_endian!($endian current),
swap_endian!($endian new),
success,
failure,
) {
Ok(x) => Ok(swap_endian!($endian x)),
Err(x) => Err(swap_endian!($endian x)),
}
}
/// Stores a value into the atomic integer if the current value is
/// the same as the `current` value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::compare_exchange_weak`] for more information.",
)]
#[inline]
pub fn compare_exchange_weak(
&self,
current: $prim_int,
new: $prim_int,
success: Ordering,
failure: Ordering,
) -> Result<$prim_int, $prim_int> {
match self.0.compare_exchange_weak(
swap_endian!($endian current),
swap_endian!($endian new),
success,
failure,
) {
Ok(x) => Ok(swap_endian!($endian x)),
Err(x) => Ok(swap_endian!($endian x)),
}
}
/// Adds to the current value, returning the previous value.
///
#[doc = concat!(
"Because addition is not an endian-agnostic operation, ",
"`fetch_add` is implemented in terms of [`",
stringify!($prim),
"::compare_exchange_weak`] on ",
opposite_endian_name!($endian),
"-endian targets. This may result in worse performance on ",
"those targets.",
)]
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_add`] for more information.",
)]
#[inline]
pub fn fetch_add(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
if_native_endian!(
$endian
self.0.fetch_add(val, order),
self.fetch_update_fast(
order,
fetch_ordering(order),
|x| x + val,
),
)
}
/// Bitwise "and" with the current value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_and`] for more information.",
)]
#[inline]
pub fn fetch_and(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
let val = swap_endian!($endian val);
swap_endian!($endian self.0.fetch_and(val, order))
}
/// Maximum with the current value.
///
#[doc = concat!(
"Because maximum is not an endian-agnostic operation, ",
"`fetch_max` is implemented in terms of [`",
stringify!($prim),
"::compare_exchange_weak`] on ",
opposite_endian_name!($endian),
"-endian targets. This may result in worse performance on ",
"those targets.",
)]
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_max`] for more information.",
)]
#[inline]
pub fn fetch_max(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
if_native_endian!(
$endian
self.0.fetch_max(val, order),
self.fetch_update_fast(
order,
fetch_ordering(order),
|x| <$prim_int>::max(x, val),
),
)
}
/// Minimum with the current value.
///
#[doc = concat!(
"Because minimum is not an endian-agnostic operation, ",
"`fetch_min` is implemented in terms of [`",
stringify!($prim),
"::compare_exchange_weak`] on ",
opposite_endian_name!($endian),
"-endian targets. This may result in worse performance on ",
"those targets.",
)]
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_min`] for more information.",
)]
#[inline]
pub fn fetch_min(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
if_native_endian!(
$endian
self.0.fetch_min(val, order),
self.fetch_update_fast(
order,
fetch_ordering(order),
|x| <$prim_int>::min(x, val),
),
)
}
/// Bitwise "nand" with the current value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_nand`] for more information.",
)]
#[inline]
pub fn fetch_nand(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
let val = swap_endian!($endian val);
swap_endian!($endian self.0.fetch_nand(val, order))
}
/// Bitwise "or" with the current value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_or`] for more information.",
)]
#[inline]
pub fn fetch_or(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
let val = swap_endian!($endian val);
swap_endian!($endian self.0.fetch_or(val, order))
}
/// Subtracts from the current value, returning the previous value.
///
#[doc = concat!(
"Because subtraction is not an endian-agnostic operation, ",
"`fetch_sub` is implemented in terms of [`",
stringify!($prim),
"::compare_exchange_weak`] on ",
opposite_endian_name!($endian),
"-endian targets. This may result in worse performance on ",
"those targets.",
)]
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_sub`] for more information.",
)]
#[inline]
pub fn fetch_sub(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
if_native_endian!(
$endian
self.0.fetch_sub(val, order),
self.fetch_update_fast(
order,
fetch_ordering(order),
|x| x - val,
),
)
}
#[allow(dead_code)]
#[inline(always)]
fn fetch_update_fast<F: Fn($prim_int) -> $prim_int>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> $prim_int {
let mut prev = swap_endian!($endian self.0.load(fetch_order));
loop {
let next = swap_endian!($endian f(prev));
match self.0.compare_exchange_weak(
prev,
next,
set_order,
fetch_order,
) {
Ok(x) => break x,
Err(next_prev) => {
prev = swap_endian!($endian next_prev);
}
}
}
}
/// Fetches the value, and applies a function to it that returns an
/// optional new value. Returns a `Result` of `Ok(previous_value)`
/// if the function returned `Some(_)`, else `Err(previous_value)`.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_update`] for more information.",
)]
#[inline]
pub fn fetch_update<F: FnMut($prim_int) -> Option<$prim_int>>(
&self,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> Result<$prim_int, $prim_int> {
self.0.fetch_update(set_order, fetch_order, |x| {
f(swap_endian!($endian x)).map(|y| swap_endian!($endian y))
})
}
/// Bitwise "xor" with the current value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::fetch_xor`] for more information.",
)]
#[inline]
pub fn fetch_xor(
&self,
val: $prim_int,
order: Ordering,
) -> $prim_int {
let val = swap_endian!($endian val);
swap_endian!($endian self.0.fetch_xor(val, order))
}
/// Consumes the atomic and returns the contained value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::into_inner`] for more information.",
)]
#[inline]
pub fn into_inner(self) -> $prim_int {
swap_endian!($endian self.0.into_inner())
}
/// Loads a value from the atomic integer.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::load`] for more information.",
)]
#[inline]
pub fn load(&self, order: Ordering) -> $prim_int {
swap_endian!($endian self.0.load(order))
}
/// Stores a value into the atomic integer.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::store`] for more information.",
)]
#[inline]
pub fn store(&self, val: $prim_int, order: Ordering) {
self.0.store(swap_endian!($endian val), order);
}
/// Stores a value into the atomic integer, returning the previous
/// value.
///
#[doc = concat!(
"See [`",
stringify!($prim),
"::swap`] for more information.",
)]
#[inline]
pub fn swap(&self, val: $prim_int, order: Ordering) -> $prim_int {
let val = swap_endian!($endian val);
swap_endian!($endian self.0.swap(val, order))
}
}
impl core::fmt::Debug for $name {
#[inline]
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
swap_endian!($endian self.load(Ordering::Relaxed)).fmt(f)
}
}
impl Default for $name {
#[inline]
fn default() -> Self {
Self::new(<$prim_int>::default())
}
}
impl From<$prim_int> for $name {
#[inline]
fn from(value: $prim_int) -> Self {
Self::new(value)
}
}
}
}
#[cfg(any(
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
))]
macro_rules! define_atomics {
($(
$le:ident $be:ident:
$size_align:literal $prim:ty as $prim_int:ty
),* $(,)?) => {
$(
define_atomic!($le: little $size_align $prim as $prim_int);
define_atomic!($be: big $size_align $prim as $prim_int);
)*
}
}
#[cfg(target_has_atomic = "16")]
define_atomics! {
AtomicI16_le AtomicI16_be: 2 AtomicI16 as i16,
AtomicU16_le AtomicU16_be: 2 AtomicU16 as u16,
}
#[cfg(target_has_atomic = "32")]
define_atomics! {
AtomicI32_le AtomicI32_be: 4 AtomicI32 as i32,
AtomicU32_le AtomicU32_be: 4 AtomicU32 as u32,
}
#[cfg(target_has_atomic = "64")]
define_atomics! {
AtomicI64_le AtomicI64_be: 8 AtomicI64 as i64,
AtomicU64_le AtomicU64_be: 8 AtomicU64 as u64,
}
#[cfg(test)]
mod tests {
use core::mem::transmute;
use super::*;
#[test]
fn signed_integers() {
assert_size_align! {
i16_be 2 2,
i16_le 2 2,
i32_be 4 4,
i32_le 4 4,
i64_be 8 8,
i64_le 8 8,
i128_be 16 16,
i128_le 16 16,
}
unsafe {
// i16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(i16_le::from_native(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(i16_be::from_native(0x0102)),
);
// i32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(i32_le::from_native(0x01020304)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(i32_be::from_native(0x01020304)),
);
// i64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(i64_le::from_native(
0x0102030405060708
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(i64_be::from_native(
0x0102030405060708
)),
);
// i128
assert_eq!(
[
0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
0x06, 0x05, 0x04, 0x03, 0x02, 0x01
],
transmute::<_, [u8; 16]>(i128_le::from_native(
0x0102030405060708090a0b0c0d0e0f10
)),
);
assert_eq!(
[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
],
transmute::<_, [u8; 16]>(i128_be::from_native(
0x0102030405060708090a0b0c0d0e0f10
)),
);
}
}
#[test]
fn unsigned_integers() {
assert_size_align! {
u16_be 2 2,
u16_le 2 2,
u32_be 4 4,
u32_le 4 4,
u64_be 8 8,
u64_le 8 8,
u128_be 16 16,
u128_le 16 16,
}
unsafe {
// u16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(u16_le::from_native(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(u16_be::from_native(0x0102)),
);
// u32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(u32_le::from_native(0x01020304)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(u32_be::from_native(0x01020304)),
);
// u64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(u64_le::from_native(
0x0102030405060708
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(u64_be::from_native(
0x0102030405060708
)),
);
// u128
assert_eq!(
[
0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
0x06, 0x05, 0x04, 0x03, 0x02, 0x01
],
transmute::<_, [u8; 16]>(u128_le::from_native(
0x0102030405060708090a0b0c0d0e0f10
)),
);
assert_eq!(
[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
],
transmute::<_, [u8; 16]>(u128_be::from_native(
0x0102030405060708090a0b0c0d0e0f10
)),
);
}
}
#[test]
fn floats() {
assert_size_align! {
f32_be 4 4,
f32_le 4 4,
f64_be 8 8,
f64_le 8 8,
}
unsafe {
// f32
assert_eq!(
[0xdb, 0x0f, 0x49, 0x40],
transmute::<_, [u8; 4]>(f32_le::from_native(
core::f32::consts::PI
)),
);
assert_eq!(
[0x40, 0x49, 0x0f, 0xdb],
transmute::<_, [u8; 4]>(f32_be::from_native(
core::f32::consts::PI
)),
);
// f64
assert_eq!(
[0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40],
transmute::<_, [u8; 8]>(f64_le::from_native(
core::f64::consts::PI
)),
);
assert_eq!(
[0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18],
transmute::<_, [u8; 8]>(f64_be::from_native(
core::f64::consts::PI
)),
);
// char
assert_eq!(
[0x89, 0xf3, 0x01, 0x00],
transmute::<_, [u8; 4]>(char_le::from_native('🎉')),
);
assert_eq!(
[0x00, 0x01, 0xf3, 0x89],
transmute::<_, [u8; 4]>(char_be::from_native('🎉')),
);
}
}
#[test]
fn signed_non_zero() {
assert_size_align! {
NonZeroI16_le 2 2,
NonZeroI16_be 2 2,
NonZeroI32_le 4 4,
NonZeroI32_be 4 4,
NonZeroI64_le 8 8,
NonZeroI64_be 8 8,
NonZeroI128_le 16 16,
NonZeroI128_be 16 16,
}
unsafe {
// NonZeroI16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(NonZeroI16_le::new_unchecked(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(NonZeroI16_be::new_unchecked(0x0102)),
);
// NonZeroI32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(NonZeroI32_le::new_unchecked(
0x01020304
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(NonZeroI32_be::new_unchecked(
0x01020304
)),
);
// NonZeroI64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(NonZeroI64_le::new_unchecked(
0x0102030405060708
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(NonZeroI64_be::new_unchecked(
0x0102030405060708
)),
);
// NonZeroI128
assert_eq!(
[
0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
0x06, 0x05, 0x04, 0x03, 0x02, 0x01
],
transmute::<_, [u8; 16]>(NonZeroI128_le::new_unchecked(
0x0102030405060708090a0b0c0d0e0f10
)),
);
assert_eq!(
[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
],
transmute::<_, [u8; 16]>(NonZeroI128_be::new_unchecked(
0x0102030405060708090a0b0c0d0e0f10
)),
);
}
}
#[test]
fn unsigned_non_zero() {
assert_size_align! {
NonZeroU16_le 2 2,
NonZeroU16_be 2 2,
NonZeroU32_le 4 4,
NonZeroU32_be 4 4,
NonZeroU64_le 8 8,
NonZeroU64_be 8 8,
NonZeroU128_le 16 16,
NonZeroU128_be 16 16,
}
unsafe {
// NonZeroU16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(NonZeroU16_le::new_unchecked(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(NonZeroU16_be::new_unchecked(0x0102)),
);
// NonZeroU32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(NonZeroU32_le::new_unchecked(
0x01020304
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(NonZeroU32_be::new_unchecked(
0x01020304
)),
);
// NonZeroU64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(NonZeroU64_le::new_unchecked(
0x0102030405060708
)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(NonZeroU64_be::new_unchecked(
0x0102030405060708
)),
);
// NonZeroU128
assert_eq!(
[
0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
0x06, 0x05, 0x04, 0x03, 0x02, 0x01
],
transmute::<_, [u8; 16]>(NonZeroU128_le::new_unchecked(
0x0102030405060708090a0b0c0d0e0f10
)),
);
assert_eq!(
[
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
],
transmute::<_, [u8; 16]>(NonZeroU128_be::new_unchecked(
0x0102030405060708090a0b0c0d0e0f10
)),
);
}
}
#[cfg(feature = "bytecheck")]
#[test]
fn unaligned_non_zero() {
use bytecheck::{
rancor::{Failure, Strategy},
CheckBytes,
};
use unaligned::{u32_ule, NonZeroU32_ule};
let zero = u32_ule::from_native(0);
let ptr = (&zero as *const u32_ule).cast::<NonZeroU32_ule>();
let mut unit = ();
let context = Strategy::<_, Failure>::wrap(&mut unit);
unsafe {
<NonZeroU32_ule as CheckBytes<Strategy<(), Failure>>>::check_bytes(
ptr, context,
)
.unwrap_err();
}
}
#[cfg(target_has_atomic = "16")]
#[test]
fn atomics_16() {
assert_size_align! {
AtomicI16_le 2 2,
AtomicI16_be 2 2,
AtomicU16_le 2 2,
AtomicU16_be 2 2,
}
unsafe {
// AtomicI16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(AtomicI16_le::new(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(AtomicI16_be::new(0x0102)),
);
// AtomicU16
assert_eq!(
[0x02, 0x01],
transmute::<_, [u8; 2]>(AtomicU16_le::new(0x0102)),
);
assert_eq!(
[0x01, 0x02],
transmute::<_, [u8; 2]>(AtomicU16_be::new(0x0102)),
);
}
}
#[cfg(target_has_atomic = "32")]
#[test]
fn atomics_32() {
assert_size_align! {
AtomicI32_le 4 4,
AtomicI32_be 4 4,
AtomicU32_le 4 4,
AtomicU32_be 4 4,
}
unsafe {
// AtomicI32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(AtomicI32_le::new(0x01020304)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(AtomicI32_be::new(0x01020304)),
);
// AtomicU32
assert_eq!(
[0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 4]>(AtomicU32_le::new(0x01020304)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04],
transmute::<_, [u8; 4]>(AtomicU32_be::new(0x01020304)),
);
}
}
#[cfg(target_has_atomic = "64")]
#[test]
fn atomics_64() {
assert_size_align! {
AtomicI64_le 8 8,
AtomicI64_be 8 8,
AtomicU64_le 8 8,
AtomicU64_be 8 8,
}
unsafe {
// AtomicI64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(AtomicI64_le::new(0x0102030405060708)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(AtomicI64_be::new(0x0102030405060708)),
);
// AtomicU64
assert_eq!(
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01],
transmute::<_, [u8; 8]>(AtomicU64_le::new(0x0102030405060708)),
);
assert_eq!(
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
transmute::<_, [u8; 8]>(AtomicU64_be::new(0x0102030405060708)),
);
}
}
}