use epui::EquisizedPrimitiveUnsignedIntExt as EPUI;
try_impl_generic_const_fn_for_trait!(
MidpointViaCpp20StdImplementationExt::midpoint_via_cpp_20_std_implementation
);
pub trait MidpointViaCpp20StdImplementationExt {
#[must_use]
fn midpoint_via_cpp_20_std_implementation(&self , b_ref: &Self) -> Self;
}
macro_rules! impl_midpoint_fn_for_t {
() => {
fn midpoint_via_cpp_20_std_implementation(&self , b_ref: &Self) -> Self {
let (a, b) = (*self, *b_ref);
let (u_a, u_b) = (
a as <Self as EPUI>::EquisizedPrimitiveUnsignedInt,
b as <Self as EPUI>::EquisizedPrimitiveUnsignedInt,
);
if a > b {
a.wrapping_sub(((u_a - u_b) / 2) as Self)
} else {
a.wrapping_add(((u_b - u_a) / 2) as Self)
}
}
};
}
impl_for_all_prim_ints!(
trait = MidpointViaCpp20StdImplementationExt,
fn macro = impl_midpoint_fn_for_t
);
#[cfg(test)]
mod tests {
use crate::MidpointViaCpp20StdImplementationExt;
#[test]
fn midpoint_via_cpp_20_std_implementation_rounds_towards_left_arg_including_when_args_are_positive(
) {
let result: i32 = 2.midpoint_via_cpp_20_std_implementation(&3);
assert_eq!(result, 2);
}
#[test]
fn midpoint_via_cpp_20_std_implementation_rounds_towards_left_arg_including_when_args_are_negative(
) {
let result: i32 = (-3).midpoint_via_cpp_20_std_implementation(&-2);
assert_eq!(result, -3);
}
}