Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::sync::Arc;
- use std::sync::Mutex;
- #[derive(Default)]
- struct StringManagerPrimative {
- pub map: HashMap<String, String>,
- }
- impl StringManagerPrimative {
- fn new() -> Self {
- StringManagerPrimative::default()
- }
- fn ref_string(&mut self, stuff: String) {
- self.map.insert(stuff.clone(), stuff);
- }
- fn unref(&mut self, stuff: String) {
- self.map.remove(&stuff);
- }
- fn get_first_key(&self) -> Option<&String> {
- self.map.keys().nth(0)
- }
- }
- #[derive(Default)]
- struct StringManager {
- primative: Arc<Mutex<StringManagerPrimative>>,
- }
- impl StringManager {
- fn get<'a>(&mut self) -> Box<Iterator<Item = String> + 'a> {
- self.primative.lock().unwrap().ref_string("Hello".into());
- Box::new(It::new(self.primative.clone()))
- }
- fn new() -> Self {
- StringManager::default()
- }
- }
- struct It {
- primative: Arc<Mutex<StringManagerPrimative>>
- }
- impl It {
- fn new(primative: Arc<Mutex<StringManagerPrimative>>) -> Self {
- It {
- primative,
- }
- }
- }
- impl Drop for It {
- fn drop(&mut self) {
- self.primative.lock().unwrap().unref("Hello".into())
- }
- }
- impl Iterator for It {
- type Item = String;
- fn next(&mut self) -> Option<Self::Item> {
- self.primative.lock().unwrap().get_first_key().cloned()
- }
- }
- fn main() {
- let mut string_manager = StringManager::new();
- let mut iter = string_manager.get();
- println!("{}", iter.next().unwrap())
- }
Add Comment
Please, Sign In to add comment