[−][src]Trait fixed::sealed::Int
This trait is implemented for all the primitive integer types.
This trait is sealed and cannot be implemented for more types; it
is implemented for i8
, i16
, i32
, i64
, i128
,
isize
, u8
, u16
, u32
, u64
, u128
, and
usize
.
Provided methods
fn from_fixed<F>(val: F) -> Self where
F: Fixed,
F: Fixed,
Converts from a fixed-point number.
Any fractional bits are truncated.
Panics
When debug assertions are enabled, panics if the value does
not fit. When debug assertions are not enabled, the wrapped
value can be returned, but it is not considered a breaking
change if in the future it panics; if wrapping is required use
wrapping_from_fixed
instead.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix = Fix::from_bits(-3 << 8 | 5); assert_eq!(i32::from_fixed(fix), -3);
fn to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number.
Panics
When debug assertions are enabled, panics if the value does
not fit. When debug assertions are not enabled, the wrapped
value can be returned, but it is not considered a breaking
change if in the future it panics; if wrapping is required use
wrapping_to_fixed
instead.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix: Fix = 3.to_fixed(); assert_eq!(fix, Fix::from_bits(3 << 8));
fn checked_from_fixed<F>(val: F) -> Option<Self> where
F: Fixed,
F: Fixed,
Converts from a fixed-point number if it fits, otherwise returns None
.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix = Fix::from_bits(-3 << 8 | 5); assert_eq!(i32::checked_from_fixed(fix), Some(-3)); assert!(u32::checked_from_fixed(fix).is_none());
fn checked_to_fixed<F>(self) -> Option<F> where
F: Fixed,
F: Fixed,
Converts to a fixed-point number if it fits, otherwise returns None
.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; assert_eq!(3.checked_to_fixed::<Fix>(), Some(Fix::from_bits(3 << 8))); assert!(i32::max_value().checked_to_fixed::<Fix>().is_none());
fn saturating_from_fixed<F>(val: F) -> Self where
F: Fixed,
F: Fixed,
Converts from a fixed-point number, saturating if it does not fit.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix = Fix::from_bits(-3 << 8 | 5); assert_eq!(i32::saturating_from_fixed(fix), -3); assert_eq!(u32::saturating_from_fixed(fix), 0);
fn saturating_to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number, saturating if it does not fit.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedU16<fixed::frac::U8>; assert_eq!(3i64.saturating_to_fixed::<Fix>(), Fix::from_bits(3 << 8)); assert_eq!((-1i8).saturating_to_fixed::<Fix>(), Fix::min_value());
fn wrapping_from_fixed<F>(val: F) -> Self where
F: Fixed,
F: Fixed,
Converts from a fixed-point number, wrapping if it does not fit.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix = Fix::from_bits(-3 << 8 | 5); assert_eq!(i32::wrapping_from_fixed(fix), -3); assert_eq!(u32::wrapping_from_fixed(fix), 3u32.wrapping_neg());
fn wrapping_to_fixed<F>(self) -> F where
F: Fixed,
F: Fixed,
Converts to a fixed-point number, wrapping if it does not fit.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedU16<fixed::frac::U8>; assert_eq!(3i64.wrapping_to_fixed::<Fix>(), Fix::from_bits(3 << 8)); assert_eq!((-1i8).wrapping_to_fixed::<Fix>(), Fix::from_bits(0xff00));
fn overflowing_from_fixed<F>(val: F) -> (Self, bool) where
F: Fixed,
F: Fixed,
Converts from a fixed-point number.
Returns a tuple of the integer and a bool
indicating
whether an overflow has occurred. On overflow, the wrapped
value is returned.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedI16<fixed::frac::U8>; let fix = Fix::from_bits(-3 << 8 | 5); assert_eq!(i32::overflowing_from_fixed(fix), (-3, false)); assert_eq!(u32::overflowing_from_fixed(fix), (3u32.wrapping_neg(), true));
fn overflowing_to_fixed<F>(self) -> (F, bool) where
F: Fixed,
F: Fixed,
Converts to a fixed-point number.
Returns a tuple of the fixed-point number and a bool
indicating whether an overflow has occurred. On overflow, the
wrapped value is returned.
Examples
use fixed::sealed::Int; type Fix = fixed::FixedU16<fixed::frac::U8>; assert_eq!(3i64.overflowing_to_fixed::<Fix>(), (Fix::from_bits(3 << 8), false)); assert_eq!((-1i8).overflowing_to_fixed::<Fix>(), (Fix::from_bits(0xff00), true));