usesuper::{MethodType, DataType, MTFn, MTFnMut, MTSync, MethodResult, MethodInfo}; usesuper::{Tree, ObjectPath, Interface, Property, Signal, Method}; usesuper::objectpath::IfaceCache; use std::sync::Arc; use Interface as IfaceName; use {Member, Path, arg}; use std::cell::RefCell;
/// The factory is used to create object paths, interfaces, methods etc. /// /// There are three factories: /// /// **MTFn** - all methods are `Fn()`. /// /// **MTFnMut** - all methods are `FnMut()`. This means they can mutate their environment, /// which has the side effect that if you call it recursively, it will RefCell panic. /// /// **MTSync** - all methods are `Fn() + Send + Sync + 'static`. This means that the methods /// can be called from different threads in parallel. /// #[derive(Debug, Clone)] pubstruct Factory<M: MethodType<D>, D: DataType=()>(Arc<IfaceCache<M, D>>);
impl Factory<MTFn<()>, ()> { /// Creates a new factory for single-thread use. pubfn new_fn<D: DataType>() -> Factory<MTFn<D>, D> { Factory(IfaceCache::new()) }
/// Creates a new factory for single-thread use, where callbacks can mutate their environment. pubfn new_fnmut<D: DataType>() -> Factory<MTFnMut<D>, D> { Factory(IfaceCache::new()) }
/// Creates a new factory for multi-thread use. pubfn new_sync<D: DataType>() -> Factory<MTSync<D>, D> { Factory(IfaceCache::new()) }
/// Creates a new property. /// /// `A` is used to calculate the type signature of the property. pubfn property<A: arg::Arg, T: Into<String>>(&self, name: T, data: D::Property) -> Property<M, D> { let sig = A::signature(); super::leaves::new_property(name.into(), sig, data)
}
/// Creates a new signal. pubfn signal<T: Into<Member<'static>>>(&self, name: T, data: D::Signal) -> Signal<D> { super::leaves::new_signal(name.into(), data)
}
/// Creates a new interface. pubfn interface<T: Into<IfaceName<'static>>>(&self, name: T, data: D::Interface) -> Interface<M, D> { super::objectpath::new_interface(name.into(), data)
}
/// Creates a new tree. pubfn tree(&self, data: D::Tree) -> Tree<M, D> { super::objectpath::new_tree(data)
}
/// Creates a new method - usually you'll use "method" instead. /// /// This is useful for being able to create methods in code which is generic over methodtype. pubfn method_sync<H, T>(&self, t: T, data: D::Method, handler: H) -> Method<M, D> where H: Fn(&MethodInfo<M, D>) -> MethodResult + Send + Sync + 'static, T: Into<Member<'static>> { super::leaves::new_method(t.into(), data, M::make_method(handler))
}
}
#[test] fn create_fnmut() { let f = Factory::new_fnmut::<()>(); letmut move_me = 5u32; let m = f.method("test", (), move |m| {
move_me += 1;
Ok(vec!(m.msg.method_return().append1(&move_me)))
});
assert_eq!(&**m.get_name(), "test");
}
#[test] fn fn_customdata() { #[derive(Default)] struct Custom; impl DataType for Custom { type Tree = (); type ObjectPath = Arc<u8>; type Interface = (); type Property = (); type Method = i32; type Signal = ();
}
let f = Factory::new_fn::<Custom>();
let m = f.method("test", 789, |_| unimplemented!());
assert_eq!(*m.get_data(), 789);
let o = f.object_path("/test/test", Arc::new(7));
assert_eq!(**o.get_data(), 7);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.