Struct rune::runtime::AnyObj

source ·
#[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

source

pub fn new<T>(data: T) -> Result<Self>
where T: Any,

Construct a new any from the original any.

source

pub unsafe fn from_ref<T>(data: &T) -> Self
where 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);
source

pub unsafe fn from_deref<T>(data: T) -> Result<Self>
where T: Deref, T::Target: Any,

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);
source

pub unsafe fn from_mut<T>(data: &mut T) -> Self
where 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);
source

pub unsafe fn from_deref_mut<T>(data: T) -> Result<Self>
where T: DerefMut, T::Target: Any,

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);
source

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.

source

pub fn is<T>(&self) -> bool
where 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>());
source

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>());
source

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>());
source

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

Debug format the current any type.

source

pub fn type_name(&self) -> RawStr

Access the underlying type name for the data.

source

pub fn type_hash(&self) -> Hash

Access the underlying type id for the data.

source

pub fn type_info(&self) -> TypeInfo

Access full type info for type.

Trait Implementations§

source§

impl Debug for AnyObj

source§

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

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

impl Drop for AnyObj

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl MaybeTypeOf for AnyObj

source§

fn maybe_type_of() -> Option<FullTypeOf>

Type information for the given type.
source§

impl ToValue for AnyObj

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl TryFrom<AnyObj> for Value

§

type Error = Error

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

fn try_from(value: AnyObj) -> Result<Self, Error>

Performs the conversion.

Auto Trait Implementations§

§

impl RefUnwindSafe for AnyObj

§

impl !Send for AnyObj

§

impl !Sync for AnyObj

§

impl Unpin for AnyObj

§

impl UnwindSafe for AnyObj

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
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> UnsafeToValue for T
where T: ToValue,

§

type Guard = ()

The type used to guard the unsafe value conversion.
source§

unsafe fn unsafe_to_value( self ) -> VmResult<(Value, <T as UnsafeToValue>::Guard)>

Convert into a value. Read more
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more