tvm_ffi::reflection

Struct ObjectDef

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

Registers the reflection of the object type N, as C++ refl::ObjectDef<N> does: the metadata, each #[reflect] field, the methods and the type attributes.

#[derive(Object)] creates one while it registers a #[type_register] type and passes it to the function #[type_reflection(path)] names, which registers methods with ObjectDef::def and ObjectDef::def_static:

ⓘ
#[type_reflection(PointObj::reflect_methods)]
struct PointObj { /* ... */ }

impl PointObj {
    fn reflect_methods(def: &mut ObjectDef<Self>) {
        def.def("norm1", |p: Point| -> Result<i64> { Ok(p.x.abs() + p.y.abs()) }, "The L1 norm.")
            .def_static("origin", || -> Result<Point> { Ok(origin()) }, "");
    }
}

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. When N already has metadata in the type table, every call is a no-op.

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> + '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, as C++ ObjectDef::def does. 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> + 'static,

Registers func as the static method name of N, as C++ ObjectDef::def_static does; 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>

§

impl<N> !Sync for ObjectDef<N>

§

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

§

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.