Struct magnus::RBignum

source ·
#[repr(transparent)]
pub struct RBignum(_);
Expand description

A Value pointer to a RBignum struct, Ruby’s internal representation of large 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(RBignum) if val is a RBignum, None otherwise.

Examples
use magnus::{eval, RBignum};

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

Create a new RBignum from an i64.

Returns Ok(RBignum) if n is large enough to require a bignum, otherwise returns Err(Fixnum).

Examples
use magnus::{eval, RBignum};

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

Create a new RBignum from an u64.

Returns Ok(RBignum) if n is large enough to require a bignum, otherwise returns Err(Fixnum).

Examples
use magnus::{eval, RBignum};

assert!(RBignum::from_u64(4611686018427387904).is_ok());
// too small
assert!(RBignum::from_u64(0).is_err());

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

Examples
use magnus::{eval, RBignum};

assert_eq!(eval::<RBignum>("4611686018427387904").unwrap().to_i64().unwrap(), 4611686018427387904);
assert_eq!(eval::<RBignum>("-4611686018427387905").unwrap().to_i64().unwrap(), -4611686018427387905);
assert!(eval::<RBignum>("9223372036854775808").unwrap().to_i64().is_err());
assert!(eval::<RBignum>("-9223372036854775809").unwrap().to_i64().is_err());

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

Examples
use magnus::{eval, RBignum};

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

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

Examples
use magnus::{eval, RBignum};

assert_eq!(eval::<RBignum>("4611686018427387904").unwrap().to_u64().unwrap(), 4611686018427387904);
assert!(eval::<RBignum>("18446744073709551616").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, RBignum};

assert_eq!(eval::<RBignum>("4611686018427387904").unwrap().to_usize().unwrap(), 4611686018427387904);
assert!(eval::<RBignum>("18446744073709551616").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 never give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");

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
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.