Trait fixed::traits::FixedBits

source ·
pub trait FixedBits: Sealed
where Self: Default + Hash + Ord + Contiguous + Pod + Debug + Display + LowerExp + UpperExp + Binary + Octal + LowerHex + UpperHex + FromStr<Err = ParseIntError> + Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign + Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign + Rem<Output = Self> + RemAssign + Not<Output = Self> + BitAnd<Output = Self> + BitAndAssign + BitOr<Output = Self> + BitOrAssign + BitXor<Output = Self> + BitXorAssign + Shl<u32, Output = Self> + ShlAssign<u32> + Shr<u32, Output = Self> + ShrAssign<u32> + Sum + Product + FixedEquiv<Equiv = Self::Fixed<0>> + FixedBitsCast<i8> + FixedBitsCast<i16> + FixedBitsCast<i32> + FixedBitsCast<i64> + FixedBitsCast<i128> + FixedBitsCast<isize> + FixedBitsCast<u8> + FixedBitsCast<u16> + FixedBitsCast<u32> + FixedBitsCast<u64> + FixedBitsCast<u128> + FixedBitsCast<usize> + FixedBitsOptionalArbitrary + FixedBitsOptionalBorsh + FixedBitsOptionalNum + FixedBitsOptionalSerde,
{ type Fixed<const FRAC: i32>: Fixed<Bits = Self>; const MIN: Self; const MAX: Self; const IS_SIGNED: bool; const BITS: u32; }
Expand description

This trait is implemented for Fixed::Bits for all fixed-point numbers.

This provides some facilities to manipulate bits in generic functions.

This trait is sealed and cannot be implemented for more types; it is implemented for i8, i16, i32, i64, i128, u8, u16, u32, u64, and u128.

§Examples

use az::OverflowingAs;
use fixed::{traits::Fixed, types::*};
fn limited_positive_bits<F: Fixed>(fixed: F) -> Option<u32> {
    let bits = fixed.to_bits();
    match bits.overflowing_as::<u32>() {
        (wrapped, false) => Some(wrapped),
        (_, true) => None,
    }
}
assert_eq!(limited_positive_bits(I16F16::from_bits(100)), Some(100));
assert_eq!(limited_positive_bits(I16F16::from_bits(-100)), None);

Required Associated Types§

source

type Fixed<const FRAC: i32>: Fixed<Bits = Self>

A fixed-point number with this type as the underlying bit representation.

§Examples
#![feature(generic_const_exprs)]

use fixed::{
    traits::{Fixed, FixedBits},
    types::I4F4,
};

fn op<F: Fixed>(f: F) -> F {
    let num = F::Bits::try_from(64).ok().unwrap();
    // a requires more than 4 integer bits
    let a = <F::Bits as FixedBits>::Fixed::<0>::TRY_ONE.unwrap() * num;
    // b requires more than 4 fractional bits
    let b = <F::Bits as FixedBits>::Fixed::<6>::TRY_ONE.unwrap() / num;

    // a * b = 1, so this effectively returns f + 1
    f.add_prod(a, b)
}

assert_eq!(op(I4F4::from_num(2.5)), 3.5);

Required Associated Constants§

source

const MIN: Self

The smallest value that can be represented by this integer type.

source

const MAX: Self

The largest value that can be represented by this integer type.

source

const IS_SIGNED: bool

true if this integer type is signed.

source

const BITS: u32

The size of this integer type in bits.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FixedBits for i8

§

type Fixed<const FRAC: i32> = FixedI8<FRAC>

source§

const MIN: i8 = -128i8

source§

const MAX: i8 = 127i8

source§

const IS_SIGNED: bool = true

source§

const BITS: u32 = 8u32

source§

impl FixedBits for i16

§

type Fixed<const FRAC: i32> = FixedI16<FRAC>

source§

const MIN: i16 = -32_768i16

source§

const MAX: i16 = 32_767i16

source§

const IS_SIGNED: bool = true

source§

const BITS: u32 = 16u32

source§

impl FixedBits for i32

§

type Fixed<const FRAC: i32> = FixedI32<FRAC>

source§

const MIN: i32 = -2_147_483_648i32

source§

const MAX: i32 = 2_147_483_647i32

source§

const IS_SIGNED: bool = true

source§

const BITS: u32 = 32u32

source§

impl FixedBits for i64

§

type Fixed<const FRAC: i32> = FixedI64<FRAC>

source§

const MIN: i64 = -9_223_372_036_854_775_808i64

source§

const MAX: i64 = 9_223_372_036_854_775_807i64

source§

const IS_SIGNED: bool = true

source§

const BITS: u32 = 64u32

source§

impl FixedBits for i128

§

type Fixed<const FRAC: i32> = FixedI128<FRAC>

source§

const MIN: i128 = -170_141_183_460_469_231_731_687_303_715_884_105_728i128

source§

const MAX: i128 = 170_141_183_460_469_231_731_687_303_715_884_105_727i128

source§

const IS_SIGNED: bool = true

source§

const BITS: u32 = 128u32

source§

impl FixedBits for u8

§

type Fixed<const FRAC: i32> = FixedU8<FRAC>

source§

const MIN: u8 = 0u8

source§

const MAX: u8 = 255u8

source§

const IS_SIGNED: bool = false

source§

const BITS: u32 = 8u32

source§

impl FixedBits for u16

§

type Fixed<const FRAC: i32> = FixedU16<FRAC>

source§

const MIN: u16 = 0u16

source§

const MAX: u16 = 65_535u16

source§

const IS_SIGNED: bool = false

source§

const BITS: u32 = 16u32

source§

impl FixedBits for u32

§

type Fixed<const FRAC: i32> = FixedU32<FRAC>

source§

const MIN: u32 = 0u32

source§

const MAX: u32 = 4_294_967_295u32

source§

const IS_SIGNED: bool = false

source§

const BITS: u32 = 32u32

source§

impl FixedBits for u64

§

type Fixed<const FRAC: i32> = FixedU64<FRAC>

source§

const MIN: u64 = 0u64

source§

const MAX: u64 = 18_446_744_073_709_551_615u64

source§

const IS_SIGNED: bool = false

source§

const BITS: u32 = 64u32

source§

impl FixedBits for u128

§

type Fixed<const FRAC: i32> = FixedU128<FRAC>

source§

const MIN: u128 = 0u128

source§

const MAX: u128 = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

source§

const IS_SIGNED: bool = false

source§

const BITS: u32 = 128u32

Implementors§