Trait fixed::traits::FixedEquiv

source ·
pub trait FixedEquiv {
    type Equiv: Fixed;

    // Required methods
    fn to_fixed_equiv(self) -> Self::Equiv;
    fn as_fixed_equiv(&self) -> &Self::Equiv;
    fn as_fixed_equiv_mut(&mut self) -> &mut Self::Equiv;
    fn from_fixed_equiv(f: Self::Equiv) -> Self;
    fn ref_from_fixed_equiv(f: &Self::Equiv) -> &Self;
    fn mut_from_fixed_equiv(f: &mut Self::Equiv) -> &mut Self;
}
Expand description

This trait provides a way to convert a number to/from an equivalent fixed-point number.

Implementations are provided for the signed integer primitives i8, i16, i32, i64 and i128, which have equivalent fixed-point types I8F0, I16F0, I32F0, I64F0 and I128F0. Similar implementations are provided for the unsigned integer primitives u8, u16, u32, u64 and u128.

§Examples

An i32 can be treated as an I32F0.

use fixed::traits::{Fixed, FixedEquiv};

fn next_up<F: Fixed>(f: &mut F) {
    *f += F::DELTA;
}

let mut i = 12i32;
// next_up is called with &mut i converted to &mut I32F0
next_up(i.as_fixed_equiv_mut());
assert_eq!(i, 13);

Simlarly, an I32F0 can be treated as an i32.

use fixed::{traits::FixedEquiv, types::I32F0};

fn increase_by_5(i: &mut i32) {
    *i += 5;
}

let mut f = I32F0::from_num(12);
// increase_by_5 is called with &mut f converted to &mut i32
increase_by_5(i32::mut_from_fixed_equiv(&mut f));
assert_eq!(f, 17);

Required Associated Types§

source

type Equiv: Fixed

The equivalent fixed-point type.

Required Methods§

source

fn to_fixed_equiv(self) -> Self::Equiv

Converts an owned value to the equivalent fixed-point type.

source

fn as_fixed_equiv(&self) -> &Self::Equiv

Converts a reference into a reference to the equivalent fixed-point type.

source

fn as_fixed_equiv_mut(&mut self) -> &mut Self::Equiv

Converts a mutable reference into a mutable reference to the equivalent fixed-point type.

source

fn from_fixed_equiv(f: Self::Equiv) -> Self

Converts an owned equivalent fixed-point type to this type.

source

fn ref_from_fixed_equiv(f: &Self::Equiv) -> &Self

Converts a reference to the equivalent fixed-point type into a reference to this type.

source

fn mut_from_fixed_equiv(f: &mut Self::Equiv) -> &mut Self

Converts a mutable reference to the equivalent fixed-point type into a mutable reference to this type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FixedEquiv for i8

source§

impl FixedEquiv for i16

source§

impl FixedEquiv for i32

source§

impl FixedEquiv for i64

source§

impl FixedEquiv for i128

source§

impl FixedEquiv for u8

source§

impl FixedEquiv for u16

source§

impl FixedEquiv for u32

source§

impl FixedEquiv for u64

source§

impl FixedEquiv for u128

Implementors§