From b5ffb7d7d2da72c58cc15d8832dc17e45a12e418 Mon Sep 17 00:00:00 2001 From: Richard Carson Date: Mon, 18 Nov 2024 16:07:47 -0500 Subject: [PATCH] Fix worker --- src/worker.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/worker.rs b/src/worker.rs index 61fcd33..a9c6835 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -253,6 +253,21 @@ where self.rx.recv().map_err(|e| Error::Runtime(e.to_string())) } + /// Try to receive a response from the worker without blocking + /// This will return `Ok(None)` if no response is available + /// + /// # Errors + /// Will return an error if the worker has already been stopped, or if the worker thread panicked + pub fn try_receive(&self) -> Result, Error> { + match self.rx.try_recv() { + Ok(v) => Ok(Some(v)), + Err(e) => match e { + std::sync::mpsc::TryRecvError::Empty => Ok(None), + std::sync::mpsc::TryRecvError::Disconnected => Err(Error::Runtime(e.to_string())), + }, + } + } + /// Send a request to the worker and wait for a response /// This will block the current thread until a response is received /// Will return an error if the worker has stopped or panicked @@ -593,6 +608,11 @@ impl DefaultWorker { } } } +impl AsRef> for DefaultWorker { + fn as_ref(&self) -> &Worker { + &self.0 + } +} /// Options for the default worker #[derive(Default, Clone)]