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
38 changes: 37 additions & 1 deletion crates/primitives/src/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use alloc::{boxed::Box, vec::Vec};

use ruint::support::sqlx::DecodeError;
use sqlx_core::{
database::Database,
decode::Decode,
Expand All @@ -15,7 +16,7 @@ use sqlx_core::{
types::Type,
};

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

impl<const BYTES: usize, DB> Type<DB> for FixedBytes<BYTES>
where
Expand Down Expand Up @@ -54,3 +55,38 @@ where
Self::try_from(bytes.as_slice()).map_err(|e| Box::new(e) as BoxDynError)
}
}

impl<const BITS: usize, const LIMBS: usize, DB: Database> Type<DB> for Signed<BITS, LIMBS>
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, const BITS: usize, const LIMBS: usize, DB: Database> Encode<'a, DB> for Signed<BITS, LIMBS>
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
self.0.to_be_bytes_vec().encode_by_ref(buf)
}
}

impl<'a, const BITS: usize, const LIMBS: usize, DB: Database> Decode<'a, DB> for Signed<BITS, LIMBS>
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
let bytes = Vec::<u8>::decode(value)?;
Self::try_from_be_slice(bytes.as_slice()).ok_or_else(|| DecodeError::Overflow.into())
}
}
Loading