11use std:: error:: Error ;
2- use postgres_shared:: types:: { FromSql , IsNull , Kind , ToSql , Type } ;
2+ use postgres_types:: { FromSql , IsNull , Kind , ToSql , Type } ;
3+ use postgres_types:: private:: BytesMut ;
34use postgres_protocol:: { self as protocol, types} ;
45
5- use { BoundSided , BoundType , Normalizable , Range , RangeBound } ;
6+ use crate :: { BoundSided , BoundType , Normalizable , Range , RangeBound } ;
67
7- impl < T > FromSql for Range < T >
8+ impl < ' a , T > FromSql < ' a > for Range < T >
89where
9- T : PartialOrd + Normalizable + FromSql ,
10+ T : PartialOrd + Normalizable + FromSql < ' a > ,
1011{
11- fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Range < T > , Box < Error + Sync + Send > > {
12- let element_type = match ty. kind ( ) {
13- & Kind :: Range ( ref ty) => ty,
12+ fn from_sql ( ty : & Type , raw : & ' a [ u8 ] ) -> Result < Range < T > , Box < dyn Error + Sync + Send > > {
13+ let element_type = match * ty. kind ( ) {
14+ Kind :: Range ( ref ty) => ty,
1415 _ => panic ! ( "unexpected type {:?}" , ty) ,
1516 } ;
1617
@@ -25,16 +26,16 @@ where
2526 }
2627
2728 fn accepts ( ty : & Type ) -> bool {
28- match ty. kind ( ) {
29- & Kind :: Range ( ref inner) => <T as FromSql >:: accepts ( inner) ,
29+ match * ty. kind ( ) {
30+ Kind :: Range ( ref inner) => <T as FromSql >:: accepts ( inner) ,
3031 _ => false ,
3132 }
3233 }
3334}
3435
35- fn bound_from_sql < T , S > ( bound : types:: RangeBound < Option < & [ u8 ] > > , ty : & Type ) -> Result < Option < RangeBound < S , T > > , Box < Error + Sync + Send > >
36+ fn bound_from_sql < ' a , T , S > ( bound : types:: RangeBound < Option < & ' a [ u8 ] > > , ty : & Type ) -> Result < Option < RangeBound < S , T > > , Box < dyn Error + Sync + Send > >
3637where
37- T : PartialOrd + Normalizable + FromSql ,
38+ T : PartialOrd + Normalizable + FromSql < ' a > ,
3839 S : BoundSided ,
3940{
4041 match bound {
@@ -60,9 +61,9 @@ impl<T> ToSql for Range<T>
6061where
6162 T : PartialOrd + Normalizable + ToSql ,
6263{
63- fn to_sql ( & self , ty : & Type , buf : & mut Vec < u8 > ) -> Result < IsNull , Box < Error + Sync + Send > > {
64- let element_type = match ty. kind ( ) {
65- & Kind :: Range ( ref ty) => ty,
64+ fn to_sql ( & self , ty : & Type , buf : & mut BytesMut ) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
65+ let element_type = match * ty. kind ( ) {
66+ Kind :: Range ( ref ty) => ty,
6667 _ => panic ! ( "unexpected type {:?}" , ty) ,
6768 } ;
6869
@@ -80,16 +81,16 @@ where
8081 }
8182
8283 fn accepts ( ty : & Type ) -> bool {
83- match ty. kind ( ) {
84- & Kind :: Range ( ref inner) => <T as ToSql >:: accepts ( inner) ,
84+ match * ty. kind ( ) {
85+ Kind :: Range ( ref inner) => <T as ToSql >:: accepts ( inner) ,
8586 _ => false ,
8687 }
8788 }
8889
8990 to_sql_checked ! ( ) ;
9091}
9192
92- fn bound_to_sql < S , T > ( bound : Option < & RangeBound < S , T > > , ty : & Type , buf : & mut Vec < u8 > ) -> Result < types:: RangeBound < protocol:: IsNull > , Box < Error + Sync + Send > >
93+ fn bound_to_sql < S , T > ( bound : Option < & RangeBound < S , T > > , ty : & Type , buf : & mut BytesMut ) -> Result < types:: RangeBound < protocol:: IsNull > , Box < dyn Error + Sync + Send > >
9394where
9495 S : BoundSided ,
9596 T : ToSql ,
@@ -114,10 +115,10 @@ where
114115mod test {
115116 use std:: fmt;
116117
117- use postgres:: { Connection , TlsMode } ;
118+ use postgres:: { Client , NoTls } ;
118119 use postgres:: types:: { FromSql , ToSql } ;
119- #[ cfg( feature = "with-time " ) ]
120- use time :: { self , Timespec } ;
120+ #[ cfg( feature = "with-chrono-0_4 " ) ]
121+ use chrono_04 :: { TimeZone , Utc , Duration } ;
121122
122123 macro_rules! test_range {
123124 ( $name: expr, $t: ty, $low: expr, $low_str: expr, $high: expr, $high_str: expr) => ( {
@@ -140,16 +141,21 @@ mod test {
140141 } )
141142 }
142143
143- fn test_type < T : PartialEq + FromSql + ToSql , S : fmt:: Display > ( sql_type : & str , checks : & [ ( T , S ) ] ) {
144- let conn = Connection :: connect ( "postgres://postgres@localhost" , TlsMode :: None ) . unwrap ( ) ;
144+
145+ fn test_type < T , S > ( sql_type : & str , checks : & [ ( T , S ) ] )
146+ where for < ' a >
147+ T : Sync + PartialEq + FromSql < ' a > + ToSql ,
148+ S : fmt:: Display
149+ {
150+ let mut conn = Client :: connect ( "postgres://postgres@localhost" , NoTls ) . unwrap ( ) ;
145151 for & ( ref val, ref repr) in checks {
146152 let stmt = conn. prepare ( & * format ! ( "SELECT {}::{}" , * repr, sql_type) )
147153 . unwrap ( ) ;
148- let result = stmt . query ( & [ ] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
154+ let result = conn . query ( & stmt , & [ ] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
149155 assert ! ( val == & result) ;
150156
151157 let stmt = conn. prepare ( & * format ! ( "SELECT $1::{}" , sql_type) ) . unwrap ( ) ;
152- let result = stmt . query ( & [ val] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
158+ let result = conn . query ( & stmt , & [ val] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
153159 assert ! ( val == & result) ;
154160 }
155161 }
@@ -164,25 +170,19 @@ mod test {
164170 test_range ! ( "INT8RANGE" , i64 , 100i64 , "100" , 200i64 , "200" )
165171 }
166172
167- #[ cfg( feature = "with-time" ) ]
168- fn test_timespec_range_params ( sql_type : & str ) {
169- fn t ( time : & str ) -> Timespec {
170- time:: strptime ( time, "%Y-%m-%d" ) . unwrap ( ) . to_timespec ( )
171- }
172- let low = "1970-01-01" ;
173- let high = "1980-01-01" ;
174- test_range ! ( sql_type, Timespec , t( low) , low, t( high) , high) ;
175- }
176-
177173 #[ test]
178- #[ cfg( feature = "with-time " ) ]
174+ #[ cfg( feature = "with-chrono-0_4 " ) ]
179175 fn test_tsrange_params ( ) {
180- test_timespec_range_params ( "TSRANGE" ) ;
176+ let low = Utc . timestamp ( 0 , 0 ) ;
177+ let high = low + Duration :: days ( 10 ) ;
178+ test_range ! ( "TSRANGE" , NaiveDateTime , low. naive_utc( ) , "1970-01-01" , high. naive_utc( ) , "1970-01-11" ) ;
181179 }
182180
183181 #[ test]
184- #[ cfg( feature = "with-time " ) ]
182+ #[ cfg( feature = "with-chrono-0_4 " ) ]
185183 fn test_tstzrange_params ( ) {
186- test_timespec_range_params ( "TSTZRANGE" ) ;
184+ let low = Utc . timestamp ( 0 , 0 ) ;
185+ let high = low + Duration :: days ( 10 ) ;
186+ test_range ! ( "TSTZRANGE" , DateTime <Utc >, low, "1970-01-01" , high, "1970-01-11" ) ;
187187 }
188188}
0 commit comments