Debian Codegraph @

src/types/float.rs

raw symbols SCIP
1 use byteorderrust-analyzer cargo byteorder 1.5.0 crate/::{ByteOrderrust-analyzer cargo byteorder 1.5.0 ByteOrder#, LittleEndianrust-analyzer cargo byteorder 1.5.0 LittleEndian#};
2
3 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::decoderust-analyzer cargo sqlx-core 0.8.3 decode/::Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#;
4 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::encoderust-analyzer cargo sqlx-core 0.8.3 encode/::{Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#, IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#};
5 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#;
6 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::textrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/::ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#;
7 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::typesrust-analyzer cargo sqlx-mysql 0.8.3 types/::Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#;
8 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::{MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#, MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#, MySqlValueFormatrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#, MySqlValueRefrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueRef#};
9
10 fn real_compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/float/real_compatible().(ty: &MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#) -> bool {
11 // NOTE: `DECIMAL` is explicitly excluded because floating-point numbers have different semantics.
12 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(ty.r#typerust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#`r#type`., ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Floatrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#Float# | ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Doublerust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#Double#)
13 }
14
15 impl Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f32 {
16 fn type_inforust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f32][`Type<MySql>`]type_info().() -> MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo# {
17 MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#::binaryrust-analyzer cargo sqlx-mysql 0.8.3 type_info/impl#[MySqlTypeInfo]binary().(ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Floatrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#Float#)
18 }
19
20 fn compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f32][`Type<MySql>`]compatible().(ty: &MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#) -> bool {
21 real_compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/float/real_compatible().(ty)
22 }
23 }
24
25 impl Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f64 {
26 fn type_inforust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f64][`Type<MySql>`]type_info().() -> MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo# {
27 MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#::binaryrust-analyzer cargo sqlx-mysql 0.8.3 type_info/impl#[MySqlTypeInfo]binary().(ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Doublerust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#Double#)
28 }
29
30 fn compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f64][`Type<MySql>`]compatible().(ty: &MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#) -> bool {
31 real_compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/float/real_compatible().(ty)
32 }
33 }
34
35 impl Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'_, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f32 {
36 fn encode_by_refrust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f32][`Encode<'_, MySql>`]encode_by_ref().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
37 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&self.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_le_bytes().());
38
39 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#)
40 }
41 }
42
43 impl Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'_, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f64 {
44 fn encode_by_refrust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f64][`Encode<'_, MySql>`]encode_by_ref().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
45 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&self.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]to_le_bytes().());
46
47 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#)
48 }
49 }
50
51 impl Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'_, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f32 {
52 fn decoderust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f32][`Decode<'_, MySql>`]decode().(value: MySqlValueRefrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueRef#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f32][`Decode<'_, MySql>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
53 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(match value.formatrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]format().() {
54 MySqlValueFormatrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#::Binaryrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#Binary# => {
55 let buf = value.as_bytesrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]as_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
56
57 match buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
58 // These functions panic if `buf` is not exactly the right size.
59 4 => LittleEndianrust-analyzer cargo byteorder 1.5.0 LittleEndian#::read_f32rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_f32().(buf),
60 // MySQL can return 8-byte DOUBLE values for a FLOAT
61 // We take and truncate to f32 as that's the same behavior as *in* MySQL,
62 #[allow(clippy::cast_possible_truncation)]
63 8 => LittleEndianrust-analyzer cargo byteorder 1.5.0 LittleEndian#::read_f64rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_f64().(buf) as f32,
64 other => {
65 // Users may try to decode a DECIMAL as floating point;
66 // inform them why that's a bad idea.
67 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
68 "expected a FLOAT as 4 or 8 bytes, got {other} bytes; \
69 note that decoding DECIMAL as `f32` is not supported \
70 due to differing semantics"
71 )
72 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
73 }
74 }
75 }
76
77 MySqlValueFormatrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#::Textrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#Text# => value.as_strrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]as_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
78 })
79 }
80 }
81
82 impl Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'_, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for f64 {
83 fn decoderust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f64][`Decode<'_, MySql>`]decode().(value: MySqlValueRefrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueRef#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 types/float/impl#[f64][`Decode<'_, MySql>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
84 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(match value.formatrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]format().() {
85 MySqlValueFormatrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#::Binaryrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#Binary# => {
86 let buf = value.as_bytesrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]as_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
87
88 // The `read_*` functions panic if `buf` is not exactly the right size.
89 match buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
90 // Allow implicit widening here
91 4 => LittleEndianrust-analyzer cargo byteorder 1.5.0 LittleEndian#::read_f32rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_f32().(buf) as f64,
92 8 => LittleEndianrust-analyzer cargo byteorder 1.5.0 LittleEndian#::read_f64rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_f64().(buf),
93 other => {
94 // Users may try to decode a DECIMAL as floating point;
95 // inform them why that's a bad idea.
96 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
97 "expected a DOUBLE as 4 or 8 bytes, got {other} bytes; \
98 note that decoding DECIMAL as `f64` is not supported \
99 due to differing semantics"
100 )
101 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
102 }
103 }
104 }
105 MySqlValueFormatrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#::Textrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueFormat#Text# => value.as_strrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]as_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
106 })
107 }
108 }