Trait substrate_fixed::traits::FromFixed
source · pub trait FromFixed {
// Required methods
fn from_fixed<F: Fixed>(src: F) -> Self;
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
where Self: Sized;
fn saturating_from_fixed<F: Fixed>(src: F) -> Self;
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self;
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
where Self: Sized;
}
Expand description
This trait provides checked conversions from fixed-point numbers.
This trait is implemented for conversions between integer primitives, floating-point primitives and fixed-point numbers.
Examples
use substrate_fixed::traits::FromFixed;
use substrate_fixed::types::U8F8;
// 0x87.65
let f = U8F8::from_bits(0x8765);
assert_eq!(f32::from_fixed(f), f32::from(0x8765u16) / 256.0);
assert_eq!(i32::checked_from_fixed(f), Some(0x87));
assert_eq!(u8::saturating_from_fixed(f), 0x87);
// no fit
assert_eq!(i8::checked_from_fixed(f), None);
assert_eq!(i8::saturating_from_fixed(f), i8::max_value());
assert_eq!(i8::wrapping_from_fixed(f), 0x87u8 as i8);
assert_eq!(i8::overflowing_from_fixed(f), (0x87u8 as i8, true));
Required Methods§
sourcefn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts from a fixed-point number.
Any extra 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.
sourcefn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>where
Self: Sized,
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>where Self: Sized,
Converts from a fixed-point number if it fits, otherwise returns None
.
Any extra fractional bits are truncated.
sourcefn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts from a fixed-point number, saturating if it does not fit.
Any extra fractional bits are truncated.
sourcefn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts from a fixed-point number, wrapping if it does not fit.
Any extra fractional bits are truncated.
Object Safety§
Implementations on Foreign Types§
source§impl FromFixed for f32
impl FromFixed for f32
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number.
Rounding is to the nearest, with ties rounded to even.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to a floating-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, saturating if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, wrapping if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
source§impl FromFixed for f64
impl FromFixed for f64
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number.
Rounding is to the nearest, with ties rounded to even.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to a floating-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, saturating if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, wrapping if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
source§impl FromFixed for i8
impl FromFixed for i8
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for i16
impl FromFixed for i16
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for i32
impl FromFixed for i32
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for i64
impl FromFixed for i64
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for i128
impl FromFixed for i128
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for isize
impl FromFixed for isize
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for u8
impl FromFixed for u8
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for u16
impl FromFixed for u16
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for u32
impl FromFixed for u32
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for u64
impl FromFixed for u64
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for u128
impl FromFixed for u128
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for usize
impl FromFixed for usize
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to an integer if it fits, otherwise returns None
.
Any fractional bits are truncated.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, saturating if it does not fit.
Any fractional bits are truncated.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to an integer, wrapping if it does not fit.
Any fractional bits are truncated.
source§impl FromFixed for bf16
impl FromFixed for bf16
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number.
Rounding is to the nearest, with ties rounded to even.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to a floating-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, saturating if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, wrapping if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
source§impl FromFixed for f16
impl FromFixed for f16
source§fn from_fixed<F: Fixed>(src: F) -> Self
fn from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number.
Rounding is to the nearest, with ties rounded to even.
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.
source§fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
fn checked_from_fixed<F: Fixed>(src: F) -> Option<Self>
Converts a fixed-point number to a floating-point
number if it fits, otherwise returns None
.
Rounding is to the nearest, with ties rounded to even.
source§fn saturating_from_fixed<F: Fixed>(src: F) -> Self
fn saturating_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, saturating if it does not fit.
Rounding is to the nearest, with ties rounded to even.
source§fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
fn wrapping_from_fixed<F: Fixed>(src: F) -> Self
Converts a fixed-point number to a floating-point number, wrapping if it does not fit.
Rounding is to the nearest, with ties rounded to even.