Debian Codegraph @

src/protocol/packet.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().;
2 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/::{Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#};
3
4 use bytesrust-analyzer cargo bytes 1.12.1 crate/::Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#;
5
6 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
7 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::iorust-analyzer cargo sqlx-mysql 0.8.3 io/::{ProtocolDecoderust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#, ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#};
8 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::responserust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/::{EofPacketrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/eof/EofPacket#, OkPacketrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/ok/OkPacket#};
9 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#;
10
11 #[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:)]
12 pub struct Packetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#<T>(pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) T);
13
14 impl<'en, 'stream, T> ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#<'stream, (Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#, &'stream mut u8)> for Packetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#<T>
15 where
16 T: ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#<'en, Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#>,
17 {
18 fn encode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<T>`][`ProtocolEncode<'stream, (Capabilities, &'stream mut u8)>`]encode_with().(
19 &self,
20 buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
21 (capabilities, sequence_id): (Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#, &'stream mut u8),
22 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
23 let mut next_header = |len: u32| {
24 let mut buf = len.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_le_bytes().();
25 buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().sequence_id;
26 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().sequence_id = sequence_id.wrapping_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]wrapping_add().(1);
27
28 buf
29 };
30
31 // reserve space to write the prefixed length
32 let offset = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
33 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&[0_u8; 4]);
34
35 // encode the payload
36 self.0rust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#0..encode_withrust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#encode_with().(buf, capabilities)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
37
38 // determine the length of the encoded payload
39 // and write to our reserved space
40 let len = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().4;
41 let header = &mut buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().offset..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/IndexMut#index_mut().;
42
43 // // `min(.., 0xFF_FF_FF)` cannot overflow
44 #[allow(clippy::cast_possible_truncation)]
45 header[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&next_header(minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(len, 0xFF_FF_FF) as u32));
46
47 // add more packets if we need to split the data
48 if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().0xFF_FF_FF {
49 let rest = buf.split_offrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]split_off().(offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().4 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().0xFF_FF_FF);
50 let mut chunks = rest.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(0xFF_FF_FF);
51
52 for chunk in chunks.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().() {
53 buf.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(chunk.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().4);
54
55 // `chunk.len() == 0xFF_FF_FF`
56 #[allow(clippy::cast_possible_truncation)]
57 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&next_header(chunk.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as u32));
58 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(chunk);
59 }
60
61 // this will also handle adding a zero sized packet if the data size is a multiple of 0xFF_FF_FF
62 let remainder = chunks.remainderrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`ChunksExact<'a, T>`]remainder().();
63 buf.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(remainder.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().4);
64
65 // `remainder.len() < 0xFF_FF_FF`
66 #[allow(clippy::cast_possible_truncation)]
67 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&next_header(remainder.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as u32));
68 buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(remainder);
69 }
70
71 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
72 }
73 }
74
75 impl Packetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#> {
76 pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) fn decoderust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]decode().<'de, T>(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>
77 where
78 T: ProtocolDecoderust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#<'de, ()>,
79 {
80 self.decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]decode_with().(())
81 }
82
83 pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) fn decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]decode_with().<'de, T, C>(self, context: C) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>
84 where
85 T: ProtocolDecoderust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#<'de, C>,
86 {
87 T::decode_withrust-analyzer cargo sqlx-core 0.8.3 io/decode/ProtocolDecode#decode_with().(self.0rust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#0., context)
88 }
89
90 pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) fn okrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]ok().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<OkPacketrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/ok/OkPacket#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
91 self.decoderust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]decode().()
92 }
93
94 pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) fn eofrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]eof().(self, capabilities: Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EofPacketrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/eof/EofPacket#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
95 if capabilities.containsrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/impl#[Capabilities]contains().(Capabilitiesrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/Capabilities#::DEPRECATE_EOFrust-analyzer cargo sqlx-mysql 0.8.3 protocol/capabilities/impl#[Capabilities]DEPRECATE_EOF.) {
96 let ok = self.okrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
97
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(EofPacketrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/eof/EofPacket# {
99 warningsrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/eof/EofPacket#warnings.: ok.warningsrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/ok/OkPacket#warnings.,
100 statusrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/eof/EofPacket#status.: ok.statusrust-analyzer cargo sqlx-mysql 0.8.3 protocol/response/ok/OkPacket#status.,
101 })
102 } else {
103 self.decode_withrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`]decode_with().(capabilities)
104 }
105 }
106 }
107
108 impl Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for Packetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#> {
109 type Targetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`][Deref]Target# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#;
110
111 fn derefrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`][Deref]deref().(&self) -> &Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes# {
112 &self.0rust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#0.
113 }
114 }
115
116 impl DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# for Packetrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#> {
117 fn deref_mutrust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/impl#[`Packet<Bytes>`][DerefMut]deref_mut().(&mut self) -> &mut Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes# {
118 &mut self.0rust-analyzer cargo sqlx-mysql 0.8.3 protocol/packet/Packet#0.
119 }
120 }