I'm trying to do a simple Arc
downcast:
use std::any::Any;
use std::sync::{Arc, Mutex};
pub struct OnTheFlySwap<T: Sized> {
inner: Arc<Mutex<Option<Box<T>>>>,
}
pub trait LockableOption<T: Sized>: Send + Sync + Any {}
impl<T: Sized + Send + 'static> LockableOption<T> for OnTheFlySwap<T> {}
struct ArcHolder<U: Sized> {
lockable_option: Arc<dyn LockableOption<U>>,
}
impl<U: 'static + Send + Sync + Sized> ArcHolder<U> {
pub fn as_on_the_fly(&self) -> Result<&OnTheFlySwap<U>, ()> {
let l: Arc<dyn Any + Send + Sync> = self.lockable_option;
match l.downcast::<OnTheFlySwap<U>>() {
Ok(o) => Ok(&o),
Err(_) => Err(()),
}
}
}
but I get
error[E0308]: mismatched types
--> src/lib.rs:18:45
|
18 | let l: Arc<dyn Any + Send + Sync> = self.lockable_option;
| -------------------------- ^^^^^^^^^^^^^^^^^^^^ expected trait `Any + Send + Sync`, found trait `LockableOption<U>`
| |
| expected due to this
|
= note: expected struct `Arc<(dyn Any + Send + Sync + 'static)>`
found struct `Arc<(dyn LockableOption<U> + 'static)>`
The only thing I can think of is that dyn LockableOption<U>
is somehow not implementing Any
. I don't see any other possible problems.
What is happening?