Hi, I'd like to create a struct where a dbus::blocking::Proxy is stored, since Proxy requires a connection I store that as well in my struct but the code below doesn't compile. While the compiler's error makes sense I don't know how to solve this. Any ideas?
struct DBusNotifier<'a> {
conn: Box<Connection>,
proxy: Box<dbus::blocking::Proxy<'static, &'a Connection>>,
}
impl<'a> DBusNotifier<'a> {
fn new() -> Result<DBusNotifier<'a>, &'static str> {
let conn = match Connection::new_session() {
Ok(conn) => Box::new(conn),
Err(e) => {
error!("{}", e);
return Err("cannot create a new connection");
}
};
let proxy = Box::new(conn.with_proxy(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
Duration::from_millis(2000),
));
Ok(DBusNotifier {
proxy: proxy,
conn: conn,
})
}
}
Compiling imap-notify v0.1.0 (/home/acardace/work/imap-notify)
error[E0515]: cannot return value referencing local data `*conn`
--> src/dbus_notify.rs:110:9
|
104 | let proxy = Box::new(conn.with_proxy(
| ---- `*conn` is borrowed here
...
110 | / Ok(DBusNotifier {
111 | | proxy: proxy,
112 | | conn: conn,
113 | | })
| |__________^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `conn` because it is borrowed
--> src/dbus_notify.rs:112:19
|
94 | impl<'a> DBusNotifier<'a> {
| -- lifetime `'a` defined here
...
104 | let proxy = Box::new(conn.with_proxy(
| ---- borrow of `*conn` occurs here
...
110 | / Ok(DBusNotifier {
111 | | proxy: proxy,
112 | | conn: conn,
| | ^^^^ move out of `conn` occurs here
113 | | })
| |__________- returning this value requires that `*conn` is borrowed for `'a`
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0505, E0515.
For more information about an error, try `rustc --explain E0505`.