Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive clone and debug for postgresql arguments #3687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sqlx-postgres/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sqlx_core::error::BoxDynError;
// that has a patch, we then apply the patch which should write to &mut Vec<u8>,
// backtrack and update the prefixed-len, then write until the next patch offset

#[derive(Default)]
#[derive(Default, Debug, Clone)]
pub struct PgArgumentBuffer {
buffer: Vec<u8>,

Expand All @@ -46,20 +46,32 @@ pub struct PgArgumentBuffer {
type_holes: Vec<(usize, HoleKind)>, // Vec<{ offset, type_name }>
}

#[derive(Debug, Clone)]
enum HoleKind {
Type { name: UStr },
Array(Arc<PgArrayOf>),
}

#[derive(Clone)]
struct Patch {
buf_offset: usize,
arg_index: usize,
#[allow(clippy::type_complexity)]
callback: Box<dyn Fn(&mut [u8], &PgTypeInfo) + 'static + Send + Sync>,
callback: Arc<dyn Fn(&mut [u8], &PgTypeInfo) + 'static + Send + Sync>,
}

impl fmt::Debug for Patch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Patch")
.field("buf_offset", &self.buf_offset)
.field("arg_index", &self.arg_index)
.field("callback", &"<callback>")
.finish()
}
}

/// Implementation of [`Arguments`] for PostgreSQL.
#[derive(Default)]
#[derive(Default, Debug, Clone)]
pub struct PgArguments {
// Types of each bind parameter
pub(crate) types: Vec<PgTypeInfo>,
Expand Down Expand Up @@ -194,7 +206,7 @@ impl PgArgumentBuffer {
self.patches.push(Patch {
buf_offset: offset,
arg_index,
callback: Box::new(callback),
callback: Arc::new(callback),
});
}

Expand Down
2 changes: 1 addition & 1 deletion sqlx-postgres/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub enum PgTypeKind {
Range(PgTypeInfo),
}

#[derive(Debug)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "offline", derive(serde::Serialize, serde::Deserialize))]
pub struct PgArrayOf {
pub(crate) elem_name: UStr,
Expand Down