Skip to content

Commit

Permalink
fix(tonic): make Interceptor UnwindSafe (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored May 12, 2021
1 parent b0ec3ea commit 57509d3
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions tonic/src/interceptor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use crate::{Request, Status};
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::{fmt, sync::Arc};

type InterceptorFn =
Arc<dyn Fn(Request<()>) -> Result<Request<()>, Status> + Send + Sync + 'static>;
type InterceptorFn = Arc<
dyn Fn(Request<()>) -> Result<Request<()>, Status>
+ Send
+ Sync
+ UnwindSafe
+ RefUnwindSafe
+ 'static,
>;

/// Represents a gRPC interceptor.
///
Expand All @@ -25,7 +32,12 @@ pub struct Interceptor {
impl Interceptor {
/// Create a new `Interceptor` from the provided function.
pub fn new(
f: impl Fn(Request<()>) -> Result<Request<()>, Status> + Send + Sync + 'static,
f: impl Fn(Request<()>) -> Result<Request<()>, Status>
+ Send
+ Sync
+ UnwindSafe
+ RefUnwindSafe
+ 'static,
) -> Self {
Interceptor { f: Arc::new(f) }
}
Expand All @@ -43,7 +55,12 @@ impl Interceptor {

impl<F> From<F> for Interceptor
where
F: Fn(Request<()>) -> Result<Request<()>, Status> + Send + Sync + 'static,
F: Fn(Request<()>) -> Result<Request<()>, Status>
+ Send
+ Sync
+ UnwindSafe
+ RefUnwindSafe
+ 'static,
{
fn from(f: F) -> Self {
Interceptor::new(f)
Expand All @@ -55,3 +72,15 @@ impl fmt::Debug for Interceptor {
f.debug_struct("Interceptor").finish()
}
}

#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;

#[test]
fn interceptor_fn_is_unwind_safe() {
fn is_unwind_safe<T: UnwindSafe + RefUnwindSafe>() {}
is_unwind_safe::<InterceptorFn>();
}
}

0 comments on commit 57509d3

Please sign in to comment.