Skip to main content

Midpoint

Trait Midpoint 

Source
pub trait Midpoint: Sized {
    type Output;

    // Required method
    fn midpoint(self, rhs: Self) -> Self::Output;
}
Expand description

Computes the midpoint (average) of two values without overflowing.

Required Associated Types§

Source

type Output

Calculates the midpoint (self + rhs) / 2 as if it were performed in a sufficiently-large type, so it never overflows. The result is rounded towards zero.

use const_num_traits::Midpoint;

assert_eq!(Midpoint::midpoint(u8::MAX, u8::MAX), u8::MAX);
assert_eq!(Midpoint::midpoint(0u8, 7), 3);
assert_eq!(Midpoint::midpoint(-7i8, 0), -3);

Required Methods§

Source

fn midpoint(self, rhs: Self) -> Self::Output

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Midpoint for i8

Source§

type Output = i8

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for i16

Source§

type Output = i16

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for i32

Source§

type Output = i32

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for i64

Source§

type Output = i64

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for i128

Source§

type Output = i128

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for isize

Source§

type Output = isize

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for u8

Source§

type Output = u8

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for u16

Source§

type Output = u16

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for u32

Source§

type Output = u32

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for u64

Source§

type Output = u64

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for u128

Source§

type Output = u128

Source§

fn midpoint(self, rhs: Self) -> Self

Source§

impl Midpoint for usize

Source§

type Output = usize

Source§

fn midpoint(self, rhs: Self) -> Self

Implementors§