Fix slice reference encoding for array parameters#115
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theEncodeimplementation for slice references and adding regression tests to ensure the fix works across all supported element types.Key Changes
forward_slice_encode_deref!macro: Added explicit forwarding for&'_ [$elem]slice references. This is necessary because sqlx 0.9's blanketimpl Encode for &TrequiresT: Sized, which doesn't cover unsized slice types&[T].Cow<'_, [$elem]>is already covered by blanket impls.&[T]implementsEncodefor 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 necessaryEncodeimplementations 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