I'm trying to store a dyn trait inside Arc<Mutex<Option<Box<>>>>>, however for some reason it won't work
use std::sync::{Arc, Mutex};
trait A{}
struct B{}
impl A for B{}
struct H{
c: Arc<Mutex<Option<Box<dyn A>>>>
}
fn main() {
let c =
Arc::new(Mutex::new(Some(Box::new(B{}))));
H{
c: c
};
}
Error:
error[E0308]: mismatched types
--> src/main.rs:17:12
|
17 | c: c
| ^ expected trait object `dyn A`, found struct `B`
|
= note: expected struct `Arc<Mutex<Option<Box<(dyn A + 'static)>>>>`
found struct `Arc<Mutex<Option<Box<B>>>>`
It looks like it cannot store a dyn as a Box<B>, which is strange because this works:
fn main() {
let c: Arc<Mutex<Option<Box<dyn A>>>> =
Arc::new(Mutex::new(Some(Box::new(B{}))));
}
What's the difference?