Merge pull request #915 from xoac/feat/const-size-array-as-bytea

feat: support [u8; N] as BYTEA
This commit is contained in:
Steven Fackler 2022-07-04 18:57:19 -04:00 committed by GitHub
commit 4ab11d59d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -754,7 +754,7 @@ pub enum IsNull {
/// | `f64` | DOUBLE PRECISION |
/// | `&str`/`String` | VARCHAR, CHAR(n), TEXT, CITEXT, NAME |
/// | | LTREE, LQUERY, LTXTQUERY |
/// | `&[u8]`/`Vec<u8>` | BYTEA |
/// | `&[u8]`/`Vec<u8>`/`[u8; N]` | BYTEA |
/// | `HashMap<String, Option<String>>` | HSTORE |
/// | `SystemTime` | TIMESTAMP, TIMESTAMP WITH TIME ZONE |
/// | `IpAddr` | INET |
@ -794,9 +794,9 @@ pub enum IsNull {
///
/// # Arrays
///
/// `ToSql` is implemented for `Vec<T>`, `&[T]`, `Box<[T]>` and `[T; N]` where
/// `T` implements `ToSql`, and corresponds to one-dimensional Postgres arrays
/// with an index offset of 1.
/// `ToSql` is implemented for `[u8; N]`, `Vec<T>`, `&[T]`, `Box<[T]>` and `[T; N]`
/// where `T` implements `ToSql` and `N` is const usize, and corresponds to one-dimensional
/// Postgres arrays with an index offset of 1.
///
/// **Note:** the impl for arrays only exist when the Cargo feature `array-impls`
/// is enabled.
@ -915,6 +915,18 @@ impl<'a> ToSql for &'a [u8] {
to_sql_checked!();
}
#[cfg(feature = "array-impls")]
impl<const N: usize> ToSql for [u8; N] {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
types::bytea_to_sql(&self[..], w);
Ok(IsNull::No)
}
accepts!(BYTEA);
to_sql_checked!();
}
#[cfg(feature = "array-impls")]
impl<T: ToSql, const N: usize> ToSql for [T; N] {
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {