cdbc_sqlite/types/
uint.rs

1use std::convert::TryInto;
2
3use cdbc::decode::Decode;
4use cdbc::encode::{Encode, IsNull};
5use cdbc::error::BoxDynError;
6use crate::type_info::DataType;
7use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
8use cdbc::types::Type;
9
10impl Type<Sqlite> for u8 {
11    fn type_info() -> SqliteTypeInfo {
12        SqliteTypeInfo(DataType::Int)
13    }
14
15    fn compatible(ty: &SqliteTypeInfo) -> bool {
16        matches!(ty.0, DataType::Int | DataType::Int64)
17    }
18}
19
20impl<'q> Encode<'q, Sqlite> for u8 {
21    fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
22        args.push(SqliteArgumentValue::Int(*self as i32));
23
24        IsNull::No
25    }
26}
27
28impl<'r> Decode<'r, Sqlite> for u8 {
29    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
30        Ok(value.int().try_into()?)
31    }
32}
33
34impl Type<Sqlite> for u16 {
35    fn type_info() -> SqliteTypeInfo {
36        SqliteTypeInfo(DataType::Int)
37    }
38
39    fn compatible(ty: &SqliteTypeInfo) -> bool {
40        matches!(ty.0, DataType::Int | DataType::Int64)
41    }
42}
43
44impl<'q> Encode<'q, Sqlite> for u16 {
45    fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
46        args.push(SqliteArgumentValue::Int(*self as i32));
47
48        IsNull::No
49    }
50}
51
52impl<'r> Decode<'r, Sqlite> for u16 {
53    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
54        Ok(value.int().try_into()?)
55    }
56}
57
58impl Type<Sqlite> for u32 {
59    fn type_info() -> SqliteTypeInfo {
60        SqliteTypeInfo(DataType::Int64)
61    }
62
63    fn compatible(ty: &SqliteTypeInfo) -> bool {
64        matches!(ty.0, DataType::Int | DataType::Int64)
65    }
66}
67
68impl<'q> Encode<'q, Sqlite> for u32 {
69    fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
70        args.push(SqliteArgumentValue::Int64(*self as i64));
71
72        IsNull::No
73    }
74}
75
76impl<'r> Decode<'r, Sqlite> for u32 {
77    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
78        Ok(value.int64().try_into()?)
79    }
80}