Skip to main content

ObjectDef

Struct ObjectDef 

Source
pub struct ObjectDef<N: ObjectCore> { /* private fields */ }
Expand description

Registers the reflection of the object type N: the metadata, each #[reflect] field, the methods and the type attributes.

rs_object creates one while it registers its type and passes it to the function its reflection = path option names, which registers methods with ObjectDef::def and ObjectDef::def_static.

That function runs under the registration lock, before N::type_index() returns, so it must not call type_index() of N or of another Rust type that is not registered yet, nor register attributes with TypeAttrDef or EnumDef, nor call TypeAttrColumn::ensure: each takes the same lock, which is not reentrant. When N already has metadata in the type table, every call is a no-op; registration fails if a previous one stopped partway.

§Examples

use tvm_ffi::derive::ObjectRef;
use tvm_ffi::reflection::{self, ObjectDef};
use tvm_ffi::{rs_object, Object, ObjectArc, Result};

#[rs_object(type_key = "my_ext.MyPair", final, reflection = MyPairObj::reflect_methods)]
pub struct MyPairObj {
    base_: Object,
    pub first: i64,
    pub second: i64,
}

#[derive(Clone, ObjectRef)]
pub struct MyPair {
    data: ObjectArc<MyPairObj>,
}

impl MyPairObj {
    fn reflect_methods(def: &mut ObjectDef<Self>) {
        def.def("sum", |p: MyPair| -> Result<i64> { Ok(p.first + p.second) }, "The sum.");
    }
}

let pair = MyPair::from_object(MyPairObj::new(Object::new(), 1, 2));
let sum = reflection::get_method("my_ext.MyPair", "sum")?;
assert_eq!(i64::try_from(sum.call_tuple((&pair,))?)?, 3);

Implementations§

Source§

impl<N: ObjectCore> ObjectDef<N>

Source

pub fn def<F, I, O>(&mut self, name: &str, func: F, doc: &str) -> &mut Self
where F: AsPackedCallable<I, O> + Send + Sync + 'static,

Registers func, a typed function as for Function::from_typed whose first parameter is the object, as the method name of N, which must be unique in N, with its type schema and doc. A failed registration is reported when the type’s registration finishes.

Source

pub fn def_static<F, I, O>( &mut self, name: &str, func: F, doc: &str, ) -> &mut Self
where F: AsPackedCallable<I, O> + Send + Sync + 'static,

Registers func as the static method name of N, which takes no object; otherwise as ObjectDef::def.

Auto Trait Implementations§

§

impl<N> Freeze for ObjectDef<N>

§

impl<N> RefUnwindSafe for ObjectDef<N>
where N: RefUnwindSafe,

§

impl<N> Send for ObjectDef<N>
where N: Send,

§

impl<N> Sync for ObjectDef<N>
where N: Sync,

§

impl<N> Unpin for ObjectDef<N>
where N: Unpin,

§

impl<N> UnsafeUnpin for ObjectDef<N>

§

impl<N> UnwindSafe for ObjectDef<N>
where N: UnwindSafe,

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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> TypeSubsumes<T> for T
where T: ?Sized,