Trait gstreamer::prelude::MulDiv

source ·
pub trait MulDiv<RHS = Self> {
    type Output;

    // Required methods
    fn mul_div_floor(self, num: RHS, denom: RHS) -> Option<Self::Output>;
    fn mul_div_round(self, num: RHS, denom: RHS) -> Option<Self::Output>;
    fn mul_div_ceil(self, num: RHS, denom: RHS) -> Option<Self::Output>;
}
Expand description

Trait for calculating val * num / denom with different rounding modes and overflow protection.

Implementations of this trait have to ensure that even if the result of the multiplication does not fit into the type, as long as it would fit after the division the correct result has to be returned instead of None. None only should be returned if the overall result does not fit into the type.

This specifically means that e.g. the u64 implementation must, depending on the arguments, be able to do 128 bit integer multiplication.

Required Associated Types§

source

type Output

Output type for the methods of this trait.

Required Methods§

source

fn mul_div_floor(self, num: RHS, denom: RHS) -> Option<Self::Output>

Calculates floor(val * num / denom), i.e. the largest integer less than or equal to the result of the division.

§Example
extern crate muldiv;
use muldiv::MulDiv;

let x = 3i8.mul_div_floor(4, 2);
assert_eq!(x, Some(6));

let x = 5i8.mul_div_floor(2, 3);
assert_eq!(x, Some(3));

let x = (-5i8).mul_div_floor(2, 3);
assert_eq!(x, Some(-4));

let x = 3i8.mul_div_floor(3, 2);
assert_eq!(x, Some(4));

let x = (-3i8).mul_div_floor(3, 2);
assert_eq!(x, Some(-5));

let x = 127i8.mul_div_floor(4, 3);
assert_eq!(x, None);
source

fn mul_div_round(self, num: RHS, denom: RHS) -> Option<Self::Output>

Calculates round(val * num / denom), i.e. the closest integer to the result of the division. If both surrounding integers are the same distance (x.5), the one with the bigger absolute value is returned (round away from 0.0).

§Example
extern crate muldiv;
use muldiv::MulDiv;

let x = 3i8.mul_div_round(4, 2);
assert_eq!(x, Some(6));

let x = 5i8.mul_div_round(2, 3);
assert_eq!(x, Some(3));

let x = (-5i8).mul_div_round(2, 3);
assert_eq!(x, Some(-3));

let x = 3i8.mul_div_round(3, 2);
assert_eq!(x, Some(5));

let x = (-3i8).mul_div_round(3, 2);
assert_eq!(x, Some(-5));

let x = 127i8.mul_div_round(4, 3);
assert_eq!(x, None);
source

fn mul_div_ceil(self, num: RHS, denom: RHS) -> Option<Self::Output>

Calculates ceil(val * num / denom), i.e. the the smallest integer greater than or equal to the result of the division.

§Example
extern crate muldiv;
use muldiv::MulDiv;

let x = 3i8.mul_div_ceil(4, 2);
assert_eq!(x, Some(6));

let x = 5i8.mul_div_ceil(2, 3);
assert_eq!(x, Some(4));

let x = (-5i8).mul_div_ceil(2, 3);
assert_eq!(x, Some(-3));

let x = 3i8.mul_div_ceil(3, 2);
assert_eq!(x, Some(5));

let x = (-3i8).mul_div_ceil(3, 2);
assert_eq!(x, Some(-4));

let x = (127i8).mul_div_ceil(4, 3);
assert_eq!(x, None);

Implementations on Foreign Types§

source§

impl MulDiv for i8

source§

type Output = i8

source§

fn mul_div_floor(self, num: i8, denom: i8) -> Option<i8>

source§

fn mul_div_round(self, num: i8, denom: i8) -> Option<i8>

source§

fn mul_div_ceil(self, num: i8, denom: i8) -> Option<i8>

source§

impl MulDiv for i16

source§

type Output = i16

source§

fn mul_div_floor(self, num: i16, denom: i16) -> Option<i16>

source§

fn mul_div_round(self, num: i16, denom: i16) -> Option<i16>

source§

fn mul_div_ceil(self, num: i16, denom: i16) -> Option<i16>

source§

impl MulDiv for i32

source§

type Output = i32

source§

fn mul_div_floor(self, num: i32, denom: i32) -> Option<i32>

source§

fn mul_div_round(self, num: i32, denom: i32) -> Option<i32>

source§

fn mul_div_ceil(self, num: i32, denom: i32) -> Option<i32>

source§

impl MulDiv for i64

source§

type Output = i64

source§

fn mul_div_floor(self, num: i64, denom: i64) -> Option<i64>

source§

fn mul_div_round(self, num: i64, denom: i64) -> Option<i64>

source§

fn mul_div_ceil(self, num: i64, denom: i64) -> Option<i64>

source§

impl MulDiv for u8

source§

type Output = u8

source§

fn mul_div_floor(self, num: u8, denom: u8) -> Option<u8>

source§

fn mul_div_round(self, num: u8, denom: u8) -> Option<u8>

source§

fn mul_div_ceil(self, num: u8, denom: u8) -> Option<u8>

source§

impl MulDiv for u16

source§

type Output = u16

source§

fn mul_div_floor(self, num: u16, denom: u16) -> Option<u16>

source§

fn mul_div_round(self, num: u16, denom: u16) -> Option<u16>

source§

fn mul_div_ceil(self, num: u16, denom: u16) -> Option<u16>

source§

impl MulDiv for u32

source§

type Output = u32

source§

fn mul_div_floor(self, num: u32, denom: u32) -> Option<u32>

source§

fn mul_div_round(self, num: u32, denom: u32) -> Option<u32>

source§

fn mul_div_ceil(self, num: u32, denom: u32) -> Option<u32>

source§

impl MulDiv for u64

source§

type Output = u64

source§

fn mul_div_floor(self, num: u64, denom: u64) -> Option<u64>

source§

fn mul_div_round(self, num: u64, denom: u64) -> Option<u64>

source§

fn mul_div_ceil(self, num: u64, denom: u64) -> Option<u64>

Implementors§

source§

impl MulDiv<i32> for Signed<u32>

source§

impl MulDiv<i32> for Signed<Percent>

source§

impl MulDiv<i64> for Signed<u64>

source§

impl MulDiv<i64> for Signed<ClockTime>

source§

impl MulDiv<i64> for Signed<Other>

source§

impl MulDiv<i64> for Signed<Buffers>

source§

impl MulDiv<i64> for Signed<Bytes>

source§

impl MulDiv<i64> for Signed<Default>

source§

impl MulDiv<u32> for Signed<u32>

source§

impl MulDiv<u32> for Signed<Percent>

source§

impl MulDiv<u32> for Percent

source§

impl MulDiv<u64> for Signed<u64>

source§

impl MulDiv<u64> for Signed<ClockTime>

source§

impl MulDiv<u64> for Signed<Other>

source§

impl MulDiv<u64> for Signed<Buffers>

source§

impl MulDiv<u64> for Signed<Bytes>

source§

impl MulDiv<u64> for Signed<Default>

source§

impl MulDiv<u64> for ClockTime

source§

impl MulDiv<u64> for Other

source§

impl MulDiv<u64> for Buffers

source§

impl MulDiv<u64> for Bytes

source§

impl MulDiv<u64> for Default