sqlx_etorreborre_postgres/message/
backend_key_data.rs

1use byteorder::{BigEndian, ByteOrder};
2use sqlx_core::bytes::Bytes;
3
4use crate::error::Error;
5use crate::io::Decode;
6
7/// Contains cancellation key data. The frontend must save these values if it
8/// wishes to be able to issue `CancelRequest` messages later.
9#[derive(Debug)]
10pub struct BackendKeyData {
11    /// The process ID of this database.
12    pub process_id: u32,
13
14    /// The secret key of this database.
15    pub secret_key: u32,
16}
17
18impl Decode<'_> for BackendKeyData {
19    fn decode_with(buf: Bytes, _: ()) -> Result<Self, Error> {
20        let process_id = BigEndian::read_u32(&buf);
21        let secret_key = BigEndian::read_u32(&buf[4..]);
22
23        Ok(Self {
24            process_id,
25            secret_key,
26        })
27    }
28}
29
30#[test]
31fn test_decode_backend_key_data() {
32    const DATA: &[u8] = b"\0\0'\xc6\x89R\xc5+";
33
34    let m = BackendKeyData::decode(DATA.into()).unwrap();
35
36    assert_eq!(m.process_id, 10182);
37    assert_eq!(m.secret_key, 2303903019);
38}
39
40#[cfg(all(test, not(debug_assertions)))]
41#[bench]
42fn bench_decode_backend_key_data(b: &mut test::Bencher) {
43    const DATA: &[u8] = b"\0\0'\xc6\x89R\xc5+";
44
45    b.iter(|| {
46        BackendKeyData::decode(test::black_box(Bytes::from_static(DATA))).unwrap();
47    });
48}