I have a loop to fetch a new set of data, which will be inserted into a HashMap. Instead of clearing the old HashMap I have decided to go with allocating a brand new one and drop the previous.
Is what I'm doing actually safe and not leaking memory? My understanding is that I don't have to do an explicit drop the clear hashmap
#[derive(Clone, Deserialize)]
pub struct Message {
name: String,
data: String
}
pub struct Inbox {
data: Arc<Mutex<HashMap<String, Message>>>,
}
impl ChaosOracle {
pub fn get_message(self, id: &str) -> Option<Message> {
self.data.lock().unwrap().get(id).cloned()
}
pub fn fetch_messages_loop(&self, http_client: Client) {
let http_client = http_client.clone();
let data = self.data.clone();
tokio::spawn(async move {
loop {
if let Ok(new_result) = get_message(&http_client).await {
*data.lock().unwrap() = map_to_struct(&new_result);
}
sleep(Duration::from_millis(2000)).await
}
});
}
}