Struct rune::runtime::AnyObj [−][src]
#[repr(C)]pub struct AnyObj { /* fields omitted */ }
Expand description
Our own private dynamic Any implementation.
In contrast to Box<dyn std::any::Any>
, this allows for storing a raw
pointer directly in the object to avoid one level of indirection. Otherwise
it’s equivalent.
Implementations
Construct an Any that wraps a pointer.
Safety
Caller must ensure that the returned AnyObj
doesn’t outlive the
reference it is wrapping.
This would be an example of incorrect use:
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
let any = unsafe { AnyObj::from_ref(&v) };
drop(v);
// any use of `any` beyond here is undefined behavior.
Examples
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
let any = unsafe { AnyObj::from_ref(&mut v) };
let b = any.downcast_borrow_ref::<Foo>().unwrap();
assert_eq!(b.0, 1u32);
Construct an Any that wraps a Deref type, behaving as the Target of the Deref implementation
Safety
Caller must ensure that the returned AnyObj
doesn’t outlive the
dereference target.
Examples
use rune::Any;
use rune::runtime::AnyObj;
use std::cell::RefCell;
#[derive(Any)]
struct Foo(u32);
let mut v = RefCell::new(Foo(1u32));
let mut guard = v.borrow();
let any = unsafe { AnyObj::from_deref(guard) };
let b = any.downcast_borrow_ref::<Foo>().unwrap();
assert_eq!(b.0, 1u32);
Construct an Any that wraps a mutable pointer.
Safety
Caller must ensure that the returned AnyObj
doesn’t outlive the
reference it is wrapping.
This would be an example of incorrect use:
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
let any = unsafe { AnyObj::from_mut(&mut v) };
drop(v);
// any use of `any` beyond here is undefined behavior.
Examples
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
{
let mut any = unsafe { AnyObj::from_mut(&mut v) };
if let Some(v) = any.downcast_borrow_mut::<Foo>() {
v.0 += 1;
}
}
assert_eq!(v.0, 2);
Construct an Any that wraps a DerefMut type, behaving as the Target of the DerefMut implementation
Safety
Caller must ensure that the returned AnyObj
doesn’t outlive the
dereference target.
Examples
use rune::Any;
use rune::runtime::AnyObj;
use std::cell::RefCell;
#[derive(Any)]
struct Foo(u32);
let mut v = RefCell::new(Foo(1u32));
let mut guard = v.borrow_mut();
let any = unsafe { AnyObj::from_deref_mut(guard) };
let b = any.downcast_borrow_ref::<Foo>().unwrap();
assert_eq!(b.0, 1u32);
Construct a new any with the specified raw components.
Safety
The caller must ensure that the vtable matches up with the data pointer provided. This is primarily public for use in a C ffi.
Returns true
if the boxed type is the same as T
.
Examples
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Debug, Any)]
struct Foo;
#[derive(Debug, Any)]
struct Other;
let any = AnyObj::new(Foo);
assert!(any.is::<Foo>());
assert!(!any.is::<Other>());
Returns some reference to the boxed value if it is of type T
, or
None
if it isn’t.
Examples
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Debug, PartialEq, Eq, Any)]
struct Thing(u32);
#[derive(Debug, PartialEq, Eq, Any)]
struct Other;
let any = AnyObj::new(Thing(1u32));
assert_eq!(Some(&Thing(1u32)), any.downcast_borrow_ref::<Thing>());
assert_eq!(None, any.downcast_borrow_ref::<Other>());
Returns some mutable reference to the boxed value if it is of type T
, or
None
if it isn’t.
Examples
use rune::Any;
use rune::runtime::AnyObj;
#[derive(Debug, PartialEq, Eq, Any)]
struct Thing(u32);
let mut any = AnyObj::new(Thing(1u32));
any.downcast_borrow_mut::<Thing>().unwrap().0 = 2;
assert_eq!(Some(&Thing(2u32)), any.downcast_borrow_ref::<Thing>());