11use std:: error:: Error ;
2- use postgres:: types:: { Type , Kind , ToSql , FromSql , IsNull , SessionInfo } ;
3- use postgres_protocol:: types;
2+ use postgres:: types:: { FromSql , IsNull , Kind , ToSql , Type } ;
3+ use postgres_protocol:: { self as protocol , types} ;
44
5- use { Range , RangeBound , BoundType , BoundSided , Normalizable } ;
5+ use { BoundSided , BoundType , Normalizable , Range , RangeBound } ;
66
7- impl < T > FromSql for Range < T > where T : PartialOrd +Normalizable +FromSql {
8- fn from_sql ( ty : & Type , raw : & [ u8 ] , info : & SessionInfo ) -> Result < Range < T > , Box < Error + Sync + Send > > {
7+ impl < T > FromSql for Range < T >
8+ where
9+ T : PartialOrd + Normalizable + FromSql ,
10+ {
11+ fn from_sql ( ty : & Type , raw : & [ u8 ] ) -> Result < Range < T > , Box < Error + Sync + Send > > {
912 let element_type = match ty. kind ( ) {
1013 & Kind :: Range ( ref ty) => ty,
11- _ => panic ! ( "unexpected type {:?}" , ty)
14+ _ => panic ! ( "unexpected type {:?}" , ty) ,
1215 } ;
1316
14- match try! ( types:: range_from_sql ( raw) ) {
17+ match types:: range_from_sql ( raw) ? {
1518 types:: Range :: Empty => Ok ( Range :: empty ( ) ) ,
1619 types:: Range :: Nonempty ( lower, upper) => {
17- let lower = try! ( bound_from_sql ( lower, element_type, info ) ) ;
18- let upper = try! ( bound_from_sql ( upper, element_type, info ) ) ;
20+ let lower = bound_from_sql ( lower, element_type) ? ;
21+ let upper = bound_from_sql ( upper, element_type) ? ;
1922 Ok ( Range :: new ( lower, upper) )
2023 }
2124 }
@@ -29,42 +32,48 @@ impl<T> FromSql for Range<T> where T: PartialOrd+Normalizable+FromSql {
2932 }
3033}
3134
32- fn bound_from_sql < T , S > ( bound : types:: RangeBound < Option < & [ u8 ] > > , ty : & Type , info : & SessionInfo ) -> Result < Option < RangeBound < S , T > > , Box < Error + Sync + Send > >
33- where T : PartialOrd + Normalizable + FromSql ,
34- S : BoundSided
35+ fn bound_from_sql < T , S > ( bound : types:: RangeBound < Option < & [ u8 ] > > , ty : & Type ) -> Result < Option < RangeBound < S , T > > , Box < Error + Sync + Send > >
36+ where
37+ T : PartialOrd + Normalizable + FromSql ,
38+ S : BoundSided ,
3539{
3640 match bound {
3741 types:: RangeBound :: Exclusive ( value) => {
3842 let value = match value {
39- Some ( value) => try! ( T :: from_sql ( ty, value, info ) ) ,
40- None => try! ( T :: from_sql_null ( ty, info ) ) ,
43+ Some ( value) => T :: from_sql ( ty, value) ? ,
44+ None => T :: from_sql_null ( ty) ? ,
4145 } ;
4246 Ok ( Some ( RangeBound :: new ( value, BoundType :: Exclusive ) ) )
43- } ,
47+ }
4448 types:: RangeBound :: Inclusive ( value) => {
4549 let value = match value {
46- Some ( value) => try! ( T :: from_sql ( ty, value, info ) ) ,
47- None => try! ( T :: from_sql_null ( ty, info ) ) ,
50+ Some ( value) => T :: from_sql ( ty, value) ? ,
51+ None => T :: from_sql_null ( ty) ? ,
4852 } ;
4953 Ok ( Some ( RangeBound :: new ( value, BoundType :: Inclusive ) ) )
50- } ,
54+ }
5155 types:: RangeBound :: Unbounded => Ok ( None ) ,
5256 }
5357}
5458
55- impl < T > ToSql for Range < T > where T : PartialOrd +Normalizable +ToSql {
56- fn to_sql ( & self , ty : & Type , mut buf : & mut Vec < u8 > , info : & SessionInfo ) -> Result < IsNull , Box < Error + Sync + Send > > {
59+ impl < T > ToSql for Range < T >
60+ where
61+ T : PartialOrd + Normalizable + ToSql ,
62+ {
63+ fn to_sql ( & self , ty : & Type , buf : & mut Vec < u8 > ) -> Result < IsNull , Box < Error + Sync + Send > > {
5764 let element_type = match ty. kind ( ) {
5865 & Kind :: Range ( ref ty) => ty,
59- _ => panic ! ( "unexpected type {:?}" , ty)
66+ _ => panic ! ( "unexpected type {:?}" , ty) ,
6067 } ;
6168
6269 if self . is_empty ( ) {
6370 types:: empty_range_to_sql ( buf) ;
6471 } else {
65- try!( types:: range_to_sql ( |buf| bound_to_sql ( self . lower ( ) , element_type, info, buf) ,
66- |buf| bound_to_sql ( self . upper ( ) , element_type, info, buf) ,
67- buf) ) ;
72+ types:: range_to_sql (
73+ |buf| bound_to_sql ( self . lower ( ) , element_type, buf) ,
74+ |buf| bound_to_sql ( self . upper ( ) , element_type, buf) ,
75+ buf,
76+ ) ?;
6877 }
6978
7079 Ok ( IsNull :: No )
@@ -80,15 +89,16 @@ impl<T> ToSql for Range<T> where T: PartialOrd+Normalizable+ToSql {
8089 to_sql_checked ! ( ) ;
8190}
8291
83- fn bound_to_sql < S , T > ( bound : Option < & RangeBound < S , T > > , ty : & Type , info : & SessionInfo , buf : & mut Vec < u8 > ) -> Result < types:: RangeBound < types:: IsNull > , Box < Error + Sync + Send > >
84- where S : BoundSided ,
85- T : ToSql
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+ where
94+ S : BoundSided ,
95+ T : ToSql ,
8696{
8797 match bound {
8898 Some ( bound) => {
89- let null = match try! ( bound. value . to_sql ( ty, buf, info ) ) {
90- IsNull :: Yes => types :: IsNull :: Yes ,
91- IsNull :: No => types :: IsNull :: No ,
99+ let null = match bound. value . to_sql ( ty, buf) ? {
100+ IsNull :: Yes => protocol :: IsNull :: Yes ,
101+ IsNull :: No => protocol :: IsNull :: No ,
92102 } ;
93103
94104 match bound. type_ {
@@ -98,7 +108,6 @@ fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, info: &Sessio
98108 }
99109 None => Ok ( types:: RangeBound :: Unbounded ) ,
100110 }
101-
102111}
103112
104113#[ cfg( test) ]
@@ -130,10 +139,11 @@ mod test {
130139 } )
131140 }
132141
133- fn test_type < T : PartialEq + FromSql + ToSql , S : fmt:: Display > ( sql_type : & str , checks : & [ ( T , S ) ] ) {
142+ fn test_type < T : PartialEq + FromSql + ToSql , S : fmt:: Display > ( sql_type : & str , checks : & [ ( T , S ) ] ) {
134143 let conn = Connection :: connect ( "postgres://postgres@localhost" , TlsMode :: None ) . unwrap ( ) ;
135144 for & ( ref val, ref repr) in checks {
136- let stmt = conn. prepare ( & * format ! ( "SELECT {}::{}" , * repr, sql_type) ) . unwrap ( ) ;
145+ let stmt = conn. prepare ( & * format ! ( "SELECT {}::{}" , * repr, sql_type) )
146+ . unwrap ( ) ;
137147 let result = stmt. query ( & [ ] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
138148 assert ! ( val == & result) ;
139149
0 commit comments