Guest User

Untitled

a guest
Jul 19th, 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::collections::HashMap;
  2. use std::sync::{Arc, Weak};
  3.  
  4. trait MyTrait {}
  5.  
  6. #[derive(Clone)]
  7. struct MyRef {
  8. inner: Weak<MyTrait>,
  9. }
  10.  
  11. impl PartialEq for MyRef {
  12. fn eq(&self, other: &MyRef) -> bool {
  13. match (self.inner.upgrade(), other.inner.upgrade()) {
  14. (Some(ref me), Some(ref it)) => {
  15. Arc::ptr_eq(me, it)
  16. }
  17. _ => false,
  18. }
  19. }
  20. }
  21.  
  22. struct Lookup {
  23. inner: HashMap<String, MyRef>,
  24. }
  25.  
  26. impl Lookup {
  27. fn remove(&mut self, it: MyRef) {
  28. let matches: Vec<_> = self.inner
  29. .iter()
  30. .filter(|&(_, other)| *other == it)
  31. .map(|(k, _v)| k.clone())
  32. .collect();
  33. for m in matches {
  34. self.inner.remove(&m);
  35. }
  36. }
  37. }
  38.  
  39. fn main() {
  40.  
  41. }
Add Comment
Please, Sign In to add comment