1

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?

smitop
  • 4,770
  • 2
  • 20
  • 53
Alex
  • 49
  • 2
  • 6
  • 1
    Based on the error message, try using `i64: for<'a> Decode<'a, DB>` in the constraint and remove `'a` from the `impl`. – kmdreko Sep 19 '21 at 02:22
  • Wow, this works. Where can I read more about it? I've checked https://doc.rust-lang.org/reference/trait-bounds.html#higher-ranked-trait-bounds But it is still unclear for me. Is it the way to define "local" lifetime for the trait? – Alex Sep 19 '21 at 09:58

1 Answers1

0

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 <'_>

Alex
  • 49
  • 2
  • 6