Struct magnus::Integer

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

A type wrapping either a Fixnum or a RBignum.

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

Implementations§

source§

impl Integer

source

pub fn from_value(val: Value) -> Option<Self>

Return Some(Integer) if val is an Integer, None otherwise.

Examples
use magnus::{eval, Integer};

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

pub fn from_i64(n: i64) -> Self

Create a new Integer from an i64.

Panics

Panics if called from a non-Ruby thread.

Examples
use magnus::{eval, Integer};

let res: bool = eval!("i == 0", i = Integer::from_i64(0)).unwrap();
assert!(res);
let res: bool = eval!("i == 4611686018427387904", i = Integer::from_i64(4611686018427387904)).unwrap();
assert!(res);
let res: bool = eval!("i == -4611686018427387905", i = Integer::from_i64(-4611686018427387905)).unwrap();
assert!(res);
source

pub fn from_u64(n: u64) -> Self

Create a new Integer from a u64.

Panics

Panics if called from a non-Ruby thread.

Examples
use magnus::{eval, Integer};

let res: bool = eval!("i == 0", i = Integer::from_u64(0)).unwrap();
assert!(res);
let res: bool = eval!("i == 4611686018427387904", i = Integer::from_u64(4611686018427387904)).unwrap();
assert!(res);
source

pub fn to_i8(self) -> Result<i8, Error>

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

Examples
use magnus::{eval, Integer};

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

pub fn to_i16(self) -> Result<i16, Error>

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

Examples
use magnus::{eval, Integer};

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

pub fn to_i32(self) -> Result<i32, Error>

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

Examples
use magnus::{eval, Integer};

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

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, Integer};

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

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, Integer};

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

pub fn to_u8(self) -> Result<u8, Error>

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

Examples
use magnus::{eval, Integer};

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

pub fn to_u16(self) -> Result<u16, Error>

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

Examples
use magnus::{eval, Integer};

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

pub fn to_u32(self) -> Result<u32, Error>

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

Examples
use magnus::{eval, Integer};

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

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, Integer};

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

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, Integer};

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

Methods from Deref<Target = Value>§

source

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");
source

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

Trait Implementations§

source§

impl Clone for Integer

source§

fn clone(&self) -> Integer

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Integer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for Integer

§

type Target = Value

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Display for Integer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Integer> for Value

source§

fn from(val: Integer) -> Self

Converts to this type from the input type.
source§

impl IntoValue for Integer

source§

fn into_value_with(self, _: &RubyHandle) -> Value

Convert self into Value.
source§

fn into_value(self) -> Value

Convert self into Value. Read more
source§

unsafe fn into_value_unchecked(self) -> Value

Convert self into Value. Read more
source§

impl Numeric for Integer

source§

fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>where T: Numeric, ID: IntoId, U: TryConvert,

Apply the operator op with coercion. Read more
source§

fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>where T: Numeric, ID: IntoId, U: TryConvert,

Apply the operator op with coercion. Read more
source§

fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>where T: Numeric, ID: IntoId, U: TryConvert,

Apply the operator op with coercion. Read more
source§

fn coerce_bit<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>where T: Numeric, ID: IntoId, U: TryConvert,

Apply the operator op with coercion. Read more
source§

impl TryConvert for Integer

source§

fn try_convert(val: Value) -> Result<Self, Error>

Convert val into Self.
source§

impl Copy for Integer

source§

impl ReprValue for Integer

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> BlockReturn for Twhere T: BlockReturn,

source§

impl<T> ReturnValue for Twhere T: ReturnValue,