Struct magnus::value::Opaque

source ·
pub struct Opaque<T>(/* private fields */);
Expand description

A wrapper to make a Ruby type Send + Sync.

Ruby types are not Send or Sync as they provide a way to call Ruby’s APIs, which it is not safe to do from a non-Ruby thread.

Ruby types are safe to send between Ruby threads, but Rust’s trait system currently can not model this detail.

To resolve this, the Opaque type makes a Ruby type Send + Sync by removing the ability to do anything with it, making it impossible to call Ruby’s API on non-Ruby threads.

An Opaque<T> can be unwrapped to T with Ruby::get_inner, as it is only possible to instantiate a Ruby on a Ruby thread.

§Examples

use magnus::{rb_assert, value::Opaque, Ruby};

let ruby = Ruby::get().unwrap();
let opaque_str = Opaque::from(ruby.str_new("example"));

// send to another Ruby thread

let ruby = Ruby::get().unwrap(); // errors on non-Ruby thread
let str = ruby.get_inner(opaque_str);
rb_assert!(ruby, r#"str == "example""#, str);

Trait Implementations§

source§

impl<T: Clone> Clone for Opaque<T>

source§

fn clone(&self) -> Opaque<T>

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<T> From<T> for Opaque<T>
where T: ReprValue,

source§

fn from(val: T) -> Self

Converts to this type from the input type.
source§

impl<T> InnerRef for Opaque<T>
where T: ReprValue,

§

type Value = T

The specific Ruby value type.
source§

fn get_inner_ref_with<'a>(&'a self, _: &Ruby) -> &'a Self::Value

Get a reference to the inner value from self. Read more
source§

impl<T> InnerValue for Opaque<T>
where T: ReprValue,

§

type Value = T

The specific Ruby value type.
source§

fn get_inner_with(self, _: &Ruby) -> Self::Value

Get the inner value from self. Read more
source§

impl<T> IntoValue for Opaque<T>
where T: IntoValue,

source§

fn into_value_with(self, handle: &Ruby) -> 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<T: Copy> Copy for Opaque<T>

source§

impl<T> Locate for Opaque<T>
where T: ReprValue,

source§

impl<T> Mark for Opaque<T>
where T: ReprValue,

source§

impl<T: ReprValue> Send for Opaque<T>

source§

impl<T: ReprValue> Sync for Opaque<T>

Auto Trait Implementations§

§

impl<T> Freeze for Opaque<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Opaque<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Opaque<T>
where T: Unpin,

§

impl<T> UnwindSafe for Opaque<T>
where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> BlockReturn for T
where T: BlockReturn,

source§

impl<T> ReturnValue for T
where T: ReturnValue,