pub struct Method<M: MethodType<D>, D: DataType> { /* private fields */ }
Expand description
A D-Bus Method.
Implementations§
Source§impl<M: MethodType<D>, D: DataType> Method<M, D>
impl<M: MethodType<D>, D: DataType> Method<M, D>
Sourcepub fn in_arg<A: Into<Argument>>(self, a: A) -> Self
pub fn in_arg<A: Into<Argument>>(self, a: A) -> Self
Builder method that adds an “in” Argument to this Method.
Sourcepub fn inarg<A: Arg, S: Into<String>>(self, s: S) -> Self
pub fn inarg<A: Arg, S: Into<String>>(self, s: S) -> Self
Builder method that adds an “in” Argument to this Method.
Examples found in repository?
examples/server.rs (line 56)
16fn main() -> Result<(), Box<dyn Error>> {
17 // Let's start by starting up a connection to the session bus and request a name.
18 let c = LocalConnection::new_session()?;
19 c.request_name("com.example.dbustest", false, true, false)?;
20
21 // The choice of factory tells us what type of tree we want,
22 // and if we want any extra data inside. We pick the simplest variant.
23 let f = Factory::new_fn::<()>();
24
25 // We create the signal first, since we'll need it in both inside the method callback
26 // and when creating the tree.
27 let signal = Arc::new(f.signal("HelloHappened", ()).sarg::<&str,_>("sender"));
28 let signal2 = signal.clone();
29
30 // We create a tree with one object path inside and make that path introspectable.
31 let tree = f.tree(()).add(f.object_path("/hello", ()).introspectable().add(
32
33 // We add an interface to the object path...
34 f.interface("com.example.dbustest", ()).add_m(
35
36 // ...and a method inside the interface.
37 f.method("Hello", (), move |m| {
38
39 // This is the callback that will be called when another peer on the bus calls our method.
40 // the callback receives "MethodInfo" struct and can return either an error, or a list of
41 // messages to send back.
42
43 let name: &str = m.msg.read1()?;
44 let s = format!("Hello {}!", name);
45 let mret = m.msg.method_return().append1(s);
46
47 let sig = signal.msg(m.path.get_name(), m.iface.get_name())
48 .append1(&*name);
49
50 // Two messages will be returned - one is the method return (and should always be there),
51 // and in our case we also have a signal we want to send at the same time.
52 Ok(vec!(mret, sig))
53
54 // Our method has one output argument and one input argument.
55 }).outarg::<&str,_>("reply")
56 .inarg::<&str,_>("name")
57
58 // We also add the signal to the interface. This is mainly for introspection.
59 ).add_s(signal2)
60
61 // Also add the root path, to help introspection from debugging tools.
62 )).add(f.object_path("/", ()).introspectable());
63
64 // We add the tree to the connection so that incoming method calls will be handled.
65 tree.start_receive(&c);
66
67 // Serve clients forever.
68 loop { c.process(Duration::from_millis(1000))?; }
69}
Sourcepub fn in_args<Z: Into<Argument>, A: IntoIterator<Item = Z>>(self, a: A) -> Self
pub fn in_args<Z: Into<Argument>, A: IntoIterator<Item = Z>>(self, a: A) -> Self
Builder method that adds multiple “in” Arguments to this Method.
Sourcepub fn out_arg<A: Into<Argument>>(self, a: A) -> Self
pub fn out_arg<A: Into<Argument>>(self, a: A) -> Self
Builder method that adds an “out” Argument to this Method.
Sourcepub fn outarg<A: Arg, S: Into<String>>(self, s: S) -> Self
pub fn outarg<A: Arg, S: Into<String>>(self, s: S) -> Self
Builder method that adds an “out” Argument to this Method.
Examples found in repository?
examples/server.rs (line 55)
16fn main() -> Result<(), Box<dyn Error>> {
17 // Let's start by starting up a connection to the session bus and request a name.
18 let c = LocalConnection::new_session()?;
19 c.request_name("com.example.dbustest", false, true, false)?;
20
21 // The choice of factory tells us what type of tree we want,
22 // and if we want any extra data inside. We pick the simplest variant.
23 let f = Factory::new_fn::<()>();
24
25 // We create the signal first, since we'll need it in both inside the method callback
26 // and when creating the tree.
27 let signal = Arc::new(f.signal("HelloHappened", ()).sarg::<&str,_>("sender"));
28 let signal2 = signal.clone();
29
30 // We create a tree with one object path inside and make that path introspectable.
31 let tree = f.tree(()).add(f.object_path("/hello", ()).introspectable().add(
32
33 // We add an interface to the object path...
34 f.interface("com.example.dbustest", ()).add_m(
35
36 // ...and a method inside the interface.
37 f.method("Hello", (), move |m| {
38
39 // This is the callback that will be called when another peer on the bus calls our method.
40 // the callback receives "MethodInfo" struct and can return either an error, or a list of
41 // messages to send back.
42
43 let name: &str = m.msg.read1()?;
44 let s = format!("Hello {}!", name);
45 let mret = m.msg.method_return().append1(s);
46
47 let sig = signal.msg(m.path.get_name(), m.iface.get_name())
48 .append1(&*name);
49
50 // Two messages will be returned - one is the method return (and should always be there),
51 // and in our case we also have a signal we want to send at the same time.
52 Ok(vec!(mret, sig))
53
54 // Our method has one output argument and one input argument.
55 }).outarg::<&str,_>("reply")
56 .inarg::<&str,_>("name")
57
58 // We also add the signal to the interface. This is mainly for introspection.
59 ).add_s(signal2)
60
61 // Also add the root path, to help introspection from debugging tools.
62 )).add(f.object_path("/", ()).introspectable());
63
64 // We add the tree to the connection so that incoming method calls will be handled.
65 tree.start_receive(&c);
66
67 // Serve clients forever.
68 loop { c.process(Duration::from_millis(1000))?; }
69}
Sourcepub fn out_args<Z: Into<Argument>, A: IntoIterator<Item = Z>>(
self,
a: A,
) -> Self
pub fn out_args<Z: Into<Argument>, A: IntoIterator<Item = Z>>( self, a: A, ) -> Self
Builder method that adds multiple “out” Arguments to this Method.
Sourcepub fn annotate<N: Into<String>, V: Into<String>>(
self,
name: N,
value: V,
) -> Self
pub fn annotate<N: Into<String>, V: Into<String>>( self, name: N, value: V, ) -> Self
Builder method that adds an annotation to the method.
Sourcepub fn deprecated(self) -> Self
pub fn deprecated(self) -> Self
Builder method that adds an annotation that this entity is deprecated.
Sourcepub fn call(&self, minfo: &MethodInfo<'_, M, D>) -> MethodResult
pub fn call(&self, minfo: &MethodInfo<'_, M, D>) -> MethodResult
Call the Method
Trait Implementations§
Auto Trait Implementations§
impl<M, D> Freeze for Method<M, D>
impl<M, D> RefUnwindSafe for Method<M, D>
impl<M, D> Send for Method<M, D>
impl<M, D> Sync for Method<M, D>
impl<M, D> Unpin for Method<M, D>
impl<M, D> UnwindSafe for Method<M, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more