First I defined a callback function
type CallbackFunction =
Box<dyn FnMut(&mut HumanVsMachineMatch) -> futures::future::BoxFuture<Box<dyn Any + Send>> + Send>;
Then defined execute
method, which will send the CallbackFunction
to a channel, and there is a dedicated thread will execute it.
pub async fn execute<T>(func: CallbackFunction) -> Box<T>
where
T: 'static,
{
let (sender, receiver) = oneshot::channel();
let tx = SENDER.lock().unwrap().clone();
let tx = tx.expect("Sender must not be None");
if let Err(e) = tx.send((func, sender)) {
panic!("Unable to send callback function on channel. {}", e);
}
let ret = receiver.await.expect("Unable to receive");
ret.downcast::<T>().expect("BUG! Unable to cast")
}
All above works fine. but I have problem to call the execute
method.
let board_info: Box<BoardInfo> =
execute(Box::new(move |m: &mut HumanVsMachineMatch| {
Box::pin(async {
if !black {
m.machine_move();
}
m.get_board()
})
}))
.await;
type mismatch resolving
<impl futures::Future<Output = BoardInfo> as futures::Future>::Output == Box<(dyn std::any::Any + std::marker::Send + 'static)>
expected structBox<(dyn std::any::Any + std::marker::Send + 'static)>
found structBoardInfo
required for the cast to the object typedyn futures::Future<Output = Box<(dyn std::any::Any + std::marker::Send + 'static)>> + std::marker::Send
Please how should I solve the error?