Guest User

Untitled

a guest
May 20th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::sync::Arc;
  3. use std::sync::Mutex;
  4.  
  5. #[derive(Default)]
  6. struct StringManagerPrimative {
  7. pub map: HashMap<String, String>,
  8. }
  9.  
  10. impl StringManagerPrimative {
  11. fn new() -> Self {
  12. StringManagerPrimative::default()
  13. }
  14.  
  15. fn ref_string(&mut self, stuff: String) {
  16. self.map.insert(stuff.clone(), stuff);
  17. }
  18.  
  19. fn unref(&mut self, stuff: String) {
  20. self.map.remove(&stuff);
  21. }
  22.  
  23. fn get_first_key(&self) -> Option<&String> {
  24. self.map.keys().nth(0)
  25. }
  26. }
  27.  
  28. #[derive(Default)]
  29. struct StringManager {
  30. primative: Arc<Mutex<StringManagerPrimative>>,
  31. }
  32.  
  33. impl StringManager {
  34. fn get<'a>(&mut self) -> Box<Iterator<Item = String> + 'a> {
  35. self.primative.lock().unwrap().ref_string("Hello".into());
  36. Box::new(It::new(self.primative.clone()))
  37. }
  38.  
  39. fn new() -> Self {
  40. StringManager::default()
  41. }
  42. }
  43.  
  44. struct It {
  45. primative: Arc<Mutex<StringManagerPrimative>>
  46. }
  47.  
  48. impl It {
  49. fn new(primative: Arc<Mutex<StringManagerPrimative>>) -> Self {
  50. It {
  51. primative,
  52. }
  53. }
  54. }
  55.  
  56. impl Drop for It {
  57. fn drop(&mut self) {
  58. self.primative.lock().unwrap().unref("Hello".into())
  59. }
  60. }
  61.  
  62. impl Iterator for It {
  63. type Item = String;
  64.  
  65. fn next(&mut self) -> Option<Self::Item> {
  66. self.primative.lock().unwrap().get_first_key().cloned()
  67. }
  68. }
  69.  
  70.  
  71. fn main() {
  72.  
  73. let mut string_manager = StringManager::new();
  74.  
  75. let mut iter = string_manager.get();
  76.  
  77. println!("{}", iter.next().unwrap())
  78.  
  79.  
  80.  
  81.  
  82.  
  83. }
Add Comment
Please, Sign In to add comment