Skip to content

Commit

Permalink
Fix worker
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Nov 18, 2024
1 parent c069f4f commit b5ffb7d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<W::Response>, 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
Expand Down Expand Up @@ -593,6 +608,11 @@ impl DefaultWorker {
}
}
}
impl AsRef<Worker<DefaultWorker>> for DefaultWorker {
fn as_ref(&self) -> &Worker<DefaultWorker> {
&self.0
}
}

/// Options for the default worker
#[derive(Default, Clone)]
Expand Down

0 comments on commit b5ffb7d

Please sign in to comment.