Skip to content

Commit

Permalink
feat(interceptor): Change InterceptorLayer constructor associated fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
tottoto committed Oct 16, 2024
1 parent 6f7e35c commit 09ac20a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/src/tower/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let channel = ServiceBuilder::new()
// Interceptors can be also be applied as middleware
.layer(tonic::service::interceptor(intercept))
.layer(tonic::service::InterceptorLayer::new(intercept))
.layer_fn(AuthSvc::new)
.service(channel);

Expand Down
2 changes: 1 addition & 1 deletion examples/src/tower/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Apply our own middleware
.layer(MyMiddlewareLayer::default())
// Interceptors can be also be applied as middleware
.layer(tonic::service::interceptor(intercept))
.layer(tonic::service::InterceptorLayer::new(intercept))
.into_inner();

Server::builder()
Expand Down
10 changes: 10 additions & 0 deletions tonic/src/service/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ where
/// Create a new interceptor layer.
///
/// See [`Interceptor`] for more details.
#[deprecated(since = "0.12.4", note = "use `InterceptorLayer::new()` instead")]
pub fn interceptor<I>(interceptor: I) -> InterceptorLayer<I>
where
I: Interceptor,
Expand All @@ -71,6 +72,15 @@ pub struct InterceptorLayer<I> {
interceptor: I,
}

impl<I> InterceptorLayer<I> {
/// Create a new interceptor layer.
///
/// See [`Interceptor`] for more details.
pub fn new(interceptor: I) -> Self {
Self { interceptor }
}
}

impl<S, I> Layer<S> for InterceptorLayer<I>
where
I: Clone,
Expand Down
5 changes: 4 additions & 1 deletion tonic/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ pub(crate) mod layered;
pub(crate) mod router;

#[doc(inline)]
pub use self::interceptor::{interceptor, Interceptor};
#[allow(deprecated)]
pub use self::interceptor::interceptor;
#[doc(inline)]
pub use self::interceptor::{Interceptor, InterceptorLayer};
pub use self::layered::{LayerExt, Layered};
#[doc(inline)]
#[cfg(feature = "router")]
Expand Down

0 comments on commit 09ac20a

Please sign in to comment.