Removes message cloning operation required for query router (#285)

* Removes message cloning operation required for query router

* fmt

* flakey?

* ?
This commit is contained in:
zainkabani
2023-01-19 10:19:49 -05:00
committed by GitHub
parent 87a771aecc
commit ca8901910c
4 changed files with 65 additions and 51 deletions

View File

@@ -7,6 +7,7 @@ use tokio::net::TcpStream;
use crate::errors::Error;
use std::collections::HashMap;
use std::io::{BufRead, Cursor};
use std::mem;
/// Postgres data type mappings
@@ -548,3 +549,19 @@ pub fn server_parameter_message(key: &str, value: &str) -> BytesMut {
server_info
}
pub trait BytesMutReader {
fn read_string(&mut self) -> Result<String, Error>;
}
impl BytesMutReader for Cursor<&BytesMut> {
/// Should only be used when reading strings from the message protocol.
/// Can be used to read multiple strings from the same message which are separated by the null byte
fn read_string(&mut self) -> Result<String, Error> {
let mut buf = vec![];
match self.read_until(b'\0', &mut buf) {
Ok(_) => Ok(String::from_utf8_lossy(&buf[..buf.len() - 1]).to_string()),
Err(err) => return Err(Error::ParseBytesError(err.to_string())),
}
}
}