I can't decide if this is a compiler bug, a bug in async_trait
, or a bug on my part. Based on my reading of the docs on supertraits, I think that this should work correctly, and that I've uncovered a compiler bug, but I want to be sure before I create a reduced test case and submit a bug report.
Basically, I have super and sub traits similar to the following:
use crate::error;
use async_trait::async_trait;
use std::{cmp::Ordering, result::Result};
#[async_trait]
pub trait AsyncFallibleOrdTrait<T>: Send + Sync {
async fn try_cmp(
&self,
other: &dyn AsyncFallibleOrdTrait<T>,
) -> Result<Ordering, error::Error<T>>;
}
#[async_trait]
pub trait KeyTrait<T>: AsyncFallibleOrdTrait<T> {}
(heavily reduced from the original code, I can put it all on GitHub if anyone wants it).
I think that based on the docs I should be able to use trait objects that implement KeyTrait
anywhere AsyncFallibleOrdTrait
is expected. The problem is that doesn't seem to be happening. I have code that is similar to the following:
// original is passed in from elsewhere, trying to reduce code here.
// original: Arc<dyn DBSKeyTrait<T>>;
let k: &dyn KeyTrait<T> = &*Arc::<dyn KeyTrait<T>>::as_ptr(&original);
let order: Ordering = key.try_cmp(k).await?;
Since original
is known to implement KeyTrait
, and since AsyncFallibleOrdTrait
, I thought that this would work, but instead I get an error like the following:
error[E0308]: mismatched types
--> vec_blob_store/src/vec_store.rs:46:47
|
46 | let order: Ordering = key.try_cmp(k).await?;
| ^ expected trait `AsyncFallibleOrdTrait`, found trait `KeyTrait`
|
= note: expected reference `&dyn AsyncFallibleOrdTrait<T>`
found reference `&dyn KeyTrait<T>`
So is this a genuine error in the compiler, or an error in my understanding of how trait objects work?