Skip to content

Commit 4ab11d5

Browse files
authored
Merge pull request rust-postgres#915 from xoac/feat/const-size-array-as-bytea
feat: support [u8; N] as BYTEA
2 parents 0984ab1 + 6b8cb8a commit 4ab11d5

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

postgres-types/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ pub enum IsNull {
754754
/// | `f64` | DOUBLE PRECISION |
755755
/// | `&str`/`String` | VARCHAR, CHAR(n), TEXT, CITEXT, NAME |
756756
/// | | LTREE, LQUERY, LTXTQUERY |
757-
/// | `&[u8]`/`Vec<u8>` | BYTEA |
757+
/// | `&[u8]`/`Vec<u8>`/`[u8; N]` | BYTEA |
758758
/// | `HashMap<String, Option<String>>` | HSTORE |
759759
/// | `SystemTime` | TIMESTAMP, TIMESTAMP WITH TIME ZONE |
760760
/// | `IpAddr` | INET |
@@ -794,9 +794,9 @@ pub enum IsNull {
794794
///
795795
/// # Arrays
796796
///
797-
/// `ToSql` is implemented for `Vec<T>`, `&[T]`, `Box<[T]>` and `[T; N]` where
798-
/// `T` implements `ToSql`, and corresponds to one-dimensional Postgres arrays
799-
/// with an index offset of 1.
797+
/// `ToSql` is implemented for `[u8; N]`, `Vec<T>`, `&[T]`, `Box<[T]>` and `[T; N]`
798+
/// where `T` implements `ToSql` and `N` is const usize, and corresponds to one-dimensional
799+
/// Postgres arrays with an index offset of 1.
800800
///
801801
/// **Note:** the impl for arrays only exist when the Cargo feature `array-impls`
802802
/// is enabled.
@@ -915,6 +915,18 @@ impl<'a> ToSql for &'a [u8] {
915915
to_sql_checked!();
916916
}
917917

918+
#[cfg(feature = "array-impls")]
919+
impl<const N: usize> ToSql for [u8; N] {
920+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
921+
types::bytea_to_sql(&self[..], w);
922+
Ok(IsNull::No)
923+
}
924+
925+
accepts!(BYTEA);
926+
927+
to_sql_checked!();
928+
}
929+
918930
#[cfg(feature = "array-impls")]
919931
impl<T: ToSql, const N: usize> ToSql for [T; N] {
920932
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {

0 commit comments

Comments
 (0)