zbus/blocking/proxy/
builder.rs

1use static_assertions::assert_impl_all;
2use zbus_names::{BusName, InterfaceName};
3use zvariant::ObjectPath;
4
5use crate::{blocking::Connection, proxy::CacheProperties, utils::block_on, Error, Result};
6
7pub use crate::proxy::ProxyDefault;
8
9/// Builder for proxies.
10#[derive(Debug, Clone)]
11pub struct Builder<'a, T = ()>(crate::proxy::Builder<'a, T>);
12
13assert_impl_all!(Builder<'_>: Send, Sync, Unpin);
14
15impl<'a, T> Builder<'a, T> {
16    /// Set the proxy destination address.
17    pub fn destination<D>(self, destination: D) -> Result<Self>
18    where
19        D: TryInto<BusName<'a>>,
20        D::Error: Into<Error>,
21    {
22        crate::proxy::Builder::destination(self.0, destination).map(Self)
23    }
24
25    /// Set the proxy path.
26    pub fn path<P>(self, path: P) -> Result<Self>
27    where
28        P: TryInto<ObjectPath<'a>>,
29        P::Error: Into<Error>,
30    {
31        crate::proxy::Builder::path(self.0, path).map(Self)
32    }
33
34    /// Set the proxy interface.
35    pub fn interface<I>(self, interface: I) -> Result<Self>
36    where
37        I: TryInto<InterfaceName<'a>>,
38        I::Error: Into<Error>,
39    {
40        crate::proxy::Builder::interface(self.0, interface).map(Self)
41    }
42
43    /// Set whether to cache properties.
44    #[must_use]
45    pub fn cache_properties(self, cache: CacheProperties) -> Self {
46        Self(self.0.cache_properties(cache))
47    }
48
49    /// Specify a set of properties (by name) which should be excluded from caching.
50    #[must_use]
51    pub fn uncached_properties(self, properties: &[&'a str]) -> Self {
52        Self(self.0.uncached_properties(properties))
53    }
54
55    /// Build a proxy from the builder.
56    ///
57    /// # Panics
58    ///
59    /// Panics if the builder is lacking the necessary details to build a proxy.
60    pub fn build(self) -> Result<T>
61    where
62        T: From<crate::Proxy<'a>>,
63    {
64        block_on(self.0.build())
65    }
66}
67
68impl<'a, T> Builder<'a, T>
69where
70    T: ProxyDefault,
71{
72    /// Create a new [`Builder`] for the given connection.
73    #[must_use]
74    pub fn new(conn: &Connection) -> Self {
75        Self(crate::proxy::Builder::new(&conn.clone().into()))
76    }
77
78    /// Create a new [`Builder`] for the given connection.
79    #[must_use]
80    #[deprecated(
81        since = "4.0.0",
82        note = "use `Builder::new` instead, which is now generic over the proxy type"
83    )]
84    pub fn new_bare(conn: &Connection) -> Self {
85        Self::new(conn)
86    }
87}