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

chore(interceptor): Rename f with interceptor #2000

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
45 changes: 24 additions & 21 deletions tonic/src/service/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,69 +55,72 @@ where
/// Create a new interceptor layer.
///
/// See [`Interceptor`] for more details.
pub fn interceptor<F>(f: F) -> InterceptorLayer<F>
pub fn interceptor<I>(interceptor: I) -> InterceptorLayer<I>
where
F: Interceptor,
I: Interceptor,
{
InterceptorLayer { f }
InterceptorLayer { interceptor }
}

/// A gRPC interceptor that can be used as a [`Layer`],
/// created by calling [`interceptor`].
///
/// See [`Interceptor`] for more details.
#[derive(Debug, Clone, Copy)]
pub struct InterceptorLayer<F> {
f: F,
pub struct InterceptorLayer<I> {
interceptor: I,
}

impl<S, F> Layer<S> for InterceptorLayer<F>
impl<S, I> Layer<S> for InterceptorLayer<I>
where
F: Interceptor + Clone,
I: Interceptor + Clone,
{
type Service = InterceptedService<S, F>;
type Service = InterceptedService<S, I>;

fn layer(&self, service: S) -> Self::Service {
InterceptedService::new(service, self.f.clone())
InterceptedService::new(service, self.interceptor.clone())
}
}

/// A service wrapped in an interceptor middleware.
///
/// See [`Interceptor`] for more details.
#[derive(Clone, Copy)]
pub struct InterceptedService<S, F> {
pub struct InterceptedService<S, I> {
inner: S,
f: F,
interceptor: I,
}

impl<S, F> InterceptedService<S, F> {
impl<S, I> InterceptedService<S, I> {
/// Create a new `InterceptedService` that wraps `S` and intercepts each request with the
/// function `F`.
pub fn new(service: S, f: F) -> Self
pub fn new(service: S, interceptor: I) -> Self
where
F: Interceptor,
I: Interceptor,
{
Self { inner: service, f }
Self {
inner: service,
interceptor,
}
}
}

impl<S, F> fmt::Debug for InterceptedService<S, F>
impl<S, I> fmt::Debug for InterceptedService<S, I>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InterceptedService")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.field("f", &format_args!("{}", std::any::type_name::<I>()))
.finish()
}
}

impl<S, F, ReqBody, ResBody> Service<http::Request<ReqBody>> for InterceptedService<S, F>
impl<S, I, ReqBody, ResBody> Service<http::Request<ReqBody>> for InterceptedService<S, I>
where
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>>,
F: Interceptor,
I: Interceptor,
ResBody: Default,
{
type Response = http::Response<ResBody>;
Expand All @@ -142,7 +145,7 @@ where
let (metadata, extensions, msg) = req.into_parts();

match self
.f
.interceptor
.call(crate::Request::from_parts(metadata, extensions, ()))
{
Ok(req) => {
Expand All @@ -157,7 +160,7 @@ where
}

// required to use `InterceptedService` with `Router`
impl<S, F> crate::server::NamedService for InterceptedService<S, F>
impl<S, I> crate::server::NamedService for InterceptedService<S, I>
where
S: crate::server::NamedService,
{
Expand Down