mail_core/default_impl/
cpupool.rs

1
2use futures::Future;
3use utils::SendBoxFuture;
4
5use futures_cpupool::{ CpuPool, Builder};
6
7use context::OffloaderComponent;
8
9/// Create a `futures_cpupool::CpuPool` with default parameters.
10///
11/// Note that this crate implements `OffloaderComponent` for `CpuPool`.
12/// (Which is a component of the context used to build/encode mails and
13/// should not be confused with header components).
14pub fn simple_cpu_pool() -> CpuPool {
15    Builder::new().create()
16}
17
18impl OffloaderComponent for CpuPool {
19    /// executes the futures `fut` "elswhere" e.g. in a cpu pool
20    fn offload<F>(&self, fut: F) -> SendBoxFuture<F::Item, F::Error>
21        where F: Future + Send + 'static,
22              F::Item: Send+'static,
23              F::Error: Send+'static
24    {
25        Box::new( self.spawn( fut ) )
26    }
27}
28
29
30#[cfg(test)]
31mod test {
32    use futures::future;
33    use futures_cpupool::Builder;
34    use super::*;
35
36    #[test]
37    fn check_if_it_works() {
38        let pool = Builder::new().create();
39        _check_if_it_works( pool )
40    }
41
42    fn _check_if_it_works<R: OffloaderComponent>(r: R) {
43        let res = r.offload(future::lazy(||-> Result<u32,  ()> { Ok(33u32) } )).wait();
44        let val = assert_ok!( res );
45        assert_eq!( 33u32, val );
46    }
47}