77// modified, or distributed except according to those terms.
88
99use :: futures:: Future ;
10- use mysql_async:: { MyFuture , Pool , QueryResult , Opts } ;
1110use mysql_async:: error:: Error ;
1211use mysql_async:: prelude:: * ;
12+ use mysql_async:: { MyFuture , Opts , Pool , QueryResult } ;
1313
1414use std:: env;
1515use std:: io;
1616
1717/// Same as `tokio::run`, but will panic if future panics and will return the result
1818/// of future execution.
1919fn run < F , T , U > ( future : F ) -> Result < T , U >
20- where
21- F : Future < Item = T , Error = U > + Send + ' static ,
22- T : Send + ' static ,
23- U : Send + ' static ,
20+ where
21+ F : Future < Item = T , Error = U > + Send + ' static ,
22+ T : Send + ' static ,
23+ U : Send + ' static ,
2424{
2525 let mut runtime = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
2626 let result = runtime. block_on ( future) ;
@@ -44,30 +44,35 @@ fn get_url() -> String {
4444 }
4545}
4646
47- pub fn get_all_results < TupleType , T , P > ( result : QueryResult < T , P > ) -> impl MyFuture < Vec < TupleType > >
48- where TupleType : FromRow + Send + ' static ,
49- P : Protocol + Send + ' static ,
50- T : ConnectionLike + Sized + Send + ' static , {
51- result. collect ( )
52- . map ( |( _, data) | data)
47+ pub fn get_all_results < TupleType , T , P > ( result : QueryResult < T , P > ) -> impl MyFuture < Vec < TupleType > >
48+ where
49+ TupleType : FromRow + Send + ' static ,
50+ P : Protocol + Send + ' static ,
51+ T : ConnectionLike + Sized + Send + ' static ,
52+ {
53+ result. collect ( ) . map ( |( _, data) | data)
5354}
5455
55- pub fn get_single_result < TupleType , T , P > ( result : QueryResult < T , P > ) -> impl MyFuture < TupleType >
56- where TupleType : FromRow + Send + ' static ,
57- P : Protocol + Send + ' static ,
58- T : ConnectionLike + Sized + Send + ' static , {
59- get_all_results ( result)
60- . and_then ( |mut data| if data. len ( ) != 1 {
56+ pub fn get_single_result < TupleType , T , P > ( result : QueryResult < T , P > ) -> impl MyFuture < TupleType >
57+ where
58+ TupleType : FromRow + Send + ' static ,
59+ P : Protocol + Send + ' static ,
60+ T : ConnectionLike + Sized + Send + ' static ,
61+ {
62+ get_all_results ( result) . and_then ( |mut data| {
63+ if data. len ( ) != 1 {
6164 Err ( Error :: from ( io:: Error :: from ( io:: ErrorKind :: InvalidData ) ) )
6265 } else {
6366 Ok ( data. remove ( 0 ) )
64- } )
67+ }
68+ } )
6569}
6670
6771#[ test]
6872fn use_generic_code ( ) {
6973 let pool = Pool :: new ( Opts :: from_url ( & * get_url ( ) ) . unwrap ( ) ) ;
70- let fut = pool. get_conn ( )
74+ let fut = pool
75+ . get_conn ( )
7176 . and_then ( move |conn| conn. query ( "SELECT 1, 2, 3" ) )
7277 . and_then ( get_single_result :: < ( u8 , u8 , u8 ) , _ , _ > ) ;
7378
0 commit comments