Skip to content

Commit

Permalink
factor out handle_resolve() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Sep 25, 2024
1 parent e4f7da7 commit 62f041d
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions capnp-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,44 @@ impl<VatId> ConnectionState<VatId> {
Ok(())
}

fn handle_resolve(connection_state: &Rc<Self>, resolve: resolve::Reader) -> capnp::Result<()> {
let replacement_or_error = match resolve.which()? {
resolve::Cap(c) => match Self::receive_cap(&connection_state, c?)? {

Check warning on line 818 in capnp-rpc/src/rpc.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> capnp-rpc/src/rpc.rs:818:56 | 818 | resolve::Cap(c) => match Self::receive_cap(&connection_state, c?)? { | ^^^^^^^^^^^^^^^^^ help: change this to: `connection_state` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Some(cap) => Ok(cap),
None => {
return Err(Error::failed(
"'Resolve' contained 'CapDescriptor.none'.".to_string(),
));
}
},
resolve::Exception(e) => {
// We can't set `replacement` to a new broken cap here because this will
// confuse PromiseClient::Resolve() into thinking that the remote
// promise resolved to a local capability and therefore a Disembargo is
// needed. We must actually reject the promise.
Err(remote_exception_to_error(e?))
}
};

// If the import is in the table, fulfill it.
let slots = &mut connection_state.imports.borrow_mut().slots;
if let Some(import) = slots.get_mut(&resolve.get_promise_id()) {
match import.promise_client_to_resolve.take() {
Some(weak_promise_client) => {
if let Some(promise_client) = weak_promise_client.upgrade() {
promise_client.borrow_mut().resolve(replacement_or_error);
}
}
None => {
return Err(Error::failed(
"Got 'Resolve' for a non-promise import.".to_string(),
));
}
}
}
Ok(())
}

fn handle_disembargo(
connection_state: &Rc<Self>,
disembargo: disembargo::Reader,
Expand Down Expand Up @@ -1103,43 +1141,7 @@ impl<VatId> ConnectionState<VatId> {
}
}
Ok(message::Finish(finish)) => Self::handle_finish(&connection_state, finish?)?,
Ok(message::Resolve(resolve)) => {
let resolve = resolve?;
let replacement_or_error = match resolve.which()? {
resolve::Cap(c) => match Self::receive_cap(&connection_state, c?)? {
Some(cap) => Ok(cap),
None => {
return Err(Error::failed(
"'Resolve' contained 'CapDescriptor.none'.".to_string(),
));
}
},
resolve::Exception(e) => {
// We can't set `replacement` to a new broken cap here because this will
// confuse PromiseClient::Resolve() into thinking that the remote
// promise resolved to a local capability and therefore a Disembargo is
// needed. We must actually reject the promise.
Err(remote_exception_to_error(e?))
}
};

// If the import is in the table, fulfill it.
let slots = &mut connection_state.imports.borrow_mut().slots;
if let Some(import) = slots.get_mut(&resolve.get_promise_id()) {
match import.promise_client_to_resolve.take() {
Some(weak_promise_client) => {
if let Some(promise_client) = weak_promise_client.upgrade() {
promise_client.borrow_mut().resolve(replacement_or_error);
}
}
None => {
return Err(Error::failed(
"Got 'Resolve' for a non-promise import.".to_string(),
));
}
}
}
}
Ok(message::Resolve(resolve)) => Self::handle_resolve(&connection_state, resolve?)?,
Ok(message::Release(release)) => {
let release = release?;
connection_state.release_export(release.get_id(), release.get_reference_count())?;
Expand Down

0 comments on commit 62f041d

Please sign in to comment.