1use zbus_names::BusName;
23use crate::{zvariant::ObjectPath, Connection, Error, Result};
4use std::convert::TryInto;
56/// A signal emission context.
7///
8/// For signal emission using the high-level API, you'll need instances of this type.
9///
10/// See [`crate::InterfaceRef::signal_context`] and [`crate::dbus_interface`]
11/// documentation for details and examples of this type in use.
12#[derive(Clone, Debug)]
13pub struct SignalContext<'s> {
14 conn: Connection,
15 path: ObjectPath<'s>,
16 destination: Option<BusName<'s>>,
17}
1819impl<'s> SignalContext<'s> {
20/// Create a new signal context for the given connection and object path.
21pub fn new<P>(conn: &Connection, path: P) -> Result<Self>
22where
23P: TryInto<ObjectPath<'s>>,
24 P::Error: Into<Error>,
25 {
26 path.try_into()
27 .map(|p| Self {
28 conn: conn.clone(),
29 path: p,
30 destination: None,
31 })
32 .map_err(Into::into)
33 }
3435/// Create a new signal context for the given connection and object path.
36pub fn from_parts(conn: Connection, path: ObjectPath<'s>) -> Self {
37Self {
38 conn,
39 path,
40 destination: None,
41 }
42 }
4344/// Set the destination for the signal emission.
45 ///
46 /// Signals are typically broadcasted and thus don't have a destination. However, there are
47 /// cases where you need to unicast signals to specific peers. This method allows you to set the
48 /// destination for the signals emitted with this context.
49pub fn set_destination(mut self, destination: BusName<'s>) -> Self {
50self.destination = Some(destination);
5152self
53}
5455/// Get a reference to the associated connection.
56pub fn connection(&self) -> &Connection {
57&self.conn
58 }
5960/// Get a reference to the associated object path.
61pub fn path(&self) -> &ObjectPath<'s> {
62&self.path
63 }
6465/// Get a reference to the associated destination (if any).
66pub fn destination(&self) -> Option<&BusName<'s>> {
67self.destination.as_ref()
68 }
6970/// Creates an owned clone of `self`.
71pub fn to_owned(&self) -> SignalContext<'static> {
72 SignalContext {
73 conn: self.conn.clone(),
74 path: self.path.to_owned(),
75 destination: self.destination.as_ref().map(|d| d.to_owned()),
76 }
77 }
7879/// Creates an owned clone of `self`.
80pub fn into_owned(self) -> SignalContext<'static> {
81 SignalContext {
82 conn: self.conn,
83 path: self.path.into_owned(),
84 destination: self.destination.map(|d| d.into_owned()),
85 }
86 }
87}