Skip to content

Commit 1bd0d92

Browse files
committed
Implement ToSql & FromSql for Box<[T]>
1 parent e569025 commit 1bd0d92

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

postgres-types/src/lib.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,11 @@ impl WrongType {
452452
///
453453
/// # Arrays
454454
///
455-
/// `FromSql` is implemented for `Vec<T>` and `[T; N]` where `T` implements
456-
/// `FromSql`, and corresponds to one-dimensional Postgres arrays. **Note:**
457-
/// the impl for arrays only exist when the Cargo feature `array-impls` is
458-
/// enabled.
455+
/// `FromSql` is implemented for `Vec<T>`, `Box<[T]>` and `[T; N]` where `T`
456+
/// implements `FromSql`, and corresponds to one-dimensional Postgres arrays.
457+
///
458+
/// **Note:** the impl for arrays only exist when the Cargo feature `array-impls`
459+
/// is enabled.
459460
pub trait FromSql<'a>: Sized {
460461
/// Creates a new value of this type from a buffer of data of the specified
461462
/// Postgres `Type` in its binary format.
@@ -580,6 +581,16 @@ impl<'a, T: FromSql<'a>, const N: usize> FromSql<'a> for [T; N] {
580581
}
581582
}
582583

584+
impl<'a, T: FromSql<'a>> FromSql<'a> for Box<[T]> {
585+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
586+
Vec::<T>::from_sql(ty, raw).map(Vec::into_boxed_slice)
587+
}
588+
589+
fn accepts(ty: &Type) -> bool {
590+
Vec::<T>::accepts(ty)
591+
}
592+
}
593+
583594
impl<'a> FromSql<'a> for Vec<u8> {
584595
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>> {
585596
Ok(types::bytea_from_sql(raw).to_owned())
@@ -783,10 +794,12 @@ pub enum IsNull {
783794
///
784795
/// # Arrays
785796
///
786-
/// `ToSql` is implemented for `Vec<T>`, `&[T]` and `[T; N]` where `T`
787-
/// implements `ToSql`, and corresponds to one-dimensional Postgres arrays with
788-
/// an index offset of 1. **Note:** the impl for arrays only exist when the
789-
/// Cargo feature `array-impls` is enabled.
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.
800+
///
801+
/// **Note:** the impl for arrays only exist when the Cargo feature `array-impls`
802+
/// is enabled.
790803
pub trait ToSql: fmt::Debug {
791804
/// Converts the value of `self` into the binary format of the specified
792805
/// Postgres `Type`, appending it to `out`.
@@ -927,6 +940,18 @@ impl<T: ToSql> ToSql for Vec<T> {
927940
to_sql_checked!();
928941
}
929942

943+
impl<T: ToSql> ToSql for Box<[T]> {
944+
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
945+
<&[T] as ToSql>::to_sql(&&**self, ty, w)
946+
}
947+
948+
fn accepts(ty: &Type) -> bool {
949+
<&[T] as ToSql>::accepts(ty)
950+
}
951+
952+
to_sql_checked!();
953+
}
954+
930955
impl ToSql for Vec<u8> {
931956
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
932957
<&[u8] as ToSql>::to_sql(&&**self, ty, w)

0 commit comments

Comments
 (0)