mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-15 09:22:55 +00:00
Our rust-postgres fork is getting messy. Mostly because proxy wants more
control over the raw protocol than tokio-postgres provides. As such,
it's diverging more and more. Storage and compute also make use of
rust-postgres, but in more normal usage, thus they don't need our crazy
changes.
Idea:
* proxy maintains their subset
* other teams use a minimal patch set against upstream rust-postgres
Reviewing this code will be difficult. To implement it, I
1. Copied tokio-postgres, postgres-protocol and postgres-types from
00940fcdb5
2. Updated their package names with the `2` suffix to make them compile
in the workspace.
3. Updated proxy to use those packages
4. Copied in the code from tokio-postgres-rustls 0.13 (with some patches
applied https://github.com/jbg/tokio-postgres-rustls/pull/32
https://github.com/jbg/tokio-postgres-rustls/pull/33)
5. Removed as much dead code as I could find in the vendored libraries
6. Updated the tokio-postgres-rustls code to use our existing channel
binding implementation
79 lines
2.0 KiB
Rust
79 lines
2.0 KiB
Rust
//! Low level Postgres protocol APIs.
|
|
//!
|
|
//! This crate implements the low level components of Postgres's communication
|
|
//! protocol, including message and value serialization and deserialization.
|
|
//! It is designed to be used as a building block by higher level APIs such as
|
|
//! `rust-postgres`, and should not typically be used directly.
|
|
//!
|
|
//! # Note
|
|
//!
|
|
//! This library assumes that the `client_encoding` backend parameter has been
|
|
//! set to `UTF8`. It will most likely not behave properly if that is not the case.
|
|
#![doc(html_root_url = "https://docs.rs/postgres-protocol/0.6")]
|
|
#![warn(missing_docs, rust_2018_idioms, clippy::all)]
|
|
|
|
use byteorder::{BigEndian, ByteOrder};
|
|
use bytes::{BufMut, BytesMut};
|
|
use std::io;
|
|
|
|
pub mod authentication;
|
|
pub mod escape;
|
|
pub mod message;
|
|
pub mod password;
|
|
pub mod types;
|
|
|
|
/// A Postgres OID.
|
|
pub type Oid = u32;
|
|
|
|
/// A Postgres Log Sequence Number (LSN).
|
|
pub type Lsn = u64;
|
|
|
|
/// An enum indicating if a value is `NULL` or not.
|
|
pub enum IsNull {
|
|
/// The value is `NULL`.
|
|
Yes,
|
|
/// The value is not `NULL`.
|
|
No,
|
|
}
|
|
|
|
fn write_nullable<F, E>(serializer: F, buf: &mut BytesMut) -> Result<(), E>
|
|
where
|
|
F: FnOnce(&mut BytesMut) -> Result<IsNull, E>,
|
|
E: From<io::Error>,
|
|
{
|
|
let base = buf.len();
|
|
buf.put_i32(0);
|
|
let size = match serializer(buf)? {
|
|
IsNull::No => i32::from_usize(buf.len() - base - 4)?,
|
|
IsNull::Yes => -1,
|
|
};
|
|
BigEndian::write_i32(&mut buf[base..], size);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
trait FromUsize: Sized {
|
|
fn from_usize(x: usize) -> Result<Self, io::Error>;
|
|
}
|
|
|
|
macro_rules! from_usize {
|
|
($t:ty) => {
|
|
impl FromUsize for $t {
|
|
#[inline]
|
|
fn from_usize(x: usize) -> io::Result<$t> {
|
|
if x > <$t>::MAX as usize {
|
|
Err(io::Error::new(
|
|
io::ErrorKind::InvalidInput,
|
|
"value too large to transmit",
|
|
))
|
|
} else {
|
|
Ok(x as $t)
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
from_usize!(i16);
|
|
from_usize!(i32);
|