Update futures-state-stream

This commit is contained in:
Steven Fackler 2017-09-30 14:56:15 -07:00
parent acb424afa4
commit 9373d2fa55
3 changed files with 125 additions and 86 deletions

View File

@ -42,7 +42,7 @@ with-openssl = ["tokio-openssl", "openssl"]
bytes = "0.4" bytes = "0.4"
fallible-iterator = "0.1.3" fallible-iterator = "0.1.3"
futures = "0.1.7" futures = "0.1.7"
futures-state-stream = "0.1" futures-state-stream = "0.2"
postgres-protocol = { version = "0.3.0", path = "../postgres-protocol" } postgres-protocol = { version = "0.3.0", path = "../postgres-protocol" }
postgres-shared = { version = "0.4.0", path = "../postgres-shared" } postgres-shared = { version = "0.4.0", path = "../postgres-shared" }
tokio-core = "0.1" tokio-core = "0.1"

View File

@ -73,7 +73,7 @@ extern crate tokio_uds;
use fallible_iterator::FallibleIterator; use fallible_iterator::FallibleIterator;
use futures::{Future, IntoFuture, BoxFuture, Stream, Sink, Poll, StartSend, Async}; use futures::{Future, IntoFuture, BoxFuture, Stream, Sink, Poll, StartSend, Async};
use futures::future::Either; use futures::future::Either;
use futures_state_stream::{StreamEvent, StateStream, BoxStateStream, FutureExt}; use futures_state_stream::{StreamEvent, StateStream, FutureExt};
use postgres_protocol::authentication; use postgres_protocol::authentication;
use postgres_protocol::message::{backend, frontend}; use postgres_protocol::message::{backend, frontend};
use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields}; use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields};
@ -168,7 +168,7 @@ where
c.send(buf).map_err(error::io) c.send(buf).map_err(error::io)
}) })
.map(|_| ()) .map(|_| ())
.boxed() .boxed2()
} }
struct InnerConnection { struct InnerConnection {
@ -192,7 +192,7 @@ impl InnerConnection {
io::ErrorKind::Other, io::ErrorKind::Other,
"connection desynchronized due to earlier IO error", "connection desynchronized due to earlier IO error",
); );
return Err((e, self)).into_future().boxed(); return Err((e, self)).into_future().boxed2();
} }
self.into_future() self.into_future()
@ -225,7 +225,7 @@ impl InnerConnection {
s.desynchronized = true; s.desynchronized = true;
(e, s) (e, s)
}) })
.boxed() .boxed2()
} }
} }
@ -329,7 +329,7 @@ impl Connection {
}).and_then(|(s, params)| s.startup(params)) }).and_then(|(s, params)| s.startup(params))
.and_then(|(s, params)| s.handle_auth(params)) .and_then(|(s, params)| s.handle_auth(params))
.and_then(|s| s.finish_startup()) .and_then(|s| s.finish_startup())
.boxed() .boxed2()
} }
fn startup(self, params: ConnectParams) -> BoxFuture<(Connection, ConnectParams), Error> { fn startup(self, params: ConnectParams) -> BoxFuture<(Connection, ConnectParams), Error> {
@ -349,7 +349,7 @@ impl Connection {
.and_then(move |()| self.0.send(buf)) .and_then(move |()| self.0.send(buf))
.map_err(error::io) .map_err(error::io)
.map(move |s| (Connection(s), params)) .map(move |s| (Connection(s), params))
.boxed() .boxed2()
} }
fn handle_auth(self, params: ConnectParams) -> BoxFuture<Connection, Error> { fn handle_auth(self, params: ConnectParams) -> BoxFuture<Connection, Error> {
@ -406,7 +406,7 @@ impl Connection {
Some(m) => Either::A(s.handle_auth_response(m)), Some(m) => Either::A(s.handle_auth_response(m)),
None => Either::B(Ok(s).into_future()), None => Either::B(Ok(s).into_future()),
}) })
.boxed() .boxed2()
} }
fn handle_auth_response(self, message: Vec<u8>) -> BoxFuture<Connection, Error> { fn handle_auth_response(self, message: Vec<u8>) -> BoxFuture<Connection, Error> {
@ -419,7 +419,7 @@ impl Connection {
backend::Message::ErrorResponse(body) => Err(err(&mut body.fields())), backend::Message::ErrorResponse(body) => Err(err(&mut body.fields())),
_ => Err(bad_message()), _ => Err(bad_message()),
}) })
.boxed() .boxed2()
} }
fn finish_startup(self) -> BoxFuture<Connection, Error> { fn finish_startup(self) -> BoxFuture<Connection, Error> {
@ -438,7 +438,7 @@ impl Connection {
} }
_ => Either::B(Err(bad_message()).into_future()), _ => Either::B(Err(bad_message()).into_future()),
}) })
.boxed() .boxed2()
} }
fn simple_query( fn simple_query(
@ -447,14 +447,14 @@ impl Connection {
) -> BoxFuture<(Vec<RowData>, Connection), (Error, Connection)> { ) -> BoxFuture<(Vec<RowData>, Connection), (Error, Connection)> {
let mut buf = vec![]; let mut buf = vec![];
if let Err(e) = frontend::query(query, &mut buf) { if let Err(e) = frontend::query(query, &mut buf) {
return Err((error::io(e), self)).into_future().boxed(); return Err((error::io(e), self)).into_future().boxed2();
} }
self.0 self.0
.send2(buf) .send2(buf)
.map_err(|(e, s)| (error::io(e), Connection(s))) .map_err(|(e, s)| (error::io(e), Connection(s)))
.and_then(|s| Connection(s).simple_read_rows(vec![])) .and_then(|s| Connection(s).simple_read_rows(vec![]))
.boxed() .boxed2()
} }
// This has its own read_rows since it will need to handle multiple query completions // This has its own read_rows since it will need to handle multiple query completions
@ -467,7 +467,7 @@ impl Connection {
.map_err(|(e, s)| (error::io(e), Connection(s))) .map_err(|(e, s)| (error::io(e), Connection(s)))
.and_then(|(m, s)| match m { .and_then(|(m, s)| match m {
backend::Message::ReadyForQuery(_) => { backend::Message::ReadyForQuery(_) => {
Ok((rows, Connection(s))).into_future().boxed() Ok((rows, Connection(s))).into_future().boxed2()
} }
backend::Message::DataRow(body) => { backend::Message::DataRow(body) => {
match RowData::new(body) { match RowData::new(body) {
@ -475,16 +475,16 @@ impl Connection {
rows.push(row); rows.push(row);
Connection(s).simple_read_rows(rows) Connection(s).simple_read_rows(rows)
} }
Err(e) => Err((error::io(e), Connection(s))).into_future().boxed(), Err(e) => Err((error::io(e), Connection(s))).into_future().boxed2(),
} }
} }
backend::Message::EmptyQueryResponse | backend::Message::EmptyQueryResponse |
backend::Message::CommandComplete(_) | backend::Message::CommandComplete(_) |
backend::Message::RowDescription(_) => Connection(s).simple_read_rows(rows), backend::Message::RowDescription(_) => Connection(s).simple_read_rows(rows),
backend::Message::ErrorResponse(body) => Connection(s).ready_err(body), backend::Message::ErrorResponse(body) => Connection(s).ready_err(body),
_ => Err((bad_message(), Connection(s))).into_future().boxed(), _ => Err((bad_message(), Connection(s))).into_future().boxed2(),
}) })
.boxed() .boxed2()
} }
fn ready<T>(self, t: T) -> BoxFuture<(T, Connection), (Error, Connection)> fn ready<T>(self, t: T) -> BoxFuture<(T, Connection), (Error, Connection)>
@ -499,7 +499,7 @@ impl Connection {
_ => Err((bad_message(), Connection(s))), _ => Err((bad_message(), Connection(s))),
}) })
.and_then(|s| Connection(s).close_gc().map(|s| (t, s))) .and_then(|s| Connection(s).close_gc().map(|s| (t, s)))
.boxed() .boxed2()
} }
fn close_gc(self) -> BoxFuture<Connection, (Error, Connection)> { fn close_gc(self) -> BoxFuture<Connection, (Error, Connection)> {
@ -510,7 +510,7 @@ impl Connection {
messages.push(buf); messages.push(buf);
} }
if messages.is_empty() { if messages.is_empty() {
return Ok(self).into_future().boxed(); return Ok(self).into_future().boxed2();
} }
let mut buf = vec![]; let mut buf = vec![];
@ -522,7 +522,7 @@ impl Connection {
)) ))
.map_err(|(e, s, _)| (error::io(e), Connection(s))) .map_err(|(e, s, _)| (error::io(e), Connection(s)))
.and_then(|s| Connection(s.0).finish_close_gc()) .and_then(|s| Connection(s.0).finish_close_gc())
.boxed() .boxed2()
} }
fn finish_close_gc(self) -> BoxFuture<Connection, (Error, Connection)> { fn finish_close_gc(self) -> BoxFuture<Connection, (Error, Connection)> {
@ -535,7 +535,7 @@ impl Connection {
backend::Message::ErrorResponse(body) => Either::B(Connection(s).ready_err(body)), backend::Message::ErrorResponse(body) => Either::B(Connection(s).ready_err(body)),
_ => Either::A(Err((bad_message(), Connection(s))).into_future()), _ => Either::A(Err((bad_message(), Connection(s))).into_future()),
}) })
.boxed() .boxed2()
} }
fn ready_err<T>(self, body: ErrorResponseBody) -> BoxFuture<T, (Error, Connection)> fn ready_err<T>(self, body: ErrorResponseBody) -> BoxFuture<T, (Error, Connection)>
@ -544,11 +544,11 @@ impl Connection {
{ {
let e = match DbError::new(&mut body.fields()) { let e = match DbError::new(&mut body.fields()) {
Ok(e) => e, Ok(e) => e,
Err(e) => return Err((error::io(e), self)).into_future().boxed(), Err(e) => return Err((error::io(e), self)).into_future().boxed2(),
}; };
self.ready(e) self.ready(e)
.and_then(|(e, s)| Err((error::db(e), s))) .and_then(|(e, s)| Err((error::db(e), s)))
.boxed() .boxed2()
} }
/// Execute a sequence of SQL statements. /// Execute a sequence of SQL statements.
@ -565,7 +565,7 @@ impl Connection {
/// data in the statement. Do not form statements via string concatenation /// data in the statement. Do not form statements via string concatenation
/// and feed them into this method. /// and feed them into this method.
pub fn batch_execute(self, query: &str) -> BoxFuture<Connection, (Error, Connection)> { pub fn batch_execute(self, query: &str) -> BoxFuture<Connection, (Error, Connection)> {
self.simple_query(query).map(|r| r.1).boxed() self.simple_query(query).map(|r| r.1).boxed2()
} }
fn raw_prepare( fn raw_prepare(
@ -578,7 +578,7 @@ impl Connection {
let mut sync = vec![]; let mut sync = vec![];
frontend::sync(&mut sync); frontend::sync(&mut sync);
if let Err(e) = frontend::parse(name, query, None, &mut parse).and_then(|()| frontend::describe(b'S', name, &mut describe)) { if let Err(e) = frontend::parse(name, query, None, &mut parse).and_then(|()| frontend::describe(b'S', name, &mut describe)) {
return Err((error::io(e), self)).into_future().boxed(); return Err((error::io(e), self)).into_future().boxed2();
} }
let it = Some(parse) let it = Some(parse)
@ -590,7 +590,7 @@ impl Connection {
.map_err(|(e, s, _)| (e, s)) .map_err(|(e, s, _)| (e, s))
.and_then(|s| s.0.read()) .and_then(|s| s.0.read())
.map_err(|(e, s)| (error::io(e), Connection(s))) .map_err(|(e, s)| (error::io(e), Connection(s)))
.boxed() // work around nonlinear trans blowup .boxed2() // work around nonlinear trans blowup
.and_then(|(m, s)| { .and_then(|(m, s)| {
match m { match m {
backend::Message::ParseComplete => Either::A(Ok(s).into_future()), backend::Message::ParseComplete => Either::A(Ok(s).into_future()),
@ -613,7 +613,7 @@ impl Connection {
} }
}) })
.and_then(|(p, s)| s.read().map(|(m, s)| (p, m, s)).map_err(|(e, s)| (error::io(e), Connection(s)))) .and_then(|(p, s)| s.read().map(|(m, s)| (p, m, s)).map_err(|(e, s)| (error::io(e), Connection(s))))
.boxed() // work around nonlinear trans blowup .boxed2() // work around nonlinear trans blowup
.and_then(|(p, m, s)| { .and_then(|(p, m, s)| {
match m { match m {
backend::Message::RowDescription(body) => { backend::Message::RowDescription(body) => {
@ -640,7 +640,7 @@ impl Connection {
|f, t| Column::new(f.0, t)) |f, t| Column::new(f.0, t))
.map(|(r, s)| (p, r, s)) .map(|(r, s)| (p, r, s))
}) })
.boxed() .boxed2()
} }
fn get_types<T, U, I, F, G>( fn get_types<T, U, I, F, G>(
@ -665,20 +665,20 @@ impl Connection {
out.push(build(v, ty)); out.push(build(v, ty));
s.get_types(raw, out, get_oid, build) s.get_types(raw, out, get_oid, build)
}) })
.boxed() .boxed2()
} }
None => Ok((out, self)).into_future().boxed(), None => Ok((out, self)).into_future().boxed2(),
} }
} }
fn get_type(self, oid: Oid) -> BoxFuture<(Type, Connection), (Error, Connection)> { fn get_type(self, oid: Oid) -> BoxFuture<(Type, Connection), (Error, Connection)> {
if let Some(type_) = Type::from_oid(oid) { if let Some(type_) = Type::from_oid(oid) {
return Ok((type_, self)).into_future().boxed(); return Ok((type_, self)).into_future().boxed2();
}; };
let ty = self.0.types.get(&oid).map(Clone::clone); let ty = self.0.types.get(&oid).map(Clone::clone);
if let Some(ty) = ty { if let Some(ty) = ty {
return Ok((ty, self)).into_future().boxed(); return Ok((ty, self)).into_future().boxed2();
} }
self.get_unknown_type(oid) self.get_unknown_type(oid)
@ -686,7 +686,7 @@ impl Connection {
c.0.types.insert(oid, ty.clone()); c.0.types.insert(oid, ty.clone());
(ty, c) (ty, c)
}) })
.boxed() .boxed2()
} }
fn get_unknown_type(self, oid: Oid) -> BoxFuture<(Type, Connection), (Error, Connection)> { fn get_unknown_type(self, oid: Oid) -> BoxFuture<(Type, Connection), (Error, Connection)> {
@ -731,31 +731,31 @@ impl Connection {
Either::B( Either::B(
c.get_enum_variants(oid) c.get_enum_variants(oid)
.map(|(v, c)| (Kind::Enum(v), c)) .map(|(v, c)| (Kind::Enum(v), c))
.boxed(), .boxed2(),
) )
} else if basetype != 0 { } else if basetype != 0 {
Either::B( Either::B(
c.get_type(basetype) c.get_type(basetype)
.map(|(t, c)| (Kind::Domain(t), c)) .map(|(t, c)| (Kind::Domain(t), c))
.boxed(), .boxed2(),
) )
} else if elem_oid != 0 { } else if elem_oid != 0 {
Either::B( Either::B(
c.get_type(elem_oid) c.get_type(elem_oid)
.map(|(t, c)| (Kind::Array(t), c)) .map(|(t, c)| (Kind::Array(t), c))
.boxed(), .boxed2(),
) )
} else if relid != 0 { } else if relid != 0 {
Either::B( Either::B(
c.get_composite_fields(relid) c.get_composite_fields(relid)
.map(|(f, c)| (Kind::Composite(f), c)) .map(|(f, c)| (Kind::Composite(f), c))
.boxed(), .boxed2(),
) )
} else if let Some(rngsubtype) = rngsubtype { } else if let Some(rngsubtype) = rngsubtype {
Either::B( Either::B(
c.get_type(rngsubtype) c.get_type(rngsubtype)
.map(|(t, c)| (Kind::Range(t), c)) .map(|(t, c)| (Kind::Range(t), c))
.boxed(), .boxed2(),
) )
} else { } else {
Either::A(Ok((Kind::Simple, c)).into_future()) Either::A(Ok((Kind::Simple, c)).into_future())
@ -765,12 +765,12 @@ impl Connection {
move |(k, c)| (Type::_new(name, oid, k, schema), c), move |(k, c)| (Type::_new(name, oid, k, schema), c),
)) ))
}) })
.boxed() .boxed2()
} }
fn setup_typeinfo_query(self) -> BoxFuture<Connection, (Error, Connection)> { fn setup_typeinfo_query(self) -> BoxFuture<Connection, (Error, Connection)> {
if self.0.has_typeinfo_query { if self.0.has_typeinfo_query {
return Ok(self).into_future().boxed(); return Ok(self).into_future().boxed2();
} }
self.raw_prepare( self.raw_prepare(
@ -804,7 +804,7 @@ impl Connection {
c.0.has_typeinfo_query = true; c.0.has_typeinfo_query = true;
c c
}) })
.boxed() .boxed2()
} }
fn get_enum_variants( fn get_enum_variants(
@ -827,12 +827,12 @@ impl Connection {
} }
Ok((variants, c)) Ok((variants, c))
}) })
.boxed() .boxed2()
} }
fn setup_typeinfo_enum_query(self) -> BoxFuture<Connection, (Error, Connection)> { fn setup_typeinfo_enum_query(self) -> BoxFuture<Connection, (Error, Connection)> {
if self.0.has_typeinfo_enum_query { if self.0.has_typeinfo_enum_query {
return Ok(self).into_future().boxed(); return Ok(self).into_future().boxed2();
} }
self.raw_prepare( self.raw_prepare(
@ -854,10 +854,13 @@ impl Connection {
c.0.has_typeinfo_enum_query = true; c.0.has_typeinfo_enum_query = true;
c c
}) })
.boxed() .boxed2()
} }
fn get_composite_fields(self, oid: Oid) -> BoxFuture<(Vec<Field>, Connection), (Error, Connection)> { fn get_composite_fields(
self,
oid: Oid,
) -> BoxFuture<(Vec<Field>, Connection), (Error, Connection)> {
self.setup_typeinfo_composite_query() self.setup_typeinfo_composite_query()
.and_then(move |c| { .and_then(move |c| {
c.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", &[OID], &[&oid]) c.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", &[OID], &[&oid])
@ -869,11 +872,15 @@ impl Connection {
|(mut fields, c), row| { |(mut fields, c), row| {
let name = match String::from_sql_nullable(&NAME, row.get(0)) { let name = match String::from_sql_nullable(&NAME, row.get(0)) {
Ok(name) => name, Ok(name) => name,
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()), Err(e) => {
return Either::A(Err((error::conversion(e), c)).into_future())
}
}; };
let oid = match Oid::from_sql_nullable(&OID, row.get(1)) { let oid = match Oid::from_sql_nullable(&OID, row.get(1)) {
Ok(oid) => oid, Ok(oid) => oid,
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()), Err(e) => {
return Either::A(Err((error::conversion(e), c)).into_future())
}
}; };
Either::B(c.get_type(oid).map(move |(ty, c)| { Either::B(c.get_type(oid).map(move |(ty, c)| {
fields.push(Field::new(name, ty)); fields.push(Field::new(name, ty));
@ -882,12 +889,12 @@ impl Connection {
}, },
) )
}) })
.boxed() .boxed2()
} }
fn setup_typeinfo_composite_query(self) -> BoxFuture<Connection, (Error, Connection)> { fn setup_typeinfo_composite_query(self) -> BoxFuture<Connection, (Error, Connection)> {
if self.0.has_typeinfo_composite_query { if self.0.has_typeinfo_composite_query {
return Ok(self).into_future().boxed(); return Ok(self).into_future().boxed2();
} }
self.raw_prepare( self.raw_prepare(
@ -902,7 +909,7 @@ impl Connection {
c.0.has_typeinfo_composite_query = true; c.0.has_typeinfo_composite_query = true;
c c
}) })
.boxed() .boxed2()
} }
fn raw_execute( fn raw_execute(
@ -942,11 +949,9 @@ impl Connection {
Err(frontend::BindError::Serialization(e)) => Err((error::io(e), self)), Err(frontend::BindError::Serialization(e)) => Err((error::io(e), self)),
}; };
r.and_then(|s| { r.and_then(|s| match frontend::execute(portal, 0, &mut execute) {
match frontend::execute(portal, 0, &mut execute) {
Ok(()) => Ok(s), Ok(()) => Ok(s),
Err(e) => Err((error::io(e), s)), Err(e) => Err((error::io(e), s)),
}
}).into_future() }).into_future()
.and_then(|s| { .and_then(|s| {
let it = Some(bind) let it = Some(bind)
@ -954,15 +959,21 @@ impl Connection {
.chain(Some(execute)) .chain(Some(execute))
.chain(Some(sync)) .chain(Some(sync))
.map(Ok::<_, io::Error>); .map(Ok::<_, io::Error>);
s.0.send_all2(futures::stream::iter(it)).map_err(|(e, s, _)| (error::io(e), Connection(s))) s.0.send_all2(futures::stream::iter(it)).map_err(
|(e, s, _)| {
(error::io(e), Connection(s))
},
)
})
.and_then(|s| {
s.0.read().map_err(|(e, s)| (error::io(e), Connection(s)))
}) })
.and_then(|s| s.0.read().map_err(|(e, s)| (error::io(e), Connection(s))))
.and_then(|(m, s)| match m { .and_then(|(m, s)| match m {
backend::Message::BindComplete => Either::A(Ok(Connection(s)).into_future()), backend::Message::BindComplete => Either::A(Ok(Connection(s)).into_future()),
backend::Message::ErrorResponse(body) => Either::B(Connection(s).ready_err(body)), backend::Message::ErrorResponse(body) => Either::B(Connection(s).ready_err(body)),
_ => Either::A(Err((bad_message(), Connection(s))).into_future()), _ => Either::A(Err((bad_message(), Connection(s))).into_future()),
}) })
.boxed() .boxed2()
} }
fn finish_execute(self) -> BoxFuture<(u64, Connection), (Error, Connection)> { fn finish_execute(self) -> BoxFuture<(u64, Connection), (Error, Connection)> {
@ -970,26 +981,27 @@ impl Connection {
.read() .read()
.map_err(|(e, s)| (error::io(e), Connection(s))) .map_err(|(e, s)| (error::io(e), Connection(s)))
.and_then(|(m, s)| match m { .and_then(|(m, s)| match m {
backend::Message::DataRow(_) => Connection(s).finish_execute().boxed(), backend::Message::DataRow(_) => Connection(s).finish_execute().boxed2(),
backend::Message::CommandComplete(body) => { backend::Message::CommandComplete(body) => {
let r = body.tag() let r = body.tag().map(|tag| {
.map(|tag| {
tag.split_whitespace().last().unwrap().parse().unwrap_or(0) tag.split_whitespace().last().unwrap().parse().unwrap_or(0)
}); });
match r { match r {
Ok(n) => Connection(s).ready(n).boxed(), Ok(n) => Connection(s).ready(n).boxed2(),
Err(e) => Err((error::io(e), Connection(s))).into_future().boxed(), Err(e) => Err((error::io(e), Connection(s))).into_future().boxed2(),
} }
} }
backend::Message::EmptyQueryResponse => Connection(s).ready(0).boxed(), backend::Message::EmptyQueryResponse => Connection(s).ready(0).boxed2(),
backend::Message::ErrorResponse(body) => Connection(s).ready_err(body).boxed(), backend::Message::ErrorResponse(body) => Connection(s).ready_err(body).boxed2(),
_ => Err((bad_message(), Connection(s))).into_future().boxed(), _ => Err((bad_message(), Connection(s))).into_future().boxed2(),
}) })
.boxed() .boxed2()
} }
fn read_rows(self) -> BoxStateStream<RowData, Connection, (Error, Connection)> { fn read_rows(
self,
) -> Box<StateStream<Item = RowData, State = Connection, Error = Error> + Send> {
futures_state_stream::unfold(self, |c| { futures_state_stream::unfold(self, |c| {
c.read_row().and_then(|(r, c)| match r { c.read_row().and_then(|(r, c)| match r {
Some(data) => { Some(data) => {
@ -998,7 +1010,7 @@ impl Connection {
} }
None => Either::B(c.ready(()).map(|((), c)| StreamEvent::Done(c))), None => Either::B(c.ready(()).map(|((), c)| StreamEvent::Done(c))),
}) })
}).boxed() }).boxed2()
} }
fn read_row(self) -> BoxFuture<(Option<RowData>, Connection), (Error, Connection)> { fn read_row(self) -> BoxFuture<(Option<RowData>, Connection), (Error, Connection)> {
@ -1011,7 +1023,7 @@ impl Connection {
backend::Message::DataRow(body) => { backend::Message::DataRow(body) => {
let r = match RowData::new(body) { let r = match RowData::new(body) {
Ok(r) => Ok((Some(r), c)), Ok(r) => Ok((Some(r), c)),
Err(e) => Err((error::io(e), c)) Err(e) => Err((error::io(e), c)),
}; };
Either::A(r.into_future()) Either::A(r.into_future())
} }
@ -1021,7 +1033,7 @@ impl Connection {
_ => Either::A(Err((bad_message(), c)).into_future()), _ => Either::A(Err((bad_message(), c)).into_future()),
} }
}) })
.boxed() .boxed2()
} }
/// Creates a new prepared statement. /// Creates a new prepared statement.
@ -1034,7 +1046,7 @@ impl Connection {
Statement::new(conn.0.close_sender.clone(), name, params, Arc::new(columns)); Statement::new(conn.0.close_sender.clone(), name, params, Arc::new(columns));
(stmt, conn) (stmt, conn)
}) })
.boxed() .boxed2()
} }
/// Executes a statement, returning the number of rows modified. /// Executes a statement, returning the number of rows modified.
@ -1050,7 +1062,7 @@ impl Connection {
) -> BoxFuture<(u64, Connection), (Error, Connection)> { ) -> BoxFuture<(u64, Connection), (Error, Connection)> {
self.raw_execute(statement.name(), "", statement.parameters(), params) self.raw_execute(statement.name(), "", statement.parameters(), params)
.and_then(|conn| conn.finish_execute()) .and_then(|conn| conn.finish_execute())
.boxed() .boxed2()
} }
/// Executes a statement, returning a stream over the resulting rows. /// Executes a statement, returning a stream over the resulting rows.
@ -1063,19 +1075,19 @@ impl Connection {
self, self,
statement: &Statement, statement: &Statement,
params: &[&ToSql], params: &[&ToSql],
) -> BoxStateStream<Row, Connection, (Error, Connection)> { ) -> Box<StateStream<Item = Row, State = Connection, Error = Error> + Send> {
let columns = statement.columns_arc().clone(); let columns = statement.columns_arc().clone();
self.raw_execute(statement.name(), "", statement.parameters(), params) self.raw_execute(statement.name(), "", statement.parameters(), params)
.map(|c| c.read_rows().map(move |r| Row::new(columns.clone(), r))) .map(|c| c.read_rows().map(move |r| Row::new(columns.clone(), r)))
.flatten_state_stream() .flatten_state_stream()
.boxed() .boxed2()
} }
/// Starts a new transaction. /// Starts a new transaction.
pub fn transaction(self) -> BoxFuture<Transaction, (Error, Connection)> { pub fn transaction(self) -> BoxFuture<Transaction, (Error, Connection)> {
self.simple_query("BEGIN") self.simple_query("BEGIN")
.map(|(_, c)| Transaction::new(c)) .map(|(_, c)| Transaction::new(c))
.boxed() .boxed2()
} }
/// Returns a stream of asynchronus notifications receieved from the server. /// Returns a stream of asynchronus notifications receieved from the server.
@ -1146,3 +1158,31 @@ where
{ {
io::Error::new(io::ErrorKind::InvalidInput, "unexpected message").into() io::Error::new(io::ErrorKind::InvalidInput, "unexpected message").into()
} }
trait BoxedFuture<I, E> {
fn boxed2(self) -> Box<Future<Item = I, Error = E> + Send>;
}
impl<F, I, E> BoxedFuture<I, E> for F
where
F: Future<Item = I, Error = E> + Send + 'static,
{
fn boxed2(self) -> Box<Future<Item = I, Error = E> + Send> {
Box::new(self)
}
}
trait BoxedStateStream<I, S, E> {
fn boxed2(self) -> Box<StateStream<Item = I, State = S, Error = E> + Send>;
}
impl<T, I, S, E> BoxedStateStream<I, S, E> for T
where
T: StateStream<Item = I, State = S, Error = E>
+ Send
+ 'static,
{
fn boxed2(self) -> Box<StateStream<Item = I, State = S, Error = E> + Send> {
Box::new(self)
}
}

View File

@ -1,9 +1,9 @@
//! Transactions. //! Transactions.
use futures::{Future, BoxFuture}; use futures::{Future, BoxFuture};
use futures_state_stream::{StateStream, BoxStateStream}; use futures_state_stream::StateStream;
use Connection; use {Connection, BoxedFuture, BoxedStateStream};
use error::Error; use error::Error;
use stmt::Statement; use stmt::Statement;
use types::ToSql; use types::ToSql;
@ -24,7 +24,7 @@ impl Transaction {
.batch_execute(query) .batch_execute(query)
.map(Transaction) .map(Transaction)
.map_err(transaction_err) .map_err(transaction_err)
.boxed() .boxed2()
} }
/// Like `Connection::prepare`. /// Like `Connection::prepare`.
@ -33,7 +33,7 @@ impl Transaction {
.prepare(query) .prepare(query)
.map(|(s, c)| (s, Transaction(c))) .map(|(s, c)| (s, Transaction(c)))
.map_err(transaction_err) .map_err(transaction_err)
.boxed() .boxed2()
} }
/// Like `Connection::execute`. /// Like `Connection::execute`.
@ -46,7 +46,7 @@ impl Transaction {
.execute(statement, params) .execute(statement, params)
.map(|(n, c)| (n, Transaction(c))) .map(|(n, c)| (n, Transaction(c)))
.map_err(transaction_err) .map_err(transaction_err)
.boxed() .boxed2()
} }
/// Like `Connection::query`. /// Like `Connection::query`.
@ -54,12 +54,11 @@ impl Transaction {
self, self,
statement: &Statement, statement: &Statement,
params: &[&ToSql], params: &[&ToSql],
) -> BoxStateStream<Row, Transaction, (Error, Transaction)> { ) -> Box<StateStream<Item = Row, State = Transaction, Error = Error> + Send> {
self.0 self.0
.query(statement, params) .query(statement, params)
.map_state(Transaction) .map_state(Transaction)
.map_err(transaction_err) .boxed2()
.boxed()
} }
/// Commits the transaction. /// Commits the transaction.
@ -73,7 +72,7 @@ impl Transaction {
} }
fn finish(self, query: &str) -> BoxFuture<Connection, (Error, Connection)> { fn finish(self, query: &str) -> BoxFuture<Connection, (Error, Connection)> {
self.0.simple_query(query).map(|(_, c)| c).boxed() self.0.simple_query(query).map(|(_, c)| c).boxed2()
} }
} }