use crate::executor::CpuPool;
use std::sync::Arc;
use futures::Future;
pub struct AsyncBridge {
pool: Arc<CpuPool>,
}
impl AsyncBridge {
pub fn new(pool: Arc<CpuPool>) -> Self {
Self { pool }
}
pub fn execute<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static,
{
self.pool.execute(move || {
futures::executor::block_on(future);
});
}
pub fn spawn<F, T>(&self, future: F) -> async_channel::Receiver<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let (sender, receiver) = async_channel::bounded(1);
self.pool.execute(move || {
let result = futures::executor::block_on(future);
let _ = futures::executor::block_on(sender.send(result));
});
receiver
}
pub fn pool(&self) -> &Arc<CpuPool> {
&self.pool
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
#[test]
fn test_async_bridge() {
let config = Config::default();
let pool = CpuPool::new(&config).unwrap();
let bridge = AsyncBridge::new(Arc::new(pool));
let receiver = bridge.spawn(async { 42 });
let result = futures::executor::block_on(receiver.recv()).unwrap();
assert_eq!(result, 42);
}
}