Function fraction::generic::read_generic_integer

source ·
pub fn read_generic_integer<T, F>(val: F) -> Option<(Sign, T)>
Expand description

Builds integer of type T from another integer of type F in a generic way. Guarantees to only return positive values.

Allows safe runtime conversions between integer types when it’s not possible statically. E.g: i8 -> u8, u8 -> i8, usize -> u8 or even BigUint -> u8 and so on.

Simply reinterprets type F as T when they are the same type.

§Examples

use fraction::{Sign, generic::read_generic_integer};

assert_eq!((Sign::Plus, 127i8), read_generic_integer(127u8).unwrap());
assert_eq!((Sign::Minus, 128u8), read_generic_integer(-128i8).unwrap());
assert_eq!((Sign::Minus, 255u8), read_generic_integer(-255isize).unwrap());