Is it possible to map an Option<impl Trait> to a Option<Box<dyn Trait>>? If the argument to new() is not an Option, it is possible to assign it to a struct with Some(Box::new(item)). Why does this work and the map doesn't?
trait TestTrait {}
impl TestTrait for i32 {}
struct TestStruct {
item: Option<Box<dyn TestTrait>>
}
impl TestStruct {
pub fn new(item: Option<impl TestTrait + 'static>) -> Self {
let item: Option<Box<dyn TestTrait>> = item.map(|i| Box::new(i));
Self {
item
}
}
}
fn main() {
let num:i32 = 0;
let s = TestStruct::new(Some(num));
}