use primitive_promotion::PrimitivePromotionExt as PP;
try_impl_generic_const_fn_for_trait!(
MidpointViaPrimitivePromotionExt::midpoint_via_primitive_promotion
);
pub trait MidpointViaPrimitivePromotionExt: PP {
#[must_use]
fn midpoint_via_primitive_promotion(&self , rhs_ref: &Self) -> Self;
}
macro_rules! impl_midpoint_fn_for_t {
() => {
fn midpoint_via_primitive_promotion(&self , rhs_ref: &Self) -> Self {
let (lhs, rhs) = (
*self as <Self as PP>::PrimitivePromotion,
*rhs_ref as <Self as PP>::PrimitivePromotion,
);
((lhs + rhs) / 2) as Self
}
};
}
impl_for_prim_ints_with_prim_promotion!(
trait = MidpointViaPrimitivePromotionExt,
fn macro = impl_midpoint_fn_for_t
);
#[cfg(test)]
mod tests {
use crate::MidpointViaPrimitivePromotionExt;
#[test]
fn midpoint_via_primitive_promotion_rounds_towards_zero_including_when_args_are_positive() {
let result: i32 = 2.midpoint_via_primitive_promotion(&3);
assert_eq!(result, 2);
}
#[test]
fn midpoint_via_primitive_promotion_rounds_towards_zero_including_when_args_are_negative() {
let result: i32 = (-3).midpoint_via_primitive_promotion(&-2);
assert_eq!(result, -2);
}
}