alloy_primitives/
diesel.rs1use crate::{FixedBytes, Signature, SignatureError};
7
8use diesel::{
9 backend::Backend,
10 deserialize::{FromSql, Result as DeserResult},
11 query_builder::bind_collector::RawBytesBindCollector,
12 serialize::{IsNull, Output, Result as SerResult, ToSql},
13 sql_types::Binary,
14};
15use std::io::Write;
16
17impl<const BYTES: usize, Db> ToSql<Binary, Db> for FixedBytes<BYTES>
18where
19 for<'c> Db: Backend<BindCollector<'c> = RawBytesBindCollector<Db>>,
20{
21 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Db>) -> SerResult {
22 out.write_all(&self[..])?;
23 Ok(IsNull::No)
24 }
25}
26
27impl<const BYTES: usize, Db: Backend> FromSql<Binary, Db> for FixedBytes<BYTES>
28where
29 *const [u8]: FromSql<Binary, Db>,
30{
31 fn from_sql(bytes: Db::RawValue<'_>) -> DeserResult<Self> {
32 let bytes: *const [u8] = FromSql::<Binary, Db>::from_sql(bytes)?;
33 let bytes = unsafe { &*bytes };
34 Self::try_from(bytes).map_err(|e| e.into())
35 }
36}
37
38impl<Db: Backend> ToSql<Binary, Db> for Signature
39where
40 for<'c> Db: Backend<BindCollector<'c> = RawBytesBindCollector<Db>>,
41{
42 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Db>) -> SerResult {
43 out.write_all(&self.as_erc2098())?;
44 Ok(IsNull::No)
45 }
46}
47
48impl<Db: Backend> FromSql<Binary, Db> for Signature
49where
50 *const [u8]: FromSql<Binary, Db>,
51{
52 fn from_sql(bytes: Db::RawValue<'_>) -> DeserResult<Self> {
53 let bytes: *const [u8] = FromSql::<Binary, Db>::from_sql(bytes)?;
54 let bytes = unsafe { &*bytes };
55 if bytes.len() != 64 {
56 return Err(SignatureError::FromBytes("Invalid length").into());
57 }
58 Ok(Self::from_erc2098(bytes))
59 }
60}