Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::rc::Rc;
- use std::any::Any;
- trait Attribute: AsAny {
- fn memory_size(&self) -> u32 { 42 }
- }
- impl Attribute for (f32, f32) {}
- impl Attribute for u32 {}
- fn main() {
- let mut h = HashMap::<String, Rc<dyn Attribute>>::new();
- h.insert("foo".into(), Rc::new((22.0, 22.0)));
- h.insert("bar".into(), Rc::new(42));
- let size = h.values()
- .map(|rc| rc.memory_size())
- .sum::<u32>();
- if let Ok(attr) = h.get_mut("foo").unwrap()
- .clone().into_any_rc()
- .downcast::<(f32, f32)>()
- {
- dbg!(attr);
- }
- }
- trait AsAny {
- fn into_any_rc(self: Rc<Self>) -> Rc<dyn Any>;
- }
- impl<T: Any> AsAny for T {
- fn into_any_rc(self: Rc<Self>) -> Rc<dyn Any> {
- self
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement