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: possible exception in request_context #5784

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion base_layer/service_framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! // At the same time receive the request and reply
//! async move {
//! let req_context = receiver.next().await.unwrap();
//! let msg = req_context.request().unwrap().clone();
//! let msg = req_context.request().clone();
//! req_context.reply(msg.to_uppercase());
//! }
//! );
Expand Down
22 changes: 5 additions & 17 deletions base_layer/service_framework/src/reply_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,37 +131,25 @@ impl<T> Future for TransportResponseFuture<T> {
/// request.
pub struct RequestContext<TReq, TResp> {
reply_tx: oneshot::Sender<TResp>,
request: Option<TReq>,
request: TReq,
}

impl<TReq, TResp> RequestContext<TReq, TResp> {
/// Create a new RequestContect
pub fn new(request: TReq, reply_tx: oneshot::Sender<TResp>) -> Self {
Self {
request: Some(request),
reply_tx,
}
Self { request, reply_tx }
}

/// Return a reference to the request object. None is returned after take_request has
/// been called.
pub fn request(&self) -> Option<&TReq> {
self.request.as_ref()
}

/// Take ownership of the request object, if ownership has not already been taken,
/// otherwise None is returned.
pub fn take_request(&mut self) -> Option<TReq> {
self.request.take()
pub fn request(&self) -> &TReq {
&self.request
}

/// Consume this object and return it's parts. Namely, the request object and
/// the reply oneshot channel.
pub fn split(self) -> (TReq, oneshot::Sender<TResp>) {
(
self.request.expect("RequestContext must be initialized with a request"),
self.reply_tx,
)
(self.request, self.reply_tx)
}

/// Sends a reply to the caller
Expand Down
Loading