#[repr(C)]pub struct AnyObj { /* private fields */ }
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§
Source§impl AnyObj
impl AnyObj
Sourcepub unsafe fn from_ref<T>(data: &T) -> Selfwhere
T: Any,
pub unsafe fn from_ref<T>(data: &T) -> Selfwhere
T: Any,
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);
Sourcepub unsafe fn from_deref<T>(data: T) -> Result<Self>
pub unsafe fn from_deref<T>(data: T) -> Result<Self>
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);
Sourcepub unsafe fn from_mut<T>(data: &mut T) -> Selfwhere
T: Any,
pub unsafe fn from_mut<T>(data: &mut T) -> Selfwhere
T: Any,
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);
Sourcepub unsafe fn from_deref_mut<T>(data: T) -> Result<Self>
pub unsafe fn from_deref_mut<T>(data: T) -> Result<Self>
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);
Sourcepub unsafe fn new_raw(vtable: &'static AnyObjVtable, data: *const ()) -> Self
pub unsafe fn new_raw(vtable: &'static AnyObjVtable, data: *const ()) -> Self
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.
Sourcepub fn is<T>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
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>());
Sourcepub fn downcast_borrow_ref<T>(&self) -> Option<&T>where
T: Any,
pub fn downcast_borrow_ref<T>(&self) -> Option<&T>where
T: Any,
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>());
Sourcepub fn downcast_borrow_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
pub fn downcast_borrow_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
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>());