I've generic trait in the app to convert results across different storage. Now I want to add support for SQLx. I want to keep it generic for all sqlx engines.
Simplified example for i64:
// My trait
pub trait TryConvert {
fn try_i64(&self) -> i64;
}
First impl try:
// Gives error
// the trait `sqlx::Decode<'_, DB>` is not implemented for `i64`
impl<DB, V> TryConvert for V
where
DB: Database,
V: Value<Database = DB>,
{
fn try_i64(&self) -> i64 {
self.try_decode_unchecked().unwrap()
}
}
Now with Decode
for i64:
// Gives error:
// expected `sqlx::Decode<'_, DB>` found `sqlx::Decode<'a, DB>`
impl<'a, DB, V> TryConvert for V
where
DB: Database,
V: Value<Database = DB>,
i64: Decode<'a, DB>,
{
fn try_i64(&self) -> i64 {
self.try_decode_unchecked().unwrap()
}
}
But all work with concrete DB type:
impl<V> TryConvert for V
where
V: Value<Database = Sqlite>,
{
fn try_i64(&self) -> i64 {
self.try_decode_unchecked().unwrap()
}
}
Is it possible to keep the TryConvert
generic for any sqlx DB engines?
Thanks to kmdreko. The final solution:
impl<DB, V> TryConvert for V
where
DB: Database,
V: Value<Database = DB>,
i64: for <'a> Decode<'a, DB>,
{
fn try_i64(&self) -> i64 {
self.try_decode_unchecked().unwrap()
}
}
More information about for <'_>