1impl Pool {
2 pub fn scoped<'pool, 'scope, Scheduler>(&'pool self, scheduler: Scheduler)
3 where
4 Scheduler: FnOnce(&Scoped<'pool, 'scope>),
5 {
6 let scoped = Scoped::new(&self);
7 scheduler(&scoped);
8 }
9}
10
11#[derive(Debug)]
13pub struct Scoped<'pool, 'scope> {
14 pool: &'pool Pool,
15 tasks: Arc<()>,
16 maker: PhantomData<std::cell::Cell<&'scope mut ()>>,
17}
18
19impl<'pool, 'scope> Scoped<'pool, 'scope> {
20 fn new(pool: &'pool Pool) -> Self {
21 Self {
22 pool: pool,
23 maker: PhantomData,
24 tasks: Arc::default(),
25 }
26 }
27 pub fn push<T>(&self, task: T)
28 where
29 T: Runable + Send + 'scope,
30 {
31 let task = unsafe { transmute::<Box<Runable + Send + 'scope>, Box<Runable + Send + 'static>>(Box::new(task)) };
32 let arc = self.tasks.clone();
33
34 let task = move || {
35 let _arc = arc; task.call();
37 };
38 self.pool.push(task);
39 }
40}
41impl<'pool, 'scope> Drop for Scoped<'pool, 'scope> {
42 fn drop(&mut self) {
43 while Arc::strong_count(&self.tasks) > 1 {
44 thread::sleep(Duration::from_millis(10));
45 }
46 }
47}