#[repr(transparent)]pub struct Proc(_);
Expand description
Implementations
sourceimpl Proc
impl Proc
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Proc)
if val
is a Proc
, None
otherwise.
sourcepub fn new<R>(block: fn(_: &[Value], _: Option<Proc>) -> R) -> Selfwhere
R: BlockReturn,
pub fn new<R>(block: fn(_: &[Value], _: Option<Proc>) -> R) -> Selfwhere
R: BlockReturn,
Create a new Proc
.
As block
is a function pointer, only functions and closures that do
not capture any variables are permitted. For more flexibility (at the
cost of allocating) see from_fn
.
Examples
use magnus::{block::Proc, eval};
let proc = Proc::new(|args, _block| {
let acc = args.get(0).unwrap().try_convert::<i64>()?;
let i = args.get(1).unwrap().try_convert::<i64>()?;
Ok(acc + i)
});
let res: bool = eval!("proc.call(1, 2) == 3", proc).unwrap();
assert!(res);
let res: bool = eval!("[1, 2, 3, 4, 5].inject(&proc) == 15", proc).unwrap();
assert!(res);
sourcepub fn from_fn<F, R>(block: F) -> Selfwhere
F: 'static + Send + FnMut(&[Value], Option<Proc>) -> R,
R: BlockReturn,
pub fn from_fn<F, R>(block: F) -> Selfwhere
F: 'static + Send + FnMut(&[Value], Option<Proc>) -> R,
R: BlockReturn,
Create a new Proc
.
See also Proc::new
, which is more efficient when block
is a
function or closure that does not capture any variables.
Examples
use magnus::{block::Proc, eval};
let proc = Proc::from_fn(|args, _block| {
let acc = args.get(0).unwrap().try_convert::<i64>()?;
let i = args.get(1).unwrap().try_convert::<i64>()?;
Ok(acc + i)
});
let res: bool = eval!("proc.call(1, 2) == 3", proc).unwrap();
assert!(res);
let res: bool = eval!("[1, 2, 3, 4, 5].inject(&proc) == 15", proc).unwrap();
assert!(res);
sourcepub fn call<A, T>(self, args: A) -> Result<T, Error>where
A: RArrayArgList,
T: TryConvert,
pub fn call<A, T>(self, args: A) -> Result<T, Error>where
A: RArrayArgList,
T: TryConvert,
Call the proc with args
.
Returns Ok(T)
if the proc runs without error and the return value
converts into a T
, or returns Err
if the proc raises or the
conversion fails.
sourcepub fn arity(self) -> i64
pub fn arity(self) -> i64
Returns the number of arguments self
takes.
If self
takes no arguments, returns 0
.
If self
takes only required arguments, returns the number of required
arguments.
If self
is a lambda and has optional arguments, or is not a lambda
and takes a splat argument, returns -n-1
, where n
is the number of
required arguments.
If self
is not a lambda, and takes a finite number of optional
arguments, returns the number of required arguments.
Keyword arguments are considered as a single additional argument, that
argument being required if any keyword argument is required.
Examples
use magnus::{block::Proc, eval};
let proc = eval::<Proc>("proc {nil}", ).unwrap();
assert_eq!(proc.arity(), 0);
let proc = eval::<Proc>("proc {|a| a + 1}", ).unwrap();
assert_eq!(proc.arity(), 1);
let proc = eval::<Proc>("proc {|a, b| a + b}", ).unwrap();
assert_eq!(proc.arity(), 2);
let proc = eval::<Proc>("proc {|*args| args.sum}", ).unwrap();
assert_eq!(proc.arity(), -1);
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");
Trait Implementations
sourceimpl Object for Proc
impl Object for Proc
sourcefn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read moresourcefn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: Into<Id>,
U: Into<Value>,
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: Into<Id>,
U: Into<Value>,
sourceimpl TryConvert for Proc
impl TryConvert for Proc
sourcefn try_convert(val: Value) -> Result<Self, Error>
fn try_convert(val: Value) -> Result<Self, Error>
val
into Self
.