Skip to content

Commit

Permalink
Allow picking and matching over pointer size as well
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 29, 2023
1 parent c7cc0d5 commit 69fd8a2
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions crates/musli-zerocopy/src/endian/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Marker types which define a [`ByteOrder`] to use.

/// A macro that picks which `$expr` to evaluate to based on if the current
/// `#[cfg(target_endian = "..")]` matches `$endian`.
/// `#[cfg(target_endian = "..")]` matches `$endian` and optionally
/// #[cfg(pointer_size = "..")] matches `$pointer_size`.
///
/// A fallback branch is supported with `_ => $expr`.
///
Expand Down Expand Up @@ -45,13 +46,13 @@
#[macro_export]
#[doc(hidden)]
macro_rules! __pick {
($($endian:literal => $expr:expr),+ $(, _ => $fallback:expr)? $(,)?) => {
($($endian:literal $(/ $pointer_size:literal)? => $expr:expr),+ $(, _ => $fallback:expr)? $(,)?) => {
match () {
$(
#[cfg(target_endian = $endian)]
#[cfg(all(target_endian = $endian $(, pointer_size = $pointer_size)*))]
() => $expr,
)*
#[cfg(not(any($(target_endian = $endian),*)))]
#[cfg(not(any($(all(target_endian = $endian $(, pointer_size = $pointer_size)*)),*)))]
() => $crate::__pick_fallback!($($fallback)*)
}
};
Expand All @@ -70,7 +71,8 @@ macro_rules! __pick_fallback {
};
}

/// A macro that matches `$expr` to the `$pat` if the current target is $endian.
/// A macro that matches `$expr` to the `$pat` if the current target is
/// `$endian` and optionally the pointer size is `$pointer_size`.
///
/// Note that if running on a platform which is not covered, the result will
/// always be `false`:
Expand Down Expand Up @@ -104,13 +106,13 @@ macro_rules! __pick_fallback {
#[macro_export]
#[doc(hidden)]
macro_rules! __matches {
($expr:expr, $($endian:literal => $pat:pat),+ $(,)?) => {
($expr:expr, $($endian:literal $(/ $pointer_size:literal)? => $pat:pat),+ $(,)?) => {
match $expr {
$(
#[cfg(target_endian = $endian)]
#[cfg(all(target_endian = $endian $(, pointer_size = $pointer_size)*))]
value => matches!(value, $pat),
)*
#[cfg(not(any($(target_endian = $endian),*)))]
#[cfg(not(any($(all(target_endian = $endian $(, pointer_size = $pointer_size)*)),*)))]
_ => false,
}
};
Expand Down

0 comments on commit 69fd8a2

Please sign in to comment.