sqlx_core_oldapi/postgres/message/
parameter_description.rs1use bytes::{Buf, Bytes};
2use smallvec::SmallVec;
3
4use crate::error::Error;
5use crate::io::Decode;
6use crate::postgres::types::Oid;
7
8#[derive(Debug)]
9pub struct ParameterDescription {
10 pub types: SmallVec<[Oid; 6]>,
11}
12
13impl Decode<'_> for ParameterDescription {
14 fn decode_with(mut buf: Bytes, _: ()) -> Result<Self, Error> {
15 let cnt = buf.get_u16();
16 let mut types = SmallVec::with_capacity(cnt as usize);
17
18 for _ in 0..cnt {
19 types.push(Oid(buf.get_u32()));
20 }
21
22 Ok(Self { types })
23 }
24}
25
26#[test]
27fn test_decode_parameter_description() {
28 const DATA: &[u8] = b"\x00\x02\x00\x00\x00\x00\x00\x00\x05\x00";
29
30 let m = ParameterDescription::decode(DATA.into()).unwrap();
31
32 assert_eq!(m.types.len(), 2);
33 assert_eq!(m.types[0], Oid(0x0000_0000));
34 assert_eq!(m.types[1], Oid(0x0000_0500));
35}
36
37#[test]
38fn test_decode_empty_parameter_description() {
39 const DATA: &[u8] = b"\x00\x00";
40
41 let m = ParameterDescription::decode(DATA.into()).unwrap();
42
43 assert!(m.types.is_empty());
44}
45
46#[cfg(all(test, not(debug_assertions)))]
47#[bench]
48fn bench_decode_parameter_description(b: &mut test::Bencher) {
49 const DATA: &[u8] = b"\x00\x02\x00\x00\x00\x00\x00\x00\x05\x00";
50
51 b.iter(|| {
52 ParameterDescription::decode(test::black_box(Bytes::from_static(DATA))).unwrap();
53 });
54}