#[repr(transparent)]pub struct RBignum(_);
Expand description
Implementations§
source§impl RBignum
impl RBignum
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
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());
sourcepub fn from_i64(n: i64) -> Result<Self, Fixnum>
pub fn from_i64(n: i64) -> Result<Self, Fixnum>
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());
sourcepub fn from_u64(n: u64) -> Result<Self, Fixnum>
pub fn from_u64(n: u64) -> Result<Self, Fixnum>
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());
sourcepub fn to_i64(self) -> Result<i64, Error>
pub fn to_i64(self) -> Result<i64, Error>
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());
sourcepub fn to_isize(self) -> Result<isize, Error>
pub fn to_isize(self) -> Result<isize, Error>
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);
sourcepub fn to_u64(self) -> Result<u64, Error>
pub fn to_u64(self) -> Result<u64, Error>
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());
sourcepub fn to_usize(self) -> Result<usize, Error>
pub fn to_usize(self) -> Result<usize, Error>
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>§
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
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");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
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");