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>
impl<N: ObjectCore> ObjectDef<N>
Sourcepub fn def<F, I, O>(&mut self, name: &str, func: F, doc: &str) -> &mut Selfwhere
F: AsPackedCallable<I, O> + 'static,
pub fn def<F, I, O>(&mut self, name: &str, func: F, doc: &str) -> &mut Selfwhere
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.
Sourcepub fn def_static<F, I, O>(
&mut self,
name: &str,
func: F,
doc: &str,
) -> &mut Selfwhere
F: AsPackedCallable<I, O> + 'static,
pub fn def_static<F, I, O>(
&mut self,
name: &str,
func: F,
doc: &str,
) -> &mut Selfwhere
F: AsPackedCallable<I, O> + 'static,
Registers func as the static method name of N, as C++
ObjectDef::def_static does; otherwise as ObjectDef::def.