@@ -130,85 +130,82 @@ impl Decode<'_, Postgres> for i64 {
130130
131131impl Type < Postgres > for u16 {
132132 fn type_info ( ) -> PgTypeInfo {
133- PgTypeInfo :: INT4
133+ < i32 as Type < Postgres > > :: type_info ( )
134134 }
135135}
136136
137137impl PgHasArrayType for u16 {
138138 fn array_type_info ( ) -> PgTypeInfo {
139- PgTypeInfo :: INT4_ARRAY
139+ < i32 as PgHasArrayType > :: array_type_info ( )
140140 }
141141}
142142
143143impl Encode < ' _ , Postgres > for u16 {
144144 fn encode_by_ref ( & self , buf : & mut PgArgumentBuffer ) -> IsNull {
145- buf . extend ( & i32:: from ( * self ) . to_be_bytes ( ) ) ;
146- IsNull :: No
145+ let v = i32:: from ( * self ) ;
146+ < i32 as Encode < ' _ , Postgres > > :: encode_by_ref ( & v , buf )
147147 }
148148}
149149
150150impl Decode < ' _ , Postgres > for u16 {
151151 fn decode ( value : PgValueRef < ' _ > ) -> Result < Self , BoxDynError > {
152- let decoded = match value. format ( ) {
153- PgValueFormat :: Binary => BigEndian :: read_i32 ( value. as_bytes ( ) ?) ,
154- PgValueFormat :: Text => value. as_str ( ) ?. parse :: < i32 > ( ) ?,
155- } ;
156- Ok ( u16:: try_from ( decoded) ?)
152+ let v = <i32 as Decode < ' _ , Postgres > >:: decode ( value) ?;
153+ Ok ( u16:: try_from ( v) ?)
157154 }
158155}
159156
160157impl Type < Postgres > for u32 {
161158 fn type_info ( ) -> PgTypeInfo {
162- PgTypeInfo :: INT8
159+ < i64 as Type < Postgres > > :: type_info ( )
163160 }
164161}
165162
166163impl PgHasArrayType for u32 {
167164 fn array_type_info ( ) -> PgTypeInfo {
168- PgTypeInfo :: INT8_ARRAY
165+ < i64 as PgHasArrayType > :: array_type_info ( )
169166 }
170167}
171168
172169impl Encode < ' _ , Postgres > for u32 {
173170 fn encode_by_ref ( & self , buf : & mut PgArgumentBuffer ) -> IsNull {
174- buf . extend ( & i64:: from ( * self ) . to_be_bytes ( ) ) ;
175- IsNull :: No
171+ let v = i64:: from ( * self ) ;
172+ < i64 as Encode < ' _ , Postgres > > :: encode_by_ref ( & v , buf )
176173 }
177174}
178175
179176impl Decode < ' _ , Postgres > for u32 {
180177 fn decode ( value : PgValueRef < ' _ > ) -> Result < Self , BoxDynError > {
181- let decoded = match value. format ( ) {
182- PgValueFormat :: Binary => BigEndian :: read_i64 ( value. as_bytes ( ) ?) ,
183- PgValueFormat :: Text => value. as_str ( ) ?. parse :: < i64 > ( ) ?,
184- } ;
185- Ok ( u32:: try_from ( decoded) ?)
178+ let v = <i64 as Decode < ' _ , Postgres > >:: decode ( value) ?;
179+ Ok ( u32:: try_from ( v) ?)
186180 }
187181}
188182
189183impl Type < Postgres > for u64 {
190184 fn type_info ( ) -> PgTypeInfo {
191- PgTypeInfo :: NUMERIC
185+ < i64 as Type < Postgres > > :: type_info ( )
192186 }
193187}
194188
195189impl PgHasArrayType for u64 {
196190 fn array_type_info ( ) -> PgTypeInfo {
197- PgTypeInfo :: NUMERIC_ARRAY
191+ < i64 as PgHasArrayType > :: array_type_info ( )
198192 }
199193}
200194
201195impl Encode < ' _ , Postgres > for u64 {
202196 fn encode_by_ref ( & self , buf : & mut PgArgumentBuffer ) -> IsNull {
203- let numeric_str = self . to_string ( ) ;
204- buf. extend ( numeric_str. as_bytes ( ) ) ;
205- IsNull :: No
197+ let v = i64:: try_from ( * self ) . unwrap_or_else ( |_| {
198+ let v = i64:: MAX ;
199+ log:: warn!( "cannot encode {self} as a signed postgres bigint, encoding as {v} instead" ) ;
200+ v
201+ } ) ;
202+ <i64 as Encode < ' _ , Postgres > >:: encode_by_ref ( & v, buf)
206203 }
207204}
208205
209206impl Decode < ' _ , Postgres > for u64 {
210207 fn decode ( value : PgValueRef < ' _ > ) -> Result < Self , BoxDynError > {
211- let decoded = value . as_str ( ) ? . parse :: < u64 > ( ) ?;
212- Ok ( decoded )
208+ let v = < i64 as Decode < ' _ , Postgres > > :: decode ( value ) ?;
209+ Ok ( u64 :: try_from ( v ) ? )
213210 }
214211}
0 commit comments