B:BD[
2.433] → [
2.433:527]
fn get(mutex: &Mutex<EventBuffer<String>>) -> State<String> {
mutex.lock().unwrap().get()
impl<T: PartialEq> EventDebouncer<T> {
fn new<F>(timeout: Duration, f: F) -> Self
where
F: Fn(T) -> (),
F: Send + 'static,
T: Send + 'static,
{
let mutex = Arc::new(Mutex::new(EventBuffer::new(timeout)));
let mutex_clone = mutex.clone();
let thread = thread::spawn(move || loop {
let state = { mutex_clone.lock().unwrap().get() };
match state {
State::Empty => thread::park(),
State::Wait(duration) => thread::sleep(duration),
State::Ready(data) => f(data),
}
});
Self { mutex, thread }
}
fn put(&self, data: T) {
self.mutex.lock().unwrap().put(data);
self.thread.thread().unpark();
}