pub trait FixedBits: Sealed where
Self: Default + Hash + Ord,
Self: Contiguous + Pod,
Self: Debug + Display + Binary + Octal + LowerHex + UpperHex,
Self: FromStr<Err = ParseIntError>,
Self: Add<Output = Self> + AddAssign,
Self: Sub<Output = Self> + SubAssign,
Self: Mul<Output = Self> + MulAssign,
Self: Div<Output = Self> + DivAssign,
Self: Rem<Output = Self> + RemAssign,
Self: Not<Output = Self>,
Self: BitAnd<Output = Self> + BitAndAssign,
Self: BitOr<Output = Self> + BitOrAssign,
Self: BitXor<Output = Self> + BitXorAssign,
Self: Shl<u32, Output = Self> + ShlAssign<u32>,
Self: Shr<u32, Output = Self> + ShrAssign<u32>,
Self: Sum + Product,
Self: FixedEquiv,
Self: FixedBitsCast<i8> + FixedBitsCast<i16> + FixedBitsCast<i32>,
Self: FixedBitsCast<i64> + FixedBitsCast<i128> + FixedBitsCast<isize>,
Self: FixedBitsCast<u8> + FixedBitsCast<u16> + FixedBitsCast<u32>,
Self: FixedBitsCast<u64> + FixedBitsCast<u128> + FixedBitsCast<usize>,
Self: FixedBitsOptionalArbitrary,
Self: FixedBitsOptionalBorsh,
Self: FixedBitsOptionalNum,
Self: FixedBitsOptionalSerde, {
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);