1use byteorder::{BigEndian, ByteOrder};
2
3use cdbc::decode::Decode;
4use cdbc::encode::{Encode, IsNull};
5use cdbc::error::BoxDynError;
6use crate::{
7 PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres,
8};
9use cdbc::types::Type;
10
11impl Type<Postgres> for f32 {
12 fn type_info() -> PgTypeInfo {
13 PgTypeInfo::FLOAT4
14 }
15}
16
17impl PgHasArrayType for f32 {
18 fn array_type_info() -> PgTypeInfo {
19 PgTypeInfo::FLOAT4_ARRAY
20 }
21}
22
23impl Encode<'_, Postgres> for f32 {
24 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
25 buf.extend(&self.to_be_bytes());
26
27 IsNull::No
28 }
29}
30
31impl Decode<'_, Postgres> for f32 {
32 fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
33 Ok(match value.format() {
34 PgValueFormat::Binary => BigEndian::read_f32(value.as_bytes()?),
35 PgValueFormat::Text => value.as_str()?.parse()?,
36 })
37 }
38}
39
40impl Type<Postgres> for f64 {
41 fn type_info() -> PgTypeInfo {
42 PgTypeInfo::FLOAT8
43 }
44}
45
46impl PgHasArrayType for f64 {
47 fn array_type_info() -> PgTypeInfo {
48 PgTypeInfo::FLOAT8_ARRAY
49 }
50}
51
52impl Encode<'_, Postgres> for f64 {
53 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
54 buf.extend(&self.to_be_bytes());
55
56 IsNull::No
57 }
58}
59
60impl Decode<'_, Postgres> for f64 {
61 fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
62 Ok(match value.format() {
63 PgValueFormat::Binary => BigEndian::read_f64(value.as_bytes()?),
64 PgValueFormat::Text => value.as_str()?.parse()?,
65 })
66 }
67}