///
/// When done, the 'complete()' method needs to be called to check for
/// outstanding errors.
-pub struct ParallelHandler<'a, I> {
+pub struct ParallelHandler<I> {
handles: Vec<JoinHandle<()>>,
name: String,
input: Option<SendHandle<I>>,
- _marker: std::marker::PhantomData<&'a ()>,
}
impl<I> Clone for SendHandle<I> {
}
}
-impl<'a, I: Send + 'static> ParallelHandler<'a, I> {
+impl<I: Send + 'static> ParallelHandler<I> {
/// Create a new thread pool, each thread processing incoming data
/// with 'handler_fn'.
pub fn new<F>(name: &str, threads: usize, handler_fn: F) -> Self
- where F: Fn(I) -> Result<(), Error> + Send + Clone + 'a,
+ where F: Fn(I) -> Result<(), Error> + Send + Clone + 'static,
{
let mut handles = Vec::new();
let (input_tx, input_rx) = bounded::<I>(threads);
for i in 0..threads {
let input_rx = input_rx.clone();
let abort = Arc::clone(&abort);
-
- // Erase the 'a lifetime bound. This is safe because we
- // join all thread in the drop handler.
- let handler_fn: Box<dyn Fn(I) -> Result<(), Error> + Send + 'a> =
- Box::new(handler_fn.clone());
- let handler_fn: Box<dyn Fn(I) -> Result<(), Error> + Send + 'static> =
- unsafe { std::mem::transmute(handler_fn) };
+ let handler_fn = handler_fn.clone();
handles.push(
std::thread::Builder::new()
input: input_tx,
abort,
}),
- _marker: std::marker::PhantomData,
}
}
let mut msg_list = Vec::new();
let mut i = 0;
- loop {
- let handle = match self.handles.pop() {
- Some(handle) => handle,
- None => break,
- };
+ while let Some(handle) = self.handles.pop() {
if let Err(panic) = handle.join() {
match panic.downcast::<&str>() {
Ok(panic_msg) => msg_list.push(
- format!("thread {} ({}) paniced: {}", self.name, i, panic_msg)
+ format!("thread {} ({}) panicked: {}", self.name, i, panic_msg)
),
Err(_) => msg_list.push(
- format!("thread {} ({}) paniced", self.name, i)
+ format!("thread {} ({}) panicked", self.name, i)
),
}
}
}
// Note: We make sure that all threads will be joined
-impl<'a, I> Drop for ParallelHandler<'a, I> {
+impl<I> Drop for ParallelHandler<I> {
fn drop(&mut self) {
drop(self.input.take());
while let Some(handle) = self.handles.pop() {