Skip to content

Commit b2a2fb5

Browse files
committed
restore previous evalaute_impl in calculator example
Naively using async/await open the possibility of double borrows, as pointed out by clippy.
1 parent 76aab35 commit b2a2fb5

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

capnp-rpc/examples/calculator/server.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
use std::cell::RefCell;
2323

2424
use ::capnp::message::HeapAllocator;
25+
use capnp::capability::Promise;
2526
use capnp::primitive_list;
2627
use capnp::Error;
2728

2829
use ::capnp_rpc::ImbuedMessageBuilder;
29-
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
30+
use capnp_rpc::{pry, rpc_twoparty_capnp, twoparty, RpcSystem};
3031

3132
use crate::calculator_capnp::calculator;
3233

3334
use futures::future;
34-
use futures::{AsyncReadExt, TryFutureExt};
35+
use futures::{AsyncReadExt, FutureExt, TryFutureExt};
3536

3637
struct ValueImpl {
3738
value: f64,
@@ -54,31 +55,43 @@ impl calculator::value::Server for ValueImpl {
5455
}
5556
}
5657

57-
async fn evaluate_impl(
58+
fn evaluate_impl(
5859
expression: calculator::expression::Reader<'_>,
5960
params: Option<primitive_list::Reader<'_, f64>>,
60-
) -> Result<f64, Error> {
61-
match expression.which()? {
62-
calculator::expression::Literal(v) => Ok(v),
63-
calculator::expression::PreviousResult(p) => {
64-
let v = p?.read_request().send().promise.await?;
65-
Ok(v.get()?.get_value())
66-
}
61+
) -> Promise<f64, Error> {
62+
match pry!(expression.which()) {
63+
calculator::expression::Literal(v) => Promise::ok(v),
64+
calculator::expression::PreviousResult(p) => Promise::from_future(
65+
pry!(p)
66+
.read_request()
67+
.send()
68+
.promise
69+
.map(|v| Ok(v?.get()?.get_value())),
70+
),
71+
6772
calculator::expression::Parameter(p) => match params {
68-
Some(params) if p < params.len() => Ok(params.get(p)),
69-
_ => Err(Error::failed(format!("bad parameter: {p}"))),
73+
Some(params) if p < params.len() => Promise::ok(params.get(p)),
74+
_ => Promise::err(Error::failed(format!("bad parameter: {p}"))),
7075
},
76+
7177
calculator::expression::Call(call) => {
72-
let func = call.get_function()?;
73-
let eval_params =
74-
future::try_join_all(call.get_params()?.iter().map(|p| evaluate_impl(p, params)));
75-
let param_values = eval_params.await?;
76-
let mut request = func.call_request();
77-
let mut params = request.get().init_params(param_values.len() as u32);
78-
for (ii, value) in param_values.iter().enumerate() {
79-
params.set(ii as u32, *value);
80-
}
81-
Ok(request.send().promise.await?.get()?.get_value())
78+
let func = pry!(call.get_function());
79+
let eval_params = future::try_join_all(
80+
pry!(call.get_params())
81+
.iter()
82+
.map(|p| evaluate_impl(p, params)),
83+
);
84+
Promise::from_future(async move {
85+
let param_values = eval_params.await?;
86+
let mut request = func.call_request();
87+
{
88+
let mut params = request.get().init_params(param_values.len() as u32);
89+
for (ii, value) in param_values.iter().enumerate() {
90+
params.set(ii as u32, *value);
91+
}
92+
}
93+
Ok(request.send().promise.await?.get()?.get_value())
94+
})
8295
}
8396
}
8497
}
@@ -120,10 +133,9 @@ impl calculator::function::Server for FunctionImpl {
120133
.get_root::<calculator::expression::Builder>()?
121134
.into_reader(),
122135
Some(params),
123-
)
124-
.await?;
136+
);
125137

126-
results.get().set_value(eval);
138+
results.get().set_value(eval.await?);
127139
Ok(())
128140
}
129141
}

0 commit comments

Comments
 (0)