#[repr(transparent)]
pub struct Fixnum(_);
Expand description

A Value known to be a fixnum, Ruby’s internal representation of small integers.

See also Integer.

All Value methods should be available on this type through Deref, but some may be missed by this documentation.

Implementations

Return Some(Fixnum) if val is a Fixnum, None otherwise.

Examples
use magnus::{eval, Fixnum};

assert!(Fixnum::from_value(eval("0").unwrap()).is_some());
// too big
assert!(Fixnum::from_value(eval("9223372036854775807").unwrap()).is_none());
// not an int
assert!(Fixnum::from_value(eval("1.23").unwrap()).is_none());

Create a new Fixnum from an i64.

Returns Ok(Fixnum) if n is in range for Fixnum, otherwise returns Err(RBignum).

Examples
use magnus::{eval, Fixnum};

assert!(Fixnum::from_i64(0).is_ok());
// too big
assert!(Fixnum::from_i64(4611686018427387904).is_err());
assert!(Fixnum::from_i64(-4611686018427387905).is_err());

Create a new Fixnum from a u64.

Returns Ok(Fixnum) if n is in range for Fixnum, otherwise returns Err(RBignum).

Examples
use magnus::{eval, Fixnum};

assert!(Fixnum::from_u64(0).is_ok());
// too big
assert!(Fixnum::from_u64(4611686018427387904).is_err());

Convert self to an i8. Returns Err if self is out of range for i8.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("127").unwrap().to_i8().unwrap(), 127);
assert!(eval::<Fixnum>("128").unwrap().to_i8().is_err());
assert_eq!(eval::<Fixnum>("-128").unwrap().to_i8().unwrap(), -128);
assert!(eval::<Fixnum>("-129").unwrap().to_i8().is_err());

Convert self to an i16. Returns Err if self is out of range for i16.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("32767").unwrap().to_i16().unwrap(), 32767);
assert!(eval::<Fixnum>("32768").unwrap().to_i16().is_err());
assert_eq!(eval::<Fixnum>("-32768").unwrap().to_i16().unwrap(), -32768);
assert!(eval::<Fixnum>("-32769").unwrap().to_i16().is_err());

Convert self to an i32. Returns Err if self is out of range for i32.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("2147483647").unwrap().to_i32().unwrap(), 2147483647);
assert!(eval::<Fixnum>("2147483648").unwrap().to_i32().is_err());
assert_eq!(eval::<Fixnum>("-2147483648").unwrap().to_i32().unwrap(), -2147483648);
assert!(eval::<Fixnum>("-2147483649").unwrap().to_i32().is_err());

Convert self to an i64. This is infallible as i64 can represent a larger range than Fixnum.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("4611686018427387903").unwrap().to_i64(), 4611686018427387903);
assert_eq!(eval::<Fixnum>("-4611686018427387904").unwrap().to_i64(), -4611686018427387904);

Convert self to an isize. Returns Err if self is out of range for isize.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("4611686018427387903").unwrap().to_isize(), 4611686018427387903);
assert_eq!(eval::<Fixnum>("-4611686018427387904").unwrap().to_isize(), -4611686018427387904);

Convert self to a u8. Returns Err if self is negative or out of range for u8.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("255").unwrap().to_u8().unwrap(), 255);
assert!(eval::<Fixnum>("256").unwrap().to_u8().is_err());
assert!(eval::<Fixnum>("-1").unwrap().to_u8().is_err());

Convert self to a u16. Returns Err if self is negative or out of range for u16.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("65535").unwrap().to_u16().unwrap(), 65535);
assert!(eval::<Fixnum>("65536").unwrap().to_u16().is_err());
assert!(eval::<Fixnum>("-1").unwrap().to_u16().is_err());

Convert self to a u32. Returns Err if self is negative or out of range for u32.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("4294967295").unwrap().to_u32().unwrap(), 4294967295);
assert!(eval::<Fixnum>("4294967296").unwrap().to_u32().is_err());
assert!(eval::<Fixnum>("-1").unwrap().to_u32().is_err());

Convert self to a u64. Returns Err if self is negative.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("4611686018427387903").unwrap().to_u64().unwrap(), 4611686018427387903);
assert!(eval::<Fixnum>("-1").unwrap().to_u64().is_err());

Convert self to a usize. Returns Err if self is negative or out of range for usize.

Examples
use magnus::{eval, Fixnum};

assert_eq!(eval::<Fixnum>("4611686018427387903").unwrap().to_usize().unwrap(), 4611686018427387903);
assert!(eval::<Fixnum>("-1").unwrap().to_usize().is_err());

Methods from Deref<Target = Value>

Convert self to a Rust string.

Safety

This may return a direct view of memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

This can be used safely by immediately calling into_owned on the return value.

Examples
use magnus::{eval, QTRUE};

let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");

Return the name of self’s class.

Safety

Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

This can be used safely by immediately calling into_owned on the return value.

Examples
use magnus::{eval, RHash};

let value = RHash::new();
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");

Convert self to the Rust type T.

See the types that TryConvert is implemented on for what this method can convert to.

Examples
use magnus::{eval, Value};

assert_eq!(eval::<Value>("42").unwrap().try_convert::<i64>().unwrap(), 42);
assert_eq!(eval::<Value>("1.23").unwrap().try_convert::<i64>().unwrap(), 1);
assert_eq!(eval::<Value>("1").unwrap().try_convert::<f64>().unwrap(), 1.0);
assert_eq!(eval::<Value>("nil").unwrap().try_convert::<Option<i64>>().unwrap(), None);
assert_eq!(eval::<Value>("42").unwrap().try_convert::<Option<i64>>().unwrap(), Some(42));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Convert val into Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.