1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#[cfg(feature = "constellation")]
mod process;
mod thread;
pub(crate) mod util;

use futures::future::{BoxFuture, TryFutureExt};
use serde_closure::traits;
use std::{error::Error, future::Future};

#[cfg(feature = "constellation")]
pub use process::ProcessPool;
pub use thread::ThreadPool;

use amadeus_core::pool::{
	ProcessPool as ProcessPoolTrait, ProcessSend, ThreadPool as ThreadPoolTrait
};

type Result<T> = std::result::Result<T, Box<dyn Error + Send>>;

#[cfg(feature = "constellation")]
#[cfg_attr(not(nightly), serde_closure::desugar)]
impl ProcessPoolTrait for ProcessPool {
	type ThreadPool = ThreadPool;

	fn processes(&self) -> usize {
		ProcessPool::processes(self)
	}
	fn spawn<F, Fut, T>(&self, work: F) -> BoxFuture<'static, Result<T>>
	where
		F: traits::FnOnce(&Self::ThreadPool) -> Fut + ProcessSend + 'static,
		Fut: Future<Output = T> + 'static,
		T: ProcessSend + 'static,
	{
		Box::pin(ProcessPool::spawn(self, work).map_err(|e| Box::new(e) as _))
	}
	#[allow(unsafe_code)]
	unsafe fn spawn_unchecked<'a, F, Fut, T>(&self, work: F) -> BoxFuture<'a, Result<T>>
	where
		F: traits::FnOnce(&Self::ThreadPool) -> Fut + ProcessSend + 'a,
		Fut: Future<Output = T> + 'a,
		T: ProcessSend + 'a,
	{
		Box::pin(ProcessPool::spawn_unchecked(self, work).map_err(|e| Box::new(e) as _))
	}
}

#[cfg_attr(not(nightly), serde_closure::desugar)]
impl ProcessPoolTrait for ThreadPool {
	type ThreadPool = Self;

	fn processes(&self) -> usize {
		1
	}
	fn spawn<F, Fut, T>(&self, work: F) -> BoxFuture<'static, Result<T>>
	where
		F: traits::FnOnce(&Self::ThreadPool) -> Fut + ProcessSend + 'static,
		Fut: Future<Output = T> + 'static,
		T: ProcessSend + 'static,
	{
		let self_ = self.clone();
		let spawn = move || work.call_once((&self_,));
		Box::pin(ThreadPool::spawn(self, spawn).map_err(|e| Box::new(e) as _))
	}
	#[allow(unsafe_code)]
	unsafe fn spawn_unchecked<'a, F, Fut, T>(&self, work: F) -> BoxFuture<'a, Result<T>>
	where
		F: traits::FnOnce(&Self::ThreadPool) -> Fut + ProcessSend + 'a,
		Fut: Future<Output = T> + 'a,
		T: ProcessSend + 'a,
	{
		let self_ = self.clone();
		let spawn = move || work.call_once((&self_,));
		Box::pin(ThreadPool::spawn_unchecked(self, spawn).map_err(|e| Box::new(e) as _))
	}
}

impl ThreadPoolTrait for ThreadPool {
	fn threads(&self) -> usize {
		ThreadPool::threads(self)
	}
	fn spawn<F, Fut, T>(&self, work: F) -> BoxFuture<'static, Result<T>>
	where
		F: FnOnce() -> Fut + Send + 'static,
		Fut: Future<Output = T> + 'static,
		T: Send + 'static,
	{
		Box::pin(ThreadPool::spawn(self, work).map_err(|e| Box::new(e) as _))
	}
	#[allow(unsafe_code)]
	unsafe fn spawn_unchecked<'a, F, Fut, T>(&self, work: F) -> BoxFuture<'a, Result<T>>
	where
		F: FnOnce() -> Fut + Send + 'a,
		Fut: Future<Output = T> + 'a,
		T: Send + 'a,
	{
		Box::pin(ThreadPool::spawn_unchecked(self, work).map_err(|e| Box::new(e) as _))
	}
}