Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. use std::any::Any;
  2.  
  3. pub fn dedup_types(list: &mut Vec<Box<Any>>) {
  4. list.sort_unstable_by_key(|elem| elem.type_id());
  5. list.dedup_by_key(|elem| (*elem).type_id());
  6. }
  7.  
  8. struct Foo;
  9. struct Bar;
  10.  
  11. pub fn main() {
  12. let mut list = vec![
  13. Box::new(Foo) as Box<Any>,
  14. Box::new(Foo) as Box<Any>,
  15. Box::new(Foo) as Box<Any>,
  16. Box::new(Bar) as Box<Any>,
  17. Box::new(Bar) as Box<Any>,
  18. ];
  19.  
  20. dedup_types(&mut list);
  21. assert_eq!(list.len(), 2);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement