Skip to content

Commit

Permalink
testing clippy's disallowed-types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Dec 30, 2023
1 parent 85573e0 commit de243d0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
89 changes: 89 additions & 0 deletions axum/src/box_clone_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use futures_util::future::BoxFuture;
use std::{
fmt,
task::{Context, Poll},
};
use tower::ServiceExt;
use tower_layer::{layer_fn, LayerFn};
use tower_service::Service;

/// Like `tower::BoxCloneService` but `Sync`
pub(crate) struct BoxCloneService<T, U, E>(
Box<
dyn CloneService<T, Response = U, Error = E, Future = BoxFuture<'static, Result<U, E>>>
+ Send
+ Sync,
>,
);

impl<T, U, E> BoxCloneService<T, U, E> {
pub(crate) fn new<S>(inner: S) -> Self
where
S: Service<T, Response = U, Error = E> + Clone + Send + Sync + 'static,
S::Future: Send + 'static,
{
let inner = inner.map_future(|f| Box::pin(f) as _);
BoxCloneService(Box::new(inner))
}

pub(crate) fn layer<S>() -> LayerFn<fn(S) -> Self>
where
S: Service<T, Response = U, Error = E> + Clone + Send + Sync + 'static,
S::Future: Send + 'static,
{
layer_fn(Self::new)
}
}

impl<T, U, E> Service<T> for BoxCloneService<T, U, E> {
type Response = U;
type Error = E;
type Future = BoxFuture<'static, Result<U, E>>;

#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
self.0.poll_ready(cx)
}

#[inline]
fn call(&mut self, request: T) -> Self::Future {
self.0.call(request)
}
}

impl<T, U, E> Clone for BoxCloneService<T, U, E> {
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}

trait CloneService<R>: Service<R> {
fn clone_box(
&self,
) -> Box<
dyn CloneService<R, Response = Self::Response, Error = Self::Error, Future = Self::Future>
+ Send
+ Sync,
>;
}

impl<R, T> CloneService<R> for T
where
T: Service<R> + Send + Sync + Clone + 'static,
{
fn clone_box(
&self,
) -> Box<
dyn CloneService<R, Response = T::Response, Error = T::Error, Future = T::Future>
+ Send
+ Sync,
> {
Box::new(self.clone())
}
}

impl<T, U, E> fmt::Debug for BoxCloneService<T, U, E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BoxCloneService").finish()
}
}
1 change: 1 addition & 0 deletions axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@
#[macro_use]
pub(crate) mod macros;

mod box_clone_service;
mod boxed;
mod extension;
#[cfg(feature = "form")]
Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disallowed-types = [
{ path = "tower::util::BoxCloneService", reason = "Use our internal BoxCloneService which is Sync" },
]

0 comments on commit de243d0

Please sign in to comment.