Skip to content
Merged
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
36 changes: 35 additions & 1 deletion crates/primitives/src/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sqlx_core::{
types::Type,
};

use crate::{FixedBytes, Signed};
use crate::{Bytes, FixedBytes, Signed};

impl<const BYTES: usize, DB> Type<DB> for FixedBytes<BYTES>
where
Expand Down Expand Up @@ -90,3 +90,37 @@ where
Self::try_from_be_slice(bytes.as_slice()).ok_or_else(|| DecodeError::Overflow.into())
}
}

impl<DB: Database> Type<DB> for Bytes
where
Vec<u8>: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<Vec<u8> as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<u8> as Type<DB>>::compatible(ty)
}
}

impl<'a, DB: Database> Encode<'a, DB> for Bytes
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
self.to_vec().encode_by_ref(buf)
Comment on lines +108 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, do we need the to_vec here or can we use the &[u8] impl for this?

}
}

impl<'a, DB: Database> Decode<'a, DB> for Bytes
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
Vec::<u8>::decode(value).map(Self::from)
}
}
Loading