Use AsRefClosure when generating the bindings

This simplifies things significantly \o/
This commit is contained in:
2020-05-11 00:48:18 +01:00
parent ad0d95ab40
commit 9151f64a6d
126 changed files with 1633 additions and 1054 deletions

View File

@@ -7,7 +7,7 @@ use std::time::Duration;
pub const CONN_BUS_NAME: &'static str = "org.freedesktop.Telepathy.Connection.padfoot.delta";
pub const CONN_OBJECT_PATH: &'static str = "/org/freedesktop/Telepathy/Connection/padfoot/delta";
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Connection {
id: String,
account: String,
@@ -67,19 +67,6 @@ mod tests {
}
}
#[derive(Debug, Default)]
struct CData;
impl dbus::tree::DataType for CData {
type Interface = Connection;
type Tree = ();
type Property = ();
type ObjectPath = ();
type Method = ();
type Signal = ();
}
impl Connection {
// This is run inside its own thread
//
@@ -88,8 +75,10 @@ impl Connection {
pub fn run(self) {
let bus = self.bus();
let path = self.path();
let f = tree::Factory::new_fn::<CData>();
let iface = telepathy::connection_server(&f, self, |m| m.iface.get_data());
let c_rc = std::rc::Rc::new(self);
let f = tree::Factory::new_fn::<()>();
let iface = telepathy::connection_server(&f, (), move |_| c_rc.clone());
let mut tree = f.tree(());
tree = tree.add(f.object_path(path, ()).introspectable().add(iface));
@@ -169,6 +158,10 @@ impl Connection {
}
}
impl AsRef<dyn telepathy::Connection + 'static> for std::rc::Rc<Connection> {
fn as_ref(&self) -> &(dyn telepathy::Connection + 'static) { &**self }
}
impl telepathy::Connection for Connection {
fn connect(&self) -> Result<(), tree::MethodErr> {
Err(tree::MethodErr::no_arg()) // FIXME: should be NotImplemented?

View File

@@ -11,25 +11,21 @@ pub const CM_OBJECT_PATH: &'static str = "/org/freedesktop/Telepathy/ConnectionM
#[derive(Debug)]
pub struct ConnectionManager {
conns: std::sync::Mutex<HashSet<String>>,
pub sender: Option<mpsc::Sender<dbus::message::Message>>,
sender: mpsc::Sender<dbus::Message>,
}
impl Default for ConnectionManager {
fn default() -> Self {
ConnectionManager {
conns: std::sync::Mutex::new(HashSet::<String>::new()),
sender: None,
}
}
impl AsRef<dyn telepathy::ConnectionManager + 'static> for std::rc::Rc<ConnectionManager> {
fn as_ref(&self) -> &(dyn telepathy::ConnectionManager + 'static) { &**self }
}
impl ConnectionManager {
pub fn new(sender: Option<mpsc::Sender<dbus::message::Message>>) -> Self {
let mut cm: ConnectionManager = Default::default();
pub fn new() -> (Self, mpsc::Receiver<dbus::Message>) {
let (msg_s, msg_r) = std::sync::mpsc::channel::<dbus::Message>();
cm.sender = sender;
cm
(ConnectionManager {
conns: std::sync::Mutex::new(HashSet::<String>::new()),
sender: msg_s,
}, msg_r)
}
fn create_connection(
@@ -59,23 +55,17 @@ impl ConnectionManager {
// Emit a NewConnection signal for the benefit of others, but the caller
// learns immediately
match &self.sender {
Some(s) => {
let sig = telepathy::ConnectionManagerNewConnection {
bus_name: bus.to_owned(),
object_path: dbus_conn_path.clone(),
protocol: super::PROTO_NAME.to_string(),
};
let dbus_cm_path = dbus::strings::Path::new(CM_OBJECT_PATH.to_string())
.expect("Object path should meet DBUS requirements");
s.send(sig.to_emit_message(&dbus_cm_path))
.expect("send signal");
}
_ => {}
let sig = telepathy::ConnectionManagerNewConnection {
bus_name: bus.to_owned(),
object_path: dbus_conn_path.clone(),
protocol: super::PROTO_NAME.to_string(),
};
let dbus_cm_path = dbus::strings::Path::new(CM_OBJECT_PATH.to_string())
.expect("Object path should meet DBUS requirements");
self.sender.send(sig.to_emit_message(&dbus_cm_path)).expect("send signal");
// The bus name *must* be org.freedesktop.Telepathy.Connection.padfoot.delta.<name>
Ok((bus, dbus_conn_path))
}

View File

@@ -8,7 +8,8 @@ pub const PROTO_BUS_NAME: &'static str =
"org.freedesktop.Telepathy.ConnectionManager.padfoot.delta";
pub const PROTO_NAME: &'static str = "delta";
#[derive(Clone, Copy, Default, Debug)]
#[derive(Debug)]
pub struct Protocol {}
pub type Variant = arg::Variant<Box<dyn arg::RefArg + 'static>>;
@@ -74,6 +75,10 @@ pub fn requestables() -> Vec<RequestableChannelSpec> {
vec![(rf, ra)]
}
impl AsRef<dyn telepathy::Protocol + 'static> for std::rc::Rc<Protocol> {
fn as_ref(&self) -> &(dyn telepathy::Protocol + 'static) { &**self }
}
impl telepathy::Protocol for Protocol {
fn identify_account(&self, params: HashMap<&str, Variant>) -> Result<String, tree::MethodErr> {
println!("Protocol::identify_account({:?})", params);