mod test {
use std::{any::Any, collections::HashMap};
#[derive(Clone)]
struct First(usize);
#[derive(Clone)]
struct Second(u32);
#[derive(Clone)]
struct View(String, u8);
impl From<View> for First {
fn from(value: View) -> Self {
Self(value.1 as usize)
}
}
impl From<View> for Second {
fn from(value: View) -> Self {
Self(value.1 as u32)
}
}
fn convert<T: Clone>(k: String, data: &mut HashMap<String, Vec<Box<dyn Any>>>) {
let views = vec![
View(String::from("first"), 1),
View(String::from("second"), 2),
];
for ele in views {
if let Some(f) = data.get_mut(&k) {
Into::<Vec<T>>::into(ele) // problem here, how to solve? plz help.
.iter()
.for_each(|e| (*f).push(Box::new((*e).clone())));
}
}
}
#[test]
pub fn test() {
let mut data: HashMap<String, Vec<Box<dyn Any>>> = HashMap::new();
data.insert(String::from("first"), Vec::new());
data.insert(String::from("second"), Vec::new());
convert::<First>(String::from("first"), &mut data)
}
}
fn convert<T: 'static + From<View>>(k: String, data: &mut HashMap<String, Vec<Box<dyn Any>>>) {
let views = vec![
View(String::from("first"), 1),
View(String::from("second"), 2),
];
for ele in views {
if let Some(f) = data.get_mut(&k) {
f.push(Box::new(T::from(ele.clone())) as Box<dyn Any>);
}
}
}
1 Like
thx for your help.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.