Cast

Trait Cast 

Source
pub trait Cast<T> {
    // Required method
    fn cast(self) -> T;
}
Expand description

Unified trait for conversions between fastnum numeric types.

Cast is a total conversion: it never fails and does not return an error. Prefer Cast when the conversion is guaranteed to be correct by definition (e.g., widening with preserved value or sign/zero extension that cannot overflow).

If the conversion may go out of range or lose information (sign/high bits), use TryCast instead.

§Examples:

use fastnum::*;

let a = u128!(123);

// Lossless widening
let b: U256 = a.cast();
assert_eq!(u256!(123), b);

See also: TryCast for fallible conversions.

Required Methods§

Source

fn cast(self) -> T

Performs an infallible, value-preserving conversion.

This method never fails. Use it for conversions that are known to be safe and cannot overflow or lose information (e.g., widening).

§Examples:
use fastnum::*;

let x = u128!(123);
let y: U256 = x.cast();
assert_eq!(u256!(123), y);

Implementors§

Source§

impl<const N: usize> Cast<Decimal<N>> for UnsignedDecimal<N>

Source§

impl<const N: usize, const M: usize> Cast<Int<N>> for Int<M>
where Dimension<N, M>: Widen,

Source§

impl<const N: usize, const M: usize> Cast<Int<N>> for UInt<M>
where Dimension<N, M>: Widen,

Source§

impl<const N: usize, const M: usize> Cast<UInt<N>> for UInt<M>
where Dimension<N, M>: Widen,

Source§

impl<const N: usize, const M: usize> Cast<Decimal<N>> for Decimal<N>
where Dimension<N, M>: Widen,

Source§

impl<const N: usize, const M: usize> Cast<Decimal<N>> for UnsignedDecimal<N>
where Dimension<N, M>: Widen,

Source§

impl<const N: usize, const M: usize> Cast<UnsignedDecimal<N>> for UnsignedDecimal<N>
where Dimension<N, M>: Widen,