Struct magnus::Binding

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

A Value known to be an instance of Binding.

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

Implementations§

source§

impl Binding

source

pub fn new() -> Self

👎Deprecated since 0.2.0: this will no longer function as of Ruby 3.2
Available on ruby_lte_3_1 only.

Create a new Binding from the current Ruby execution context.

Panics

Panics if called from a non-Ruby thread.

Examples
use magnus::Binding;

let binding = Binding::new();
source

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

Return Some(Binding) if val is a Binding, None otherwise.

source

pub fn eval<T, U>(&self, s: T) -> Result<U, Error>where T: IntoRString, U: TryConvert,

Evaluate a string of Ruby code within the binding’s context.

Examples
use magnus::{eval, Binding};

let binding = eval::<Binding>("binding").unwrap();
assert_eq!(binding.eval::<_, i64>("1 + 2").unwrap(), 3);
source

pub fn local_variable_get<N, T>(&self, name: N) -> Result<T, Error>where N: IntoSymbol, T: TryConvert,

Get the named local variable from the binding.

Returns Ok(T) if the method returns without error and the return value converts to a T, or returns Err if the local variable does not exist or the conversion fails.

Examples
use magnus::{eval, Binding, Value};

let binding = eval::<Binding>("binding").unwrap();
binding.local_variable_set("a", 1);
assert_eq!(binding.local_variable_get::<_, i64>("a").unwrap(), 1);
assert!(binding.local_variable_get::<_, Value>("b").is_err());
source

pub fn local_variable_set<N, T>(&self, name: N, val: T)where N: IntoSymbol, T: IntoValue,

Set the named local variable in the binding.

Examples
use magnus::{eval, Binding};

let binding = eval::<Binding>("binding").unwrap();
binding.local_variable_set("a", 1);
assert_eq!(binding.local_variable_get::<_, i64>("a").unwrap(), 1);

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 Binding

source§

fn clone(&self) -> Binding

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 Binding

source§

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

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

impl Deref for Binding

§

type Target = Value

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl Display for Binding

source§

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

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

impl From<Binding> for Value

source§

fn from(val: Binding) -> Self

Converts to this type from the input type.
source§

impl IntoValue for Binding

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 Object for Binding

source§

fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where M: Method,

Define a singleton method in self’s scope. Read more
source§

fn ivar_get<T, U>(self, name: T) -> Result<U, Error>where T: IntoId, U: TryConvert,

Get the value for the instance variable name within self’s scope. Read more
source§

fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where T: IntoId, U: IntoValue,

Set the value for the instance variable name within self’s scope. Read more
source§

fn singleton_class(self) -> Result<RClass, Error>

Finds or creates the singleton class of self. Read more
source§

fn extend_object(self, module: RModule) -> Result<(), Error>

Extend self with module. Read more
source§

impl TryConvert for Binding

source§

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

Convert val into Self.
source§

impl Copy for Binding

source§

impl ReprValue for Binding

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,