try_impl_unsafe_generic_const_fn_for_trait!(
NaiveMidpointExt::naive_midpoint
);
pub trait NaiveMidpointExt {
#[must_use]
unsafe fn naive_midpoint(&self , rhs_ref: &Self) -> Self;
}
macro_rules! impl_midpoint_fn_for_t {
() => {
unsafe fn naive_midpoint(&self , rhs_ref: &Self) -> Self {
let (lhs, rhs) = (*self, *rhs_ref);
(lhs + rhs) / 2
}
};
}
impl_for_all_prim_ints!(
trait = NaiveMidpointExt,
fn macro = impl_midpoint_fn_for_t
);
#[cfg(test)]
mod tests {
use crate::NaiveMidpointExt;
#[test]
fn naive_midpoint_rounds_towards_zero_including_when_args_are_positive() {
let result: i32 = unsafe { 2.naive_midpoint(&3) };
assert_eq!(result, 2);
}
#[test]
fn naive_midpoint_rounds_towards_zero_including_when_args_are_negative() {
let result: i32 = unsafe { (-3).naive_midpoint(&-2) };
assert_eq!(result, -2);
}
}