Skip to content

Commit

Permalink
Use slices instead of Vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
nyovaya committed Aug 17, 2024
1 parent 1cf9cc5 commit 94ba6ff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions examples/testlibpq3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
*/

/* Here is our out-of-line parameter value */
let param_values = vec![Some(b"joe's place\0".to_vec())];
let param_values = vec![Some("joe's place\0".as_bytes())];

let res = conn.exec_params(
"SELECT * FROM test1 WHERE t = $1",
Expand Down Expand Up @@ -91,7 +91,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let binary_int_val = htonl(2);

/* Set up parameter arrays for PQexecParams */
let param_values = vec![Some(binary_int_val)];
let param_values = vec![Some(&binary_int_val[..])];
let param_formats = vec![libpq::Format::Binary];

let res = conn.exec_params(
Expand Down Expand Up @@ -165,6 +165,6 @@ fn ntohl(netlong: &[u8]) -> Result<i32, std::array::TryFromSliceError> {
netlong[..4].try_into().map(i32::from_be_bytes)
}

fn htonl(hostlong: i32) -> Vec<u8> {
hostlong.to_be_bytes().to_vec()
fn htonl(hostlong: i32) -> [u8; 4] {
hostlong.to_be_bytes()
}
4 changes: 2 additions & 2 deletions src/connection/_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Connection {
&self,
command: &str,
param_types: &[crate::Oid],
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::errors::Result {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Connection {
pub fn send_query_prepared(
&self,
name: Option<&str>,
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::errors::Result {
Expand Down
4 changes: 2 additions & 2 deletions src/connection/_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Connection {
&self,
command: &str,
param_types: &[crate::Oid],
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::PQResult {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Connection {
pub fn exec_prepared(
&self,
name: Option<&str>,
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::PQResult {
Expand Down
16 changes: 8 additions & 8 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Connection {
}

fn transform_params(
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
) -> (Vec<*const libc::c_char>, Vec<i32>, Vec<i32>) {
if param_values.is_empty() {
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Connection {
prefix: &str,
command: &str,
param_types: &[crate::Oid],
param_values: &[Option<Vec<u8>>],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
) {
use std::fmt::Write;
Expand Down Expand Up @@ -284,7 +284,7 @@ mod test {
let results = conn.exec_params(
"SELECT $1",
&[crate::types::INT4.oid],
&[Some(b"1\0".to_vec())],
&[Some(b"1\0")],
&[],
crate::Format::Text,
);
Expand All @@ -299,7 +299,7 @@ mod test {
let results = conn.exec_params(
"SELECT $1",
&[crate::types::INT4.oid],
&[Some(b"foo\0".to_vec())],
&[Some(b"foo\0")],
&[],
crate::Format::Text,
);
Expand All @@ -313,7 +313,7 @@ mod test {
let _ = conn.exec_params(
"SELECT $1",
&[],
&[Some(b"foo".to_vec())],
&[Some(b"foo")],
&[],
crate::Format::Text,
);
Expand All @@ -330,7 +330,7 @@ mod test {

let results = conn.exec_prepared(
Some("test1"),
&[Some(b"fooo\0".to_vec())],
&[Some(b"fooo\0")],
&[],
crate::Format::Text,
);
Expand Down Expand Up @@ -360,7 +360,7 @@ mod test {
conn.send_query_params(
"SELECT $1",
&[crate::types::TEXT.oid],
&[Some(b"fooo\0".to_vec())],
&[Some(b"fooo\0")],
&[],
crate::Format::Text,
)
Expand Down Expand Up @@ -397,7 +397,7 @@ mod test {
.unwrap();
while conn.result().is_some() {}

conn.send_query_prepared(None, &[Some(b"fooo\0".to_vec())], &[], crate::Format::Text)
conn.send_query_prepared(None, &[Some(b"fooo\0")], &[], crate::Format::Text)
.unwrap();
assert_eq!(conn.result().unwrap().value(0, 0), Some(&b"fooo"[..]));
assert!(conn.result().is_none());
Expand Down

0 comments on commit 94ba6ff

Please sign in to comment.