Skip to content

Fix slice reference encoding for array parameters#115

Merged
swlynch99 merged 1 commit into
mainfrom
claude/wizardly-ptolemy-hpm6te
Jun 10, 2026
Merged

Fix slice reference encoding for array parameters#115
swlynch99 merged 1 commit into
mainfrom
claude/wizardly-ptolemy-hpm6te

Conversation

@swlynch99

Copy link
Copy Markdown
Contributor

Summary

This PR fixes an issue where slice references (&[T]) could not be bound as array parameters in SQL queries. The fix involves explicitly forwarding the Encode implementation for slice references and adding regression tests to ensure the fix works across all supported element types.

Key Changes

  • Updated forward_slice_encode_deref! macro: Added explicit forwarding for &'_ [$elem] slice references. This is necessary because sqlx 0.9's blanket impl Encode for &T requires T: Sized, which doesn't cover unsized slice types &[T].
  • Improved documentation: Clarified the comment explaining why slice references need explicit forwarding while Cow<'_, [$elem]> is already covered by blanket impls.
  • Added regression tests: Introduced a new test module that verifies &[T] implements Encode for all supported element types (i8, i16, i32, i64, f32, f64, bool, &str, String, and uuid::Uuid when the uuid feature is enabled).

Implementation Details

The fix leverages the existing forward_encode_deref! macro to create the necessary Encode implementations for slice references. The test uses a compile-time assertion pattern (assert_encode::<T>()) to ensure that the trait bounds are satisfied, which will catch any regressions if the forwarding implementations are accidentally removed or broken.

https://claude.ai/code/session_0175YJdZtGPi5MhpF6HYErzC

sqlx 0.9's blanket `impl Encode for &T` requires `T: Sized`, so it does
not cover `&[T]` (the pointee `[T]` is unsized). As a result, binding a
slice reference such as `&[i32]` or `&[&str]` as an array parameter no
longer compiled for the element types declared via `generic_slice_decl!`,
while the hand-written `uuid` impls still worked — an inconsistent
regression in 0.6.0.

Add the missing `&[$elem]` forward to `forward_slice_encode_deref!` so
the macro-declared element types regain parity with `uuid`, and add a
compile-time regression test covering the affected element types.

Fixes #114
@swlynch99 swlynch99 enabled auto-merge (squash) June 10, 2026 22:27
@swlynch99 swlynch99 merged commit a7b76ba into main Jun 10, 2026
7 checks passed
@swlynch99 swlynch99 deleted the claude/wizardly-ptolemy-hpm6te branch June 10, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants