Commit e3253001 authored by Marc-André Lureau's avatar Marc-André Lureau
Browse files

zb: re-introduce ProxyBuilder::new() for higher-level proxies



This constructor will retain the targetted high-level proxy type
associated with the builder.
Signed-off-by: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
parent 13b1751d
......@@ -75,7 +75,7 @@ async fn run() -> Result<()> {
let conn = conn.clone();
async move {
let location: AsyncLocationProxy<'_> = ProxyBuilder::new_bare(&conn).path(new)?.build();
let location: AsyncLocationProxy<'_> = ProxyBuilder::new(&conn).path(new)?.build();
println!(
"Latitude: {}\nLongitude: {}",
location.latitude().await?,
......@@ -158,7 +158,7 @@ example again to receive multiple signals on different proxies:
// Everything else remains the same before this point.
client.set_desktop_id("org.freedesktop.zbus").await?;
let props: zbus::fdo::AsyncPropertiesProxy<'_> = zbus::azync::ProxyBuilder::new_bare(&conn)
let props: zbus::fdo::AsyncPropertiesProxy<'_> = zbus::azync::ProxyBuilder::new(&conn)
.destination("org.freedesktop.GeoClue2")
.path(client.path())?
.build();
......@@ -178,7 +178,7 @@ example again to receive multiple signals on different proxies:
let conn = conn.clone();
async move {
let location: AsyncLocationProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let location: AsyncLocationProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.path(new)?
.build();
println!(
......
......@@ -261,7 +261,7 @@ client.set_desktop_id("org.freedesktop.zbus").unwrap();
client
.connect_location_updated(move |_old, new| {
let location: LocationProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let location: LocationProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.path(new.as_str())?
.build();
println!(
......@@ -336,7 +336,7 @@ actually getting set:
let conn_clone = conn.clone();
client.connect_location_updated(move |_old, new| {
let location: LocationProxy<'_> = ProxyBuilder::new_bare(&conn_clone.clone().into())
let location: LocationProxy<'_> = ProxyBuilder::new(&conn_clone.clone().into())
.destination("org.freedesktop.GeoClue2")
.path(new.as_str())?
.build();
......@@ -349,7 +349,7 @@ client.connect_location_updated(move |_old, new| {
Ok(())
}).unwrap();
let props: zbus::fdo::PropertiesProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let props: zbus::fdo::PropertiesProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.GeoClue2")
.path(client.path()).unwrap()
.build();
......
......@@ -146,7 +146,9 @@ impl<'a> ProxyBuilder<'a> {
proxy_type: std::marker::PhantomData,
}
}
}
impl<'a, T> ProxyBuilder<'a, T> {
/// Set the proxy destination address.
pub fn destination<D: Into<Cow<'a, str>>>(mut self, destination: D) -> Self {
self.destination = Some(destination.into());
......@@ -183,6 +185,22 @@ impl<'a> ProxyBuilder<'a> {
inner: Arc::new(ProxyInner::new(conn, destination, path, interface)),
}
}
}
impl<'a, T> ProxyBuilder<'a, T>
where
T: ProxyDefault + From<Proxy<'a>>,
{
/// Create a new [`ProxyBuilder`] for the given connection.
pub fn new(conn: &Connection) -> Self {
Self {
conn: conn.clone(),
destination: None,
path: None,
interface: None,
proxy_type: std::marker::PhantomData,
}
}
/// Build a proxy from the builder.
///
......@@ -190,16 +208,13 @@ impl<'a> ProxyBuilder<'a> {
/// [`dbus_proxy`] macro. When missing, default values are taken from [`ProxyDefault`].
///
/// If you need a low-level [`Proxy`], you can use [`build_bare`] instead.
pub fn build<P>(self) -> P
where
P: ProxyDefault + From<Proxy<'a>>,
{
pub fn build(self) -> T {
let conn = self.conn;
let destination = self.destination.unwrap_or_else(|| P::DESTINATION.into());
let destination = self.destination.unwrap_or_else(|| T::DESTINATION.into());
let path = self
.path
.unwrap_or_else(|| P::PATH.try_into().expect("invalid default path"));
let interface = self.interface.unwrap_or_else(|| P::INTERFACE.into());
.unwrap_or_else(|| T::PATH.try_into().expect("invalid default path"));
let interface = self.interface.unwrap_or_else(|| T::INTERFACE.into());
Proxy {
inner: Arc::new(ProxyInner::new(conn, destination, path, interface)),
......@@ -278,7 +293,7 @@ impl<'a> Proxy<'a> {
///
/// See the [xml](xml/index.html) module for parsing the result.
pub async fn introspect(&self) -> fdo::Result<String> {
let proxy: AsyncIntrospectableProxy<'_> = ProxyBuilder::new_bare(&self.inner.conn)
let proxy: AsyncIntrospectableProxy<'_> = ProxyBuilder::new(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build();
......@@ -293,7 +308,7 @@ impl<'a> Proxy<'a> {
where
T: TryFrom<OwnedValue>,
{
let proxy: AsyncPropertiesProxy<'_> = ProxyBuilder::new_bare(&self.inner.conn)
let proxy: AsyncPropertiesProxy<'_> = ProxyBuilder::new(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build();
......@@ -312,7 +327,7 @@ impl<'a> Proxy<'a> {
where
T: Into<Value<'t>>,
{
let proxy: AsyncPropertiesProxy<'_> = ProxyBuilder::new_bare(&self.inner.conn)
let proxy: AsyncPropertiesProxy<'_> = ProxyBuilder::new(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build();
......
......@@ -606,7 +606,7 @@ mod tests {
) -> zbus::Result<(OwnedValue, OwnedObjectPath)>;
}
let proxy: SecretProxy<'_> = zbus::azync::ProxyBuilder::new_bare(&conn.clone().into())
let proxy: SecretProxy<'_> = zbus::azync::ProxyBuilder::new(&conn.clone().into())
.destination(&service_name)
.path("/org/freedesktop/secrets")
.unwrap()
......
......@@ -707,16 +707,15 @@ mod tests {
fn my_iface_test(tx: Sender<()>) -> std::result::Result<u32, Box<dyn Error>> {
let conn = Connection::new_session()?;
let proxy: MyIfaceProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let proxy: MyIfaceProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.MyService")
.path("/org/freedesktop/MyService")?
.build();
let props_proxy: zbus::fdo::PropertiesProxy<'_> =
ProxyBuilder::new_bare(&conn.clone().into())
.destination("org.freedesktop.MyService")
.path("/org/freedesktop/MyService")?
.build();
let props_proxy: zbus::fdo::PropertiesProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.MyService")
.path("/org/freedesktop/MyService")?
.build();
props_proxy
.connect_properties_changed(|_, changed, _| {
......@@ -775,7 +774,7 @@ mod tests {
let val = proxy.ping()?;
proxy.create_obj("MyObj")?;
let my_obj_proxy: MyIfaceProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let my_obj_proxy: MyIfaceProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.MyService")
.path("/zbus/test/MyObj")?
.build();
......
......@@ -172,7 +172,7 @@ mod tests {
let conn = Connection::new_session()?;
let mut receiver = SignalReceiver::new(conn.clone());
let proxy1: MultiSignalProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let proxy1: MultiSignalProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.zbus.MultiSignal")
.path("/org/freedesktop/zbus/MultiSignal/1")?
.build();
......@@ -185,7 +185,7 @@ mod tests {
})?;
receiver.receive_for(&proxy1)?;
let proxy2: MultiSignalProxy<'_> = ProxyBuilder::new_bare(&conn.clone().into())
let proxy2: MultiSignalProxy<'_> = ProxyBuilder::new(&conn.clone().into())
.destination("org.freedesktop.zbus.MultiSignal")
.path("/org/freedesktop/zbus/MultiSignal/2")?
.build();
......
......@@ -148,7 +148,7 @@ pub fn create_proxy(args: &[NestedMeta], input: &ItemTrait, azync: bool) -> Toke
/// Creates a new proxy with the default service & path.
pub fn new(conn: &#connection) -> Self {
let conn = &conn.clone().into();
::#zbus::azync::ProxyBuilder::new_bare(conn).build()
::#zbus::azync::ProxyBuilder::new(conn).build()
}
/// Consumes `self`, returning the underlying `zbus::Proxy`.
......@@ -264,7 +264,7 @@ fn gen_proxy_method_call(
&(#(#args),*),
)
#wait?;
let proxy: #proxy<'_> = ::#zbus::azync::ProxyBuilder::new_bare(&self.0.connection().clone().into())
let proxy: #proxy<'_> = ::#zbus::azync::ProxyBuilder::new(&self.0.connection().clone().into())
.path(object_path)?
.build();
Ok(proxy)
......
......@@ -19,12 +19,12 @@ use gen::GenTrait;
fn main() -> Result<(), Box<dyn Error>> {
let input_src;
let proxy = |conn: zbus::Connection, service, path| {
zbus::azync::ProxyBuilder::new_bare(&conn.into())
let proxy = |conn: zbus::Connection, service, path| -> zbus::fdo::IntrospectableProxy<'_> {
zbus::azync::ProxyBuilder::new(&conn.into())
.destination(service)
.path(path)
.expect("invalid path")
.build::<zbus::fdo::IntrospectableProxy<'_>>()
.build()
};
let node: Node = match args().nth(1) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment