forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.rs
More file actions
54 lines (46 loc) 路 1.32 KB
/
Copy pathfloat.rs
File metadata and controls
54 lines (46 loc) 路 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::type_info::DataType;
use crate::types::Type;
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
impl Type<Sqlite> for f32 {
fn type_info() -> SqliteTypeInfo {
SqliteTypeInfo(DataType::Float)
}
}
impl<'q> Encode<'q, Sqlite> for f32 {
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Double((*self).into()));
Ok(IsNull::No)
}
}
impl<'r> Decode<'r, Sqlite> for f32 {
fn decode(value: SqliteValueRef<'r>) -> Result<f32, BoxDynError> {
// Truncation is intentional
#[allow(clippy::cast_possible_truncation)]
Ok(value.double() as f32)
}
}
impl Type<Sqlite> for f64 {
fn type_info() -> SqliteTypeInfo {
SqliteTypeInfo(DataType::Float)
}
}
impl<'q> Encode<'q, Sqlite> for f64 {
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Double(*self));
Ok(IsNull::No)
}
}
impl<'r> Decode<'r, Sqlite> for f64 {
fn decode(value: SqliteValueRef<'r>) -> Result<f64, BoxDynError> {
Ok(value.double())
}
}