use crate::postgres::type_info::SharedStr;
use crate::postgres::PgConnection;
use byteorder::{ByteOrder, NetworkEndian};
use core::ops::{Deref, DerefMut};
#[derive(Debug, Default, PartialEq)]
pub struct PgRawBuffer {
inner: Vec<u8>,
type_holes: Vec<(usize, SharedStr)>,
}
impl PgRawBuffer {
pub(crate) fn push_type_hole(&mut self, type_name: &SharedStr) {
let offset = self.len();
self.extend_from_slice(&0_u32.to_be_bytes());
self.type_holes.push((offset, type_name.clone()));
}
pub(crate) async fn patch_type_holes(
&mut self,
connection: &mut PgConnection,
) -> crate::Result<()> {
for (offset, name) in &self.type_holes {
let oid = connection.get_type_id_by_name(&*name).await?;
NetworkEndian::write_u32(&mut self.inner[*offset..], oid);
}
Ok(())
}
}
impl Deref for PgRawBuffer {
type Target = Vec<u8>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for PgRawBuffer {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}