Struct runestick::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 a new any from the original 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 runestick::{Any, 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 runestick::{Any, 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 runestick::{Any, 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 runestick::{Any, 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 runestick::{Any, 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 runestick::{Any, 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 runestick::Any;

#[derive(Debug, Any)]
struct Foo;

#[derive(Debug, Any)]
struct Other;

let any = runestick::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 runestick::Any;

#[derive(Debug, PartialEq, Eq, Any)]
struct Thing(u32);

#[derive(Debug, PartialEq, Eq, Any)]
struct Other;

let any = runestick::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 runestick::Any;

#[derive(Debug, PartialEq, Eq, Any)]
struct Thing(u32);

let mut any = runestick::AnyObj::new(Thing(1u32));
any.downcast_borrow_mut::<Thing>().unwrap().0 = 2;
assert_eq!(Some(&Thing(2u32)), any.downcast_borrow_ref::<Thing>());

Debug format the current any type.

Access the underlying type name for the data.

Access the underlying type id for the data.

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Convert into a value.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.