try_impl_generic_const_fn_for_trait!(
MidpointViaBitwiseOpsExt::midpoint_via_bitwise_ops
);
pub trait MidpointViaBitwiseOpsExt {
#[must_use]
fn midpoint_via_bitwise_ops(&self , rhs_ref: &Self) -> Self;
}
macro_rules! impl_midpoint_fn_for_t {
() => {
fn midpoint_via_bitwise_ops(&self , rhs_ref: &Self) -> Self {
let (lhs, rhs) = (*self, *rhs_ref);
let (half_lhs, half_rhs) = (lhs / 2, rhs / 2);
let lsb_masked_bitwise_or = lhs & rhs & 0x1;
sum_without_overflow!(half_lhs, half_rhs, lsb_masked_bitwise_or)
}
};
}
impl_for_all_prim_ints!(
trait = MidpointViaBitwiseOpsExt,
fn macro = impl_midpoint_fn_for_t
);
#[cfg(test)]
mod tests {
use crate::MidpointViaBitwiseOpsExt;
#[test]
fn midpoint_via_bitwise_ops_rounds_towards_zero_including_when_args_are_positive() {
let result: i32 = 2.midpoint_via_bitwise_ops(&3);
assert_eq!(result, 2);
}
#[test]
fn midpoint_via_bitwise_ops_rounds_towards_zero_including_when_args_are_negative() {
let result: i32 = (-3).midpoint_via_bitwise_ops(&-2);
assert_eq!(result, -2);
}
}