| 1 | //! Low level Postgres protocol APIs. |
| 2 | //! |
| 3 | //! This crate implements the low level components of Postgres's communication |
| 4 | //! protocol, including message and value serialization and deserialization. |
| 5 | //! It is designed to be used as a building block by higher level APIs such as |
| 6 | //! `rust-postgres`, and should not typically be used directly. |
| 7 | //! |
| 8 | //! # Note |
| 9 | //! |
| 10 | //! This library assumes that the `client_encoding` backend parameter has been |
| 11 | //! set to `UTF8`. It will most likely not behave properly if that is not the case. |
| 12 | #![warn(missing_docs, rust_2018_idioms, clippy::all)] |
| 13 | |
| 14 | use byteorderrust-analyzer cargo byteorder 1.5.0 crate/::{BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#, ByteOrderrust-analyzer cargo byteorder 1.5.0 ByteOrder#}; |
| 15 | use bytesrust-analyzer cargo bytes 1.11.1 crate/::{BufMutrust-analyzer cargo bytes 1.11.1 buf/buf_mut/BufMut#, BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#}; |
| 16 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 17 | |
| 18 | pub mod authenticationrust-analyzer cargo postgres-protocol 0.6.12 authentication/; |
| 19 | pub mod escaperust-analyzer cargo postgres-protocol 0.6.12 escape/; |
| 20 | mod hexrust-analyzer cargo postgres-protocol 0.6.12 hex/; |
| 21 | pub mod messagerust-analyzer cargo postgres-protocol 0.6.12 message/; |
| 22 | pub mod passwordrust-analyzer cargo postgres-protocol 0.6.12 password/; |
| 23 | pub mod typesrust-analyzer cargo postgres-protocol 0.6.12 types/; |
| 24 | |
| 25 | /// A Postgres OID. |
| 26 | pub type Oidrust-analyzer cargo postgres-protocol 0.6.12 Oid# = u32; |
| 27 | |
| 28 | /// A Postgres Log Sequence Number (LSN). |
| 29 | pub type Lsnrust-analyzer cargo postgres-protocol 0.6.12 Lsn# = u64; |
| 30 | |
| 31 | /// An enum indicating if a value is `NULL` or not. |
| 32 | pub enum IsNullrust-analyzer cargo postgres-protocol 0.6.12 IsNull# { |
| 33 | /// The value is `NULL`. |
| 34 | Yesrust-analyzer cargo postgres-protocol 0.6.12 IsNull#Yes#, |
| 35 | /// The value is not `NULL`. |
| 36 | Norust-analyzer cargo postgres-protocol 0.6.12 IsNull#No#, |
| 37 | } |
| 38 | |
| 39 | fn write_nullablerust-analyzer cargo postgres-protocol 0.6.12 write_nullable().<F, E>(serializer: F, buf: &mut BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> |
| 40 | where |
| 41 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo postgres-protocol 0.6.12 IsNull#, E>, |
| 42 | E: Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>, |
| 43 | { |
| 44 | let base = buf.lenrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]len().(); |
| 45 | buf.put_i32rust-analyzer cargo bytes 1.11.1 buf/buf_mut/BufMut#put_i32().(0); |
| 46 | let size = match serializer(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 47 | IsNullrust-analyzer cargo postgres-protocol 0.6.12 IsNull#::Norust-analyzer cargo postgres-protocol 0.6.12 IsNull#No# => i32::from_usizerust-analyzer cargo postgres-protocol 0.6.12 impl#[i32][FromUsize]from_usize().(buf.lenrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().base rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().4)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 48 | IsNullrust-analyzer cargo postgres-protocol 0.6.12 IsNull#::Yesrust-analyzer cargo postgres-protocol 0.6.12 IsNull#Yes# => -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, |
| 49 | }; |
| 50 | BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#::write_i32rust-analyzer cargo byteorder 1.5.0 ByteOrder#write_i32().(&mut buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().base..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()., size); |
| 51 | |
| 52 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 53 | } |
| 54 | |
| 55 | trait FromUsizerust-analyzer cargo postgres-protocol 0.6.12 FromUsize#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 56 | fn from_usizerust-analyzer cargo postgres-protocol 0.6.12 FromUsize#from_usize().(x: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>; |
| 57 | } |
| 58 | |
| 59 | macro_rules! from_usizerust-analyzer cargo postgres-protocol 0.6.12 from_usize! { |
| 60 | ($t:ty) => { |
| 61 | impl FromUsize for $t { |
| 62 | #[inline] |
| 63 | fn from_usize(x: usize) -> io::Result<$t> { |
| 64 | if x > <$t>::MAX as usize { |
| 65 | Err(io::Error::new( |
| 66 | io::ErrorKind::InvalidInput, |
| 67 | "value too large to transmit", |
| 68 | )) |
| 69 | } else { |
| 70 | Ok(x as $t) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | from_usizerust-analyzer cargo postgres-protocol 0.6.12 from_usize!!(i16); |
| 78 | from_usizerust-analyzer cargo postgres-protocol 0.6.12 from_usize!!(u16); |
| 79 | from_usizerust-analyzer cargo postgres-protocol 0.6.12 from_usize!!(i32); |