Trait magnus::numeric::Numeric

source ·
pub trait Numeric: ReprValue + Copy {
    // Provided methods
    fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
       where T: Numeric,
             ID: IntoId,
             U: TryConvert { ... }
    fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
       where T: Numeric,
             ID: IntoId,
             U: TryConvert { ... }
    fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
       where T: Numeric,
             ID: IntoId,
             U: TryConvert { ... }
    fn coerce_bit<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
       where T: Numeric,
             ID: IntoId,
             U: TryConvert { ... }
}
Expand description

Functions available for all of Ruby’s Numeric types.

Provided Methods§

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.

As Ruby’s operators are implemented as methods, this function can be thought of as a specialised version of Value::funcall, just for subclasses of Numeric, and that follows Ruby’s coercion protocol.

Returns Ok(U) if the method returns without error and the return value converts to a U, or returns Err if the method raises or the conversion fails.

The returned errors are tailored for binary operators such as +, /, etc.

§Examples
use magnus::{Integer, Numeric};

let a = Integer::from_i64(2);
let b = Integer::from_i64(3);
let c: i64 = a.coerce_bin(b, "+").unwrap();
assert_eq!(c, 5);

Avoiding type conversion of the result to demonstrate Ruby is coercing the types:

use magnus::{Float, Integer, Numeric, Value};

let a = Integer::from_i64(2);
let b = Float::from_f64(3.5);
let c: Value = a.coerce_bin(b, "+").unwrap();
let c = Float::from_value(c);
assert!(c.is_some());
assert_eq!(c.unwrap().to_f64(), 5.5);
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.

As Ruby’s operators are implemented as methods, this function can be thought of as a specialised version of Value::funcall, just for subclasses of Numeric, and that follows Ruby’s coercion protocol.

Returns Ok(U) if the method returns without error and the return value converts to a U, or returns Err if the method raises or the conversion fails.

The returned errors are tailored for comparison operators such as <=>.

Note, if coercion fails this will return nil, if you want to detect this you should set the result type to Option<U>. Other errors in applying op will still result in an Err.

§Examples
use std::num::NonZeroI64;

use magnus::{Float, Numeric, RRational};

let a = RRational::new(1, NonZeroI64::new(4).unwrap());
let b = Float::from_f64(0.3);
let result: i64 = a.coerce_cmp(b, "<=>").unwrap();
assert_eq!(result, -1);
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.

As Ruby’s operators are implemented as methods, this function can be thought of as a specialised version of Value::funcall, just for subclasses of Numeric, and that follows Ruby’s coercion protocol.

Returns Ok(U) if the method returns without error and the return value converts to a U, or returns Err if the method raises or the conversion fails.

The returned errors are tailored for relationship operators such as <=.

§Examples
use std::num::NonZeroI64;

use magnus::{Float, Numeric, RRational};

let a = Float::from_f64(0.3);
let b = RRational::new(1, NonZeroI64::new(4).unwrap());
let result: bool = a.coerce_cmp(b, "<=").unwrap();
assert_eq!(result, false);
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.

As Ruby’s operators are implemented as methods, this function can be thought of as a specialised version of Value::funcall, just for subclasses of Numeric, and that follows Ruby’s coercion protocol.

Returns Ok(U) if the method returns without error and the return value converts to a U, or returns Err if the method raises or the conversion fails.

The returned errors are tailored for bitwise operators such as |, ^, etc.

§Examples
use magnus::{Integer, Numeric};

let a = Integer::from_i64(0b00000011);
let b = Integer::from_i64(0b00001110);
let result: i64 = a.coerce_cmp(b, "^").unwrap();
assert_eq!(result, 0b00001101);

Object Safety§

This trait is not object safe.

Implementors§