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>
impl<N: ObjectCore> ObjectDef<N>
Sourcepub fn def<F, I, O>(&mut self, name: &str, func: F, doc: &str) -> &mut Self
pub fn def<F, I, O>(&mut self, name: &str, func: F, doc: &str) -> &mut Self
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.
Sourcepub fn def_static<F, I, O>(
&mut self,
name: &str,
func: F,
doc: &str,
) -> &mut Self
pub fn def_static<F, I, O>( &mut self, name: &str, func: F, doc: &str, ) -> &mut Self
Registers func as the static method name of N, which takes no
object; otherwise as ObjectDef::def.