1use std::marker::PhantomData;
2
3use rmpv::Value;
4
5use neovim;
6use rpc::model::FromVal;
7use session::ClientConnection;
8
9pub struct AsyncCall<'a, R: FromVal<Value>> {
10 method: String,
11 args: Vec<Value>,
12 client: &'a mut ClientConnection,
13 cb: Option<Box<FnMut(Result<Value, Value>) + Send + 'static>>,
14 marker: PhantomData<R>,
15}
16
17impl<'a, R: FromVal<Value>> AsyncCall<'a, R> {
18 pub fn new(client: &'a mut ClientConnection, method: String, args: Vec<Value>) -> Self {
19 AsyncCall {
20 method,
21 args,
22 client,
23 cb: None,
24 marker: PhantomData,
25 }
26 }
27
28 pub fn cb<F>(mut self, cb: F) -> Self
29 where
30 F: FnOnce(Result<R, neovim::CallError>) + Send + 'static,
31 {
32 let mut cb = Some(cb);
33
34 self.cb = Some(Box::new(move |res| {
35 let res = res.map(R::from_val).map_err(neovim::map_generic_error);
36 cb.take().unwrap()(res);
37 }));
38 self
39 }
40
41 pub fn call(self) {
43 match *self.client {
44 ClientConnection::Child(ref mut client, _) => {
45 client.call_async(self.method, self.args, self.cb)
46 }
47 ClientConnection::Parent(ref mut client) => {
48 client.call_async(self.method, self.args, self.cb)
49 }
50 ClientConnection::Tcp(ref mut client) => {
51 client.call_async(self.method, self.args, self.cb)
52 }
53
54 #[cfg(unix)]
55 ClientConnection::UnixSocket(ref mut client) => {
56 client.call_async(self.method, self.args, self.cb)
57 }
58 };
59 }
60}