Skip to content

Commit

Permalink
[comm-lib] Add Send requirement for Defer closures
Browse files Browse the repository at this point in the history
Summary:
This is to make `Defer` work in fully asynchronous context. It was working in actix-web without this because every HTTP listener is single-threaded there. For tonic gRPC, closures must be `Send` in RPC handlers in order to pass them between `await` calls

Depends on D14281

Test Plan: cargo check

Reviewers: kamil, tomek

Reviewed By: kamil

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D14282
  • Loading branch information
barthap committed Feb 2, 2025
1 parent e113258 commit 5251555
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions shared/comm-lib/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ pub type BoxedError = Box<dyn std::error::Error>;
/// }
/// }
/// ```
pub struct Defer<'s>(Option<Box<dyn FnOnce() + 's>>);
pub struct Defer<'s>(Option<Box<dyn FnOnce() + 's + Send>>);

impl<'s> Defer<'s> {
pub fn new(f: impl FnOnce() + 's) -> Self {
pub fn new(f: impl FnOnce() + 's + Send) -> Self {
Self(Some(Box::new(f)))
}

pub fn empty() -> Self {
Self(None)
}

/// Consumes the value, without calling the provided function
///
/// # Example
Expand Down

0 comments on commit 5251555

Please sign in to comment.