Debian Codegraph @

src/message/data_row.rs

raw symbols SCIP
1 use byteorderrust-analyzer cargo byteorder 1.5.0 crate/::{BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#, ByteOrderrust-analyzer cargo byteorder 1.5.0 ByteOrder#};
2 use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::bytesrust-analyzer cargo bytes 1.12.1 crate/::Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#;
4
5 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
6 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::messagerust-analyzer cargo sqlx-postgres 0.8.3 message/::{BackendMessagerust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessage#, BackendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessageFormat#};
7
8 /// A row of data from the database.
9 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
10 pub struct DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow# {
11 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) storagerust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#storage.: Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#,
12
13 /// Ranges into the stored row data.
14 /// This uses `u32` instead of usize to reduce the size of this type. Values cannot be larger
15 /// than `i32` in postgres.
16 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) valuesrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#values.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<u32>>>,
17 }
18
19 impl DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow# {
20 #[inline]
21 pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) fn getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(&self, index: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'_ [u8]> {
22 self.valuesrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#values.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().
23 .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()
24 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|col| &self.storagerust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#storage.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().(col.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start. as usize)..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(col.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end. as usize)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)
25 }
26 }
27
28 impl BackendMessagerust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessage# for DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow# {
29 const FORMATrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow][BackendMessage]FORMAT.: BackendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessageFormat# = BackendMessageFormatrust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessageFormat#::DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/BackendMessageFormat#DataRow#;
30
31 fn decode_bodyrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow][BackendMessage]decode_body().(buf: Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow][BackendMessage], Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
32 if buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().2 {
33 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!(
34 "expected at least 2 bytes, got {}",
35 buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().()
36 ));
37 }
38
39 let cnt = BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#::read_u16rust-analyzer cargo byteorder 1.5.0 impl#[BigEndian][ByteOrder]read_u16().(&buf) as usize;
40
41 let mut values = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(cnt);
42 let mut offset: u32 = 2;
43
44 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#cnt {
45 let value_start = offset
46 .checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]checked_add().(4)
47 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!("next value start out of range (offset: {offset})"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
48
49 // widen both to a larger type for a safe comparison
50 if (buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().() as u64) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().(value_start as u64) {
51 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!(
52 "expected 4 bytes at offset {offset}, got {}",
53 (value_start as u64) -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). (buf.lenrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]len().() as u64)
54 ));
55 }
56
57 // Length of the column value, in bytes (this count does not include itself).
58 // Can be zero. As a special case, -1 indicates a NULL column value.
59 // No value bytes follow in the NULL case.
60 //
61 // we know `offset` is within range of `buf.len()` from the above check
62 #[allow(clippy::cast_possible_truncation)]
63 let length = BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#::read_i32rust-analyzer cargo byteorder 1.5.0 ByteOrder#read_i32().(&buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().(offset as usize)..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
64
65 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(length) = u32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u32][`TryFrom<i32>`]try_from().(length) {
66 let value_end = value_start.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]checked_add().(length).ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
67 err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!("value_start + length out of range ({offset} + {length})")
68 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
69
70 values.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#value_end));
71 offset = value_end;
72 } else {
73 // Negative values signify NULL
74 values.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
75 // `value_start` is actually the next value now.
76 offset = value_start;
77 }
78 }
79
80 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow][BackendMessage] {
81 storagerust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#storage.: buf,
82 valuesrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#values.,
83 })
84 }
85 }
86
87 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
88 fn test_decode_data_rowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/test_decode_data_row().() {
89 const DATArust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DATA.: &[u8] = b"\
90 \x00\x08\
91 \xff\xff\xff\xff\
92 \x00\x00\x00\x04\
93 \x00\x00\x00\n\
94 \xff\xff\xff\xff\
95 \x00\x00\x00\x04\
96 \x00\x00\x00\x14\
97 \xff\xff\xff\xff\
98 \x00\x00\x00\x04\
99 \x00\x00\x00(\
100 \xff\xff\xff\xff\
101 \x00\x00\x00\x04\
102 \x00\x00\x00P";
103
104 let row = DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#::decode_bodyrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow][BackendMessage]decode_body().(DATArust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DATA..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
105
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.valuesrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#values..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 8);
107
108 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(0).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
109 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), &[0_u8, 0, 0, 10][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
110 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(2).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
111 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(3).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), &[0_u8, 0, 0, 20][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
112 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(4).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
113 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), &[0_u8, 0, 0, 40][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
114 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(6).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
115 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.getrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/impl#[DataRow]get().(7).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), &[0_u8, 0, 0, 80][rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
116 }
117
118 #[cfg(all(test, not(debug_assertions)))]
119 #[benchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/bench:]
120 fn bench_data_row_get(b: &mut test::Bencher) {
121 const DATA: &[u8] = b"\x00\x08\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00\n\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00\x14\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00(\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00P";
122
123 let row = DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#::decode_body(test::black_box(Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::from_static(DATA))).unwrap();
124
125 b.iter(|| {
126 let _value = test::black_box(&row).get(3);
127 });
128 }
129
130 #[cfg(all(test, not(debug_assertions)))]
131 #[benchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/bench:]
132 fn bench_decode_data_row(b: &mut test::Bencher) {
133 const DATA: &[u8] = b"\x00\x08\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00\n\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00\x14\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00(\xff\xff\xff\xff\x00\x00\x00\x04\x00\x00\x00P";
134
135 b.iter(|| {
136 let _ = DataRowrust-analyzer cargo sqlx-postgres 0.8.3 message/data_row/DataRow#::decode_body(test::black_box(Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::from_static(DATA)));
137 });
138 }