Debian Codegraph @

src/features/diesel.rs

raw
1 #![cfg_attr(docsrs, doc(cfg(feature = "diesel")))]
2
3 // Copied and adapted from
4 // <https://github.com/diesel-rs/diesel/blob/ab70dd5ed1f96926a3e8d98ab42636eaac1e1594/diesel/src/type_impls/primitives.rs>
5
6 use diesel::{backend, deserialize, expression, serialize, sql_types};
7
8 use crate::CompactString;
9
10 #[derive(expression::AsExpression, deserialize::FromSqlRow)]
11 #[diesel(foreign_derive)]
12 #[diesel(sql_type = sql_types::Text)]
13 #[allow(dead_code)]
14 struct CompactStringProxy(CompactString);
15
16 impl<ST, DB> deserialize::FromSql<ST, DB> for CompactString
17 where
18 DB: backend::Backend,
19 *const str: deserialize::FromSql<ST, DB>,
20 {
21 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
22 let str_ptr = <*const str as deserialize::FromSql<ST, DB>>::from_sql(bytes)?;
23 if !str_ptr.is_null() {
24 // SAFETY: We just checked that `str_ptr` is not null, and `from_sql()` should return
25 // a valid pointer to an `str`.
26 let string = unsafe { &*str_ptr };
27 Ok(string.into())
28 } else {
29 Ok(CompactString::new(""))
30 }
31 }
32 }
33
34 impl<DB> serialize::ToSql<sql_types::Text, DB> for CompactString
35 where
36 DB: backend::Backend,
37 str: serialize::ToSql<sql_types::Text, DB>,
38 {
39 fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, DB>) -> serialize::Result {
40 self.as_str().to_sql(out)
41 }
42 }