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> InnerValue for Opaque<T>where
T: ReprValue,
impl<T> InnerValue for Opaque<T>where
T: ReprValue,
impl<T: Copy> Copy for Opaque<T>
impl<T> Locate for Opaque<T>where
T: ReprValue,
impl<T> Mark for Opaque<T>where
T: ReprValue,
impl<T: ReprValue> Send for Opaque<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more