1#![doc = include_str!("../docs/eval.md")]
2
3use crate::error::EvalError;
4use generational_box::GenerationalBox;
5use std::future::{poll_fn, Future, IntoFuture};
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9#[doc = include_str!("../docs/eval.md")]
10pub struct Eval {
11 evaluator: GenerationalBox<Box<dyn Evaluator>>,
12}
13
14impl Eval {
15 pub fn new(evaluator: GenerationalBox<Box<dyn Evaluator + 'static>>) -> Self {
17 Self { evaluator }
18 }
19
20 pub async fn join<T: serde::de::DeserializeOwned>(self) -> Result<T, EvalError> {
22 let json_value = poll_fn(|cx| match self.evaluator.try_write() {
23 Ok(mut evaluator) => evaluator.poll_join(cx),
24 Err(_) => Poll::Ready(Err(EvalError::Finished)),
25 })
26 .await?;
27 serde_json::from_value(json_value).map_err(EvalError::Serialization)
28 }
29
30 pub fn send(&self, data: impl serde::Serialize) -> Result<(), EvalError> {
32 match self.evaluator.try_read() {
33 Ok(evaluator) => {
34 evaluator.send(serde_json::to_value(data).map_err(EvalError::Serialization)?)
35 }
36 Err(_) => Err(EvalError::Finished),
37 }
38 }
39
40 pub async fn recv<T: serde::de::DeserializeOwned>(&mut self) -> Result<T, EvalError> {
42 let json_value = poll_fn(|cx| match self.evaluator.try_write() {
43 Ok(mut evaluator) => evaluator.poll_recv(cx),
44 Err(_) => Poll::Ready(Err(EvalError::Finished)),
45 })
46 .await?;
47 serde_json::from_value(json_value).map_err(EvalError::Serialization)
48 }
49}
50
51impl IntoFuture for Eval {
52 type Output = Result<serde_json::Value, EvalError>;
53 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
54
55 fn into_future(self) -> Self::IntoFuture {
56 Box::pin(self.join().into_future())
57 }
58}
59
60pub trait Evaluator {
62 fn send(&self, data: serde_json::Value) -> Result<(), EvalError>;
64 fn poll_recv(
66 &mut self,
67 context: &mut Context<'_>,
68 ) -> Poll<Result<serde_json::Value, EvalError>>;
69 fn poll_join(
71 &mut self,
72 context: &mut Context<'_>,
73 ) -> Poll<Result<serde_json::Value, EvalError>>;
74}