@@ -10,6 +10,8 @@ use postgres_protocol::message::frontend;
10
10
use std:: pin:: Pin ;
11
11
use std:: sync:: Arc ;
12
12
use std:: task:: { Context , Poll } ;
13
+ use std:: marker:: PhantomPinned ;
14
+ use pin_project:: pin_project;
13
15
14
16
pub async fn simple_query ( client : & InnerClient , query : & str ) -> Result < SimpleQueryStream , Error > {
15
17
let buf = encode ( client, query) ?;
@@ -18,6 +20,7 @@ pub async fn simple_query(client: &InnerClient, query: &str) -> Result<SimpleQue
18
20
Ok ( SimpleQueryStream {
19
21
responses,
20
22
columns : None ,
23
+ _p : PhantomPinned ,
21
24
} )
22
25
}
23
26
@@ -44,17 +47,22 @@ fn encode(client: &InnerClient, query: &str) -> Result<Bytes, Error> {
44
47
} )
45
48
}
46
49
50
+ /// A stream of simple query results.
51
+ #[ pin_project]
47
52
pub struct SimpleQueryStream {
48
53
responses : Responses ,
49
54
columns : Option < Arc < [ String ] > > ,
55
+ #[ pin]
56
+ _p : PhantomPinned ,
50
57
}
51
58
52
59
impl Stream for SimpleQueryStream {
53
60
type Item = Result < SimpleQueryMessage , Error > ;
54
61
55
- fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
62
+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
63
+ let this = self . project ( ) ;
56
64
loop {
57
- match ready ! ( self . responses. poll_next( cx) ?) {
65
+ match ready ! ( this . responses. poll_next( cx) ?) {
58
66
Message :: CommandComplete ( body) => {
59
67
let rows = body
60
68
. tag ( )
@@ -76,10 +84,10 @@ impl Stream for SimpleQueryStream {
76
84
. collect :: < Vec < _ > > ( )
77
85
. map_err ( Error :: parse) ?
78
86
. into ( ) ;
79
- self . columns = Some ( columns) ;
87
+ * this . columns = Some ( columns) ;
80
88
}
81
89
Message :: DataRow ( body) => {
82
- let row = match & self . columns {
90
+ let row = match & this . columns {
83
91
Some ( columns) => SimpleQueryRow :: new ( columns. clone ( ) , body) ?,
84
92
None => return Poll :: Ready ( Some ( Err ( Error :: unexpected_message ( ) ) ) ) ,
85
93
} ;
0 commit comments