1
1
use 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} ;
4
4
5
- use { Range , RangeBound , BoundType , BoundSided , Normalizable } ;
5
+ use { BoundSided , BoundType , Normalizable , Range , RangeBound } ;
6
6
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 > > {
9
12
let element_type = match ty. kind ( ) {
10
13
& Kind :: Range ( ref ty) => ty,
11
- _ => panic ! ( "unexpected type {:?}" , ty)
14
+ _ => panic ! ( "unexpected type {:?}" , ty) ,
12
15
} ;
13
16
14
- match try! ( types:: range_from_sql ( raw) ) {
17
+ match types:: range_from_sql ( raw) ? {
15
18
types:: Range :: Empty => Ok ( Range :: empty ( ) ) ,
16
19
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) ? ;
19
22
Ok ( Range :: new ( lower, upper) )
20
23
}
21
24
}
@@ -29,42 +32,48 @@ impl<T> FromSql for Range<T> where T: PartialOrd+Normalizable+FromSql {
29
32
}
30
33
}
31
34
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 ,
35
39
{
36
40
match bound {
37
41
types:: RangeBound :: Exclusive ( value) => {
38
42
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) ? ,
41
45
} ;
42
46
Ok ( Some ( RangeBound :: new ( value, BoundType :: Exclusive ) ) )
43
- } ,
47
+ }
44
48
types:: RangeBound :: Inclusive ( value) => {
45
49
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) ? ,
48
52
} ;
49
53
Ok ( Some ( RangeBound :: new ( value, BoundType :: Inclusive ) ) )
50
- } ,
54
+ }
51
55
types:: RangeBound :: Unbounded => Ok ( None ) ,
52
56
}
53
57
}
54
58
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 > > {
57
64
let element_type = match ty. kind ( ) {
58
65
& Kind :: Range ( ref ty) => ty,
59
- _ => panic ! ( "unexpected type {:?}" , ty)
66
+ _ => panic ! ( "unexpected type {:?}" , ty) ,
60
67
} ;
61
68
62
69
if self . is_empty ( ) {
63
70
types:: empty_range_to_sql ( buf) ;
64
71
} 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
+ ) ?;
68
77
}
69
78
70
79
Ok ( IsNull :: No )
@@ -80,15 +89,16 @@ impl<T> ToSql for Range<T> where T: PartialOrd+Normalizable+ToSql {
80
89
to_sql_checked ! ( ) ;
81
90
}
82
91
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 ,
86
96
{
87
97
match bound {
88
98
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 ,
92
102
} ;
93
103
94
104
match bound. type_ {
@@ -98,7 +108,6 @@ fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, info: &Sessio
98
108
}
99
109
None => Ok ( types:: RangeBound :: Unbounded ) ,
100
110
}
101
-
102
111
}
103
112
104
113
#[ cfg( test) ]
@@ -130,10 +139,11 @@ mod test {
130
139
} )
131
140
}
132
141
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 ) ] ) {
134
143
let conn = Connection :: connect ( "postgres://postgres@localhost" , TlsMode :: None ) . unwrap ( ) ;
135
144
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 ( ) ;
137
147
let result = stmt. query ( & [ ] ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
138
148
assert ! ( val == & result) ;
139
149
0 commit comments