use crate::IceBox;
use std::{
thread,
thread::JoinHandle
};
use std::sync::{
Arc, Mutex,
mpsc::Receiver
};
pub(crate) struct Heater {
core: Option<JoinHandle<()>>
}
impl Heater {
pub fn new(ice_boxes: Arc<Mutex<Receiver<IceBox>>>) -> Heater {
let core = thread::Builder::new().name("Heater".to_string()).spawn(move || {
loop {
let icebox = ice_boxes.lock().unwrap()
.recv().unwrap();
match icebox {
IceBox::Some(ice) => ice.melt(),
IceBox::None => break
}
}
}).expect("ice_threads::Heater::new -> Could not create a new instance");
Heater {
core: Some(core)
}
}
pub fn take_core(&mut self) -> Option<JoinHandle<()>> {
self.core.take()
}
}