Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow picking and matching over pointer size as well #52

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 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(target_pointer_width = "..")] matches `$pointer_width`.
///
/// 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_width:literal)? => $expr:expr),+ $(, _ => $fallback:expr)? $(,)?) => {
match () {
$(
#[cfg(target_endian = $endian)]
#[cfg(all(target_endian = $endian $(, target_pointer_width = $pointer_width)*))]
() => $expr,
)*
#[cfg(not(any($(target_endian = $endian),*)))]
#[cfg(not(any($(all(target_endian = $endian $(, target_pointer_width = $pointer_width)*)),*)))]
() => $crate::__pick_fallback!($($fallback)*)
}
};
Expand All @@ -70,7 +71,9 @@ macro_rules! __pick_fallback {
};
}

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