sqlx_postgres/io/
buf_mut.rs1use crate::io::{PortalId, StatementId};
2
3pub trait PgBufMutExt {
4 fn put_length_prefixed<F>(&mut self, f: F) -> Result<(), crate::Error>
5 where
6 F: FnOnce(&mut Vec<u8>) -> Result<(), crate::Error>;
7
8 fn put_statement_name(&mut self, id: StatementId);
9
10 fn put_portal_name(&mut self, id: PortalId);
11}
12
13impl PgBufMutExt for Vec<u8> {
14 fn put_length_prefixed<F>(&mut self, write_contents: F) -> Result<(), crate::Error>
17 where
18 F: FnOnce(&mut Vec<u8>) -> Result<(), crate::Error>,
19 {
20 let offset = self.len();
22 self.extend(&[0; 4]);
23
24 let write_result = write_contents(self);
26
27 let size_result = write_result.and_then(|_| {
28 let size = self.len() - offset;
29 i32::try_from(size)
30 .map_err(|_| err_protocol!("message size out of range for protocol: {size}"))
31 });
32
33 match size_result {
34 Ok(size) => {
35 self[offset..(offset + 4)].copy_from_slice(&size.to_be_bytes());
37 Ok(())
38 }
39 Err(e) => {
40 self.truncate(offset);
42 Err(e)
43 }
44 }
45 }
46
47 #[inline]
49 fn put_statement_name(&mut self, id: StatementId) {
50 id.put_name_with_nul(self);
51 }
52
53 #[inline]
55 fn put_portal_name(&mut self, id: PortalId) {
56 id.put_name_with_nul(self);
57 }
58}