sqlx_core_oldapi/postgres/io/
buf_mut.rs

1use crate::postgres::types::Oid;
2
3pub trait PgBufMutExt {
4    fn put_length_prefixed<F>(&mut self, f: F)
5    where
6        F: FnOnce(&mut Vec<u8>);
7
8    fn put_statement_name(&mut self, id: Oid);
9
10    fn put_portal_name(&mut self, id: Option<Oid>);
11}
12
13impl PgBufMutExt for Vec<u8> {
14    // writes a length-prefixed message, this is used when encoding nearly all messages as postgres
15    // wants us to send the length of the often-variable-sized messages up front
16    fn put_length_prefixed<F>(&mut self, f: F)
17    where
18        F: FnOnce(&mut Vec<u8>),
19    {
20        // reserve space to write the prefixed length
21        let offset = self.len();
22        self.extend(&[0; 4]);
23
24        // write the main body of the message
25        f(self);
26
27        // now calculate the size of what we wrote and set the length value
28        let size = (self.len() - offset) as i32;
29        self[offset..(offset + 4)].copy_from_slice(&size.to_be_bytes());
30    }
31
32    // writes a statement name by ID
33    #[inline]
34    fn put_statement_name(&mut self, id: Oid) {
35        // N.B. if you change this don't forget to update it in ../describe.rs
36        self.extend(b"sqlx_s_");
37
38        self.extend(itoa::Buffer::new().format(id.0).as_bytes());
39
40        self.push(0);
41    }
42
43    // writes a portal name by ID
44    #[inline]
45    fn put_portal_name(&mut self, id: Option<Oid>) {
46        if let Some(id) = id {
47            self.extend(b"sqlx_p_");
48
49            self.extend(itoa::Buffer::new().format(id.0).as_bytes());
50        }
51
52        self.push(0);
53    }
54}