Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc module): make async closures Clone #948

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/src/server/rpc_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,15 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> {
where
R: Serialize + Send + Sync + 'static,
Fut: Future<Output = Result<R, Error>> + Send,
Fun: (Fn(Params<'static>, Arc<Context>) -> Fut) + Copy + Send + Sync + 'static,
Fun: (Fn(Params<'static>, Arc<Context>) -> Fut) + Clone + Send + Sync + 'static,
{
let ctx = self.ctx.clone();
let callback = self.methods.verify_and_insert(
method_name,
MethodCallback::new_async(Arc::new(move |id, params, _, max_response_size, claimed| {
let ctx = ctx.clone();
let callback = callback.clone();

let future = async move {
let result = match callback(params, ctx).await {
Ok(res) => MethodResponse::response(id, res, max_response_size),
Expand Down Expand Up @@ -612,13 +614,14 @@ impl<Context: Send + Sync + 'static> RpcModule<Context> {
where
Context: Send + Sync + 'static,
R: Serialize,
F: Fn(Params, Arc<Context>) -> Result<R, Error> + Copy + Send + Sync + 'static,
F: Fn(Params, Arc<Context>) -> Result<R, Error> + Clone + Send + Sync + 'static,
{
let ctx = self.ctx.clone();
let callback = self.methods.verify_and_insert(
method_name,
MethodCallback::new_async(Arc::new(move |id, params, _, max_response_size, claimed| {
let ctx = ctx.clone();
let callback = callback.clone();

tokio::task::spawn_blocking(move || {
let result = match callback(params, ctx) {
Expand Down