PgLsn type.

This commit is contained in:
Jeff Davis
2020-12-14 11:56:21 -08:00
parent 77aa702e6c
commit f3cbc8ce04
5 changed files with 117 additions and 2 deletions

View File

@@ -24,6 +24,9 @@ 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`.

View File

@@ -8,7 +8,7 @@ use std::io::Read;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str;
use crate::{write_nullable, FromUsize, IsNull, Oid};
use crate::{write_nullable, FromUsize, IsNull, Lsn, Oid};
#[cfg(test)]
mod test;
@@ -142,6 +142,22 @@ pub fn int8_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<dyn Error + Sync + Se
Ok(v)
}
/// Serializes a `PG_LSN` value.
#[inline]
pub fn lsn_to_sql(v: Lsn, buf: &mut BytesMut) {
buf.put_u64(v);
}
/// Deserializes a `PG_LSN` value.
#[inline]
pub fn lsn_from_sql(mut buf: &[u8]) -> Result<Lsn, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_u64::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
}
Ok(v)
}
/// Serializes a `FLOAT4` value.
#[inline]
pub fn float4_to_sql(v: f32, buf: &mut BytesMut) {