pub trait Encode {
// Required methods
fn encode_binary(&self, buf: &mut Vec<u8>);
fn type_oid(&self) -> u32;
// Provided methods
fn is_null(&self) -> bool { ... }
fn encode_at(&self, dst: &mut [u8]) -> bool { ... }
}Expand description
Encode a Rust value into PostgreSQL binary format.
Implementations append the binary representation to buf. The length prefix
is handled by the caller (wire protocol layer), not the encoder.
§Example
use bsql_driver_postgres::Encode;
let mut buf = Vec::new();
42i32.encode_binary(&mut buf);
assert_eq!(buf, &[0, 0, 0, 42]);Required Methods§
Sourcefn encode_binary(&self, buf: &mut Vec<u8>)
fn encode_binary(&self, buf: &mut Vec<u8>)
Append the binary-encoded value to buf.
Provided Methods§
Sourcefn is_null(&self) -> bool
fn is_null(&self) -> bool
Whether this value represents SQL NULL.
When true, the wire protocol sends length -1 with no data bytes.
Default is false. Implementations for Option<T> override this.
Sourcefn encode_at(&self, dst: &mut [u8]) -> bool
fn encode_at(&self, dst: &mut [u8]) -> bool
Encode the binary value directly into dst at position 0.
Returns true if the encoded length matches dst.len() (i.e., same size
as the template slot). Returns false if the size differs, signaling the
caller to fall back to a full rebuild.
The default implementation uses a small inline buffer and copies. Fixed-size types (i32, i64, etc.) override this to write directly — eliminating the scratch buffer double-copy on the bind-template hot path.