Crate scolapasta_fixable

Source
Expand description

Functions for converting numeric immediates to integer or “fixnum” immediates.

Fixnums have range of a 63-bit signed int and are returned as a native representation i64.

§Usage

Check whether a numeric value is able to be converted to an in-range “fixnum”:

use scolapasta_fixable::RB_FIXABLE;

assert!(RB_FIXABLE(23_u8));
assert!(RB_FIXABLE(u16::MIN));
assert!(RB_FIXABLE(i32::MAX));
assert!(RB_FIXABLE(1024_u64));
assert!(RB_FIXABLE(1024_i64));
assert!(RB_FIXABLE(1.0_f32));
assert!(RB_FIXABLE(-9000.27_f64));

This crate also exports a Fixable trait which provides methods on numeric types to check if they are fixable and to do a fallible conversion to an i64 fixnum.

use scolapasta_fixable::Fixable;

assert!(23_u8.is_fixable());
assert_eq!(23_u8.to_fix(), Some(23_i64));
assert!((-9000.27_f64).is_fixable());
assert_eq!((-9000.27_f64).to_fix(), Some(-9000_i64));

Some numeric types, such as u64, i128, and f64 have values that exceed fixnum range. Conversions on values of these types which are outside the 63-bit int range will fail:

use scolapasta_fixable::Fixable;

assert_eq!(u64::MAX.to_fix(), None);
assert_eq!(i128::MIN.to_fix(), None);
assert_eq!(4_611_686_018_427_387_904.0_f64.to_fix(), None);
assert_eq!(f64::INFINITY.to_fix(), None);
assert_eq!(f64::NAN.to_fix(), None);

For non-integer fixable types, the fractional part is discarded when converting to fixnum, i.e. converting to fixnum rounds to zero.

§Panics

All routines in this crate are implemented with checked operations and will never panic.

Constants§

RUBY_FIXNUM_MAX
The maximum possible value that a fixnum can represent, 63 bits of an i64.
RUBY_FIXNUM_MIN
The minimum possible value that a fixnum can represent, 63 bits of an i64.

Traits§

Fixable
Marker trait for numeric values which can be converted to a “fixnum”, or Integer, representation.

Functions§

RB_FIXABLE
Check whether the given numeric is in the range of fixnum.