forked from Cohedrin/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.rs
72 lines (66 loc) · 1.96 KB
/
bind.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use futures::sync::mpsc;
use futures::{Poll, Stream};
use postgres_protocol::message::backend::Message;
use proto::client::{Client, PendingRequest};
use proto::portal::Portal;
use proto::statement::Statement;
use state_machine_future::RentToOwn;
use Error;
#[derive(StateMachineFuture)]
pub enum Bind {
#[state_machine_future(start, transitions(ReadBindComplete))]
Start {
client: Client,
request: PendingRequest,
name: String,
statement: Statement,
},
#[state_machine_future(transitions(Finished))]
ReadBindComplete {
receiver: mpsc::Receiver<Message>,
client: Client,
name: String,
statement: Statement,
},
#[state_machine_future(ready)]
Finished(Portal),
#[state_machine_future(error)]
Failed(Error),
}
impl PollBind for Bind {
fn poll_start<'a>(state: &'a mut RentToOwn<'a, Start>) -> Poll<AfterStart, Error> {
let state = state.take();
let receiver = state.client.send(state.request)?;
transition!(ReadBindComplete {
receiver,
client: state.client,
name: state.name,
statement: state.statement,
})
}
fn poll_read_bind_complete<'a>(
state: &'a mut RentToOwn<'a, ReadBindComplete>,
) -> Poll<AfterReadBindComplete, Error> {
let message = try_ready_receive!(state.receiver.poll());
let state = state.take();
match message {
Some(Message::BindComplete) => transition!(Finished(Portal::new(
state.client.downgrade(),
state.name,
state.statement,
))),
Some(_) => Err(Error::unexpected_message()),
None => Err(Error::closed()),
}
}
}
impl BindFuture {
pub fn new(
client: Client,
request: PendingRequest,
name: String,
statement: Statement,
) -> BindFuture {
Bind::start(client, request, name, statement)
}
}