Guest User

Untitled

a guest
May 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. use std::cell::{Ref, RefCell};
  2. use std::rc::Rc;
  3.  
  4. struct First {}
  5.  
  6. struct Second {}
  7.  
  8. enum TestEnum {
  9. First(First),
  10. Second(Second),
  11. }
  12.  
  13. struct FirstWrapper<'a> {
  14. first: &'a First,
  15. }
  16.  
  17. struct SecondWrapper<'a> {
  18. second: &'a Second,
  19. }
  20.  
  21. trait Wrapper<'a> {}
  22.  
  23. impl<'a> Wrapper<'a> for FirstWrapper<'a> {}
  24. impl<'a> Wrapper<'a> for SecondWrapper<'a> {}
  25.  
  26. fn wrap<'a>(val: Ref<'a, TestEnum>) -> Box<Wrapper<'a>> {
  27. match &*val {
  28. &TestEnum::First(ref first) => Box::new(FirstWrapper { first }),
  29. &TestEnum::Second(ref second) => Box::new(SecondWrapper { second }),
  30. }
  31. }
  32.  
  33. fn main() {
  34. let vec: Vec<Rc<RefCell<TestEnum>>> = vec![];
  35.  
  36. let borrowed: Vec<Box<Wrapper>> = vec.iter().map(|i| i.borrow()).map(wrap).collect::<Vec<_>>();
  37.  
  38. println!("{:?}", borrowed);
  39. }
Add Comment
Please, Sign In to add comment