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

Implementing ToSql for [u8;N] #3714

Merged
merged 5 commits into from
Jul 26, 2023
Merged
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
15 changes: 15 additions & 0 deletions diesel/src/type_impls/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ mod foreign_impls {
#[diesel(foreign_derive, not_sized)]
#[diesel(sql_type = Binary)]
struct BinarySliceProxy([u8]);

#[derive(AsExpression)]
#[diesel(foreign_derive)]
#[diesel(sql_type = Binary)]
struct BinaryArrayProxy<const N: usize>([u8; N]);
}

impl<ST, DB> FromSql<ST, DB> for String
Expand Down Expand Up @@ -175,6 +180,16 @@ where
}
}

impl<DB, const N: usize> ToSql<sql_types::Binary, DB> for [u8; N]
where
DB: Backend,
[u8]: ToSql<sql_types::Binary, DB>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
self.as_slice().to_sql(out)
}
}

impl<DB> ToSql<sql_types::Binary, DB> for [u8]
where
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
Expand Down
5 changes: 5 additions & 0 deletions diesel_tests/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ fn binary_from_sql() {
#[cfg(feature = "postgres")]
fn bytes_to_sql_binary() {
let invalid_utf8_bytes = vec![0x1Fu8, 0x8Bu8];
let invalid_utf8_array = [0x1Fu8, 0x8Bu8];
assert!(query_to_sql_equality::<Binary, Vec<u8>>(
"E'\\\\x1F8B'::bytea",
invalid_utf8_bytes.clone()
Expand All @@ -618,6 +619,10 @@ fn bytes_to_sql_binary() {
"E'\\\\x1F8B'::bytea",
&invalid_utf8_bytes
));
assert!(query_to_sql_equality::<Binary, &[u8; 2]>(
"E'\\\\x1F8B'::bytea",
&invalid_utf8_array
));
assert!(!query_to_sql_equality::<Binary, &[u8]>(
"''::bytea",
&invalid_utf8_bytes
Expand Down