sqlx_core/any/types/
float.rs

1use crate::any::{Any, AnyArgumentBuffer, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind, AnyValueRef};
2use crate::database::Database;
3use crate::decode::Decode;
4use crate::encode::{Encode, IsNull};
5use crate::error::BoxDynError;
6use crate::types::Type;
7
8impl Type<Any> for f32 {
9    fn type_info() -> AnyTypeInfo {
10        AnyTypeInfo {
11            kind: AnyTypeInfoKind::Real,
12        }
13    }
14}
15
16impl<'q> Encode<'q, Any> for f32 {
17    fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
18        buf.0.push(AnyValueKind::Real(*self));
19        Ok(IsNull::No)
20    }
21}
22
23impl<'r> Decode<'r, Any> for f32 {
24    fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError> {
25        match value.kind {
26            AnyValueKind::Real(r) => Ok(r),
27            other => other.unexpected(),
28        }
29    }
30}
31
32impl Type<Any> for f64 {
33    fn type_info() -> AnyTypeInfo {
34        AnyTypeInfo {
35            kind: AnyTypeInfoKind::Double,
36        }
37    }
38}
39
40impl<'q> Encode<'q, Any> for f64 {
41    fn encode_by_ref(
42        &self,
43        buf: &mut <Any as Database>::ArgumentBuffer<'q>,
44    ) -> Result<IsNull, BoxDynError> {
45        buf.0.push(AnyValueKind::Double(*self));
46        Ok(IsNull::No)
47    }
48}
49
50impl<'r> Decode<'r, Any> for f64 {
51    fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
52        match value.kind {
53            // Widening is safe
54            AnyValueKind::Real(r) => Ok(r as f64),
55            AnyValueKind::Double(d) => Ok(d),
56            other => other.unexpected(),
57        }
58    }
59}