Skip to content

Commit 34a6951

Browse files
authored
add ServiceBuilder::boxed_clone_sync helper (#804)
1 parent 7dc533e commit 34a6951

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tower/src/builder/mod.rs

+62
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,68 @@ impl<L> ServiceBuilder<L> {
789789
{
790790
self.layer(crate::util::BoxCloneService::layer())
791791
}
792+
793+
/// This wraps the inner service with the [`Layer`] returned by [`BoxCloneSyncServiceLayer`].
794+
///
795+
/// This is similar to the [`boxed_clone`] method, but it requires that `Self` implement
796+
/// [`Sync`], and the returned boxed service implements [`Sync`].
797+
///
798+
/// See [`BoxCloneSyncService`] for more details.
799+
///
800+
/// # Example
801+
///
802+
/// ```
803+
/// use tower::{Service, ServiceBuilder, BoxError, util::BoxCloneSyncService};
804+
/// use std::time::Duration;
805+
/// #
806+
/// # struct Request;
807+
/// # struct Response;
808+
/// # impl Response {
809+
/// # fn new() -> Self { Self }
810+
/// # }
811+
///
812+
/// let service: BoxCloneSyncService<Request, Response, BoxError> = ServiceBuilder::new()
813+
/// .load_shed()
814+
/// .concurrency_limit(64)
815+
/// .timeout(Duration::from_secs(10))
816+
/// .boxed_clone_sync()
817+
/// .service_fn(|req: Request| async {
818+
/// Ok::<_, BoxError>(Response::new())
819+
/// });
820+
/// # let service = assert_service(service);
821+
///
822+
/// // The boxed service can still be cloned.
823+
/// service.clone();
824+
/// # fn assert_service<S, R>(svc: S) -> S
825+
/// # where S: Service<R> { svc }
826+
/// ```
827+
///
828+
/// [`BoxCloneSyncServiceLayer`]: crate::util::BoxCloneSyncServiceLayer
829+
/// [`BoxCloneSyncService`]: crate::util::BoxCloneSyncService
830+
/// [`boxed_clone`]: Self::boxed_clone
831+
#[cfg(feature = "util")]
832+
pub fn boxed_clone_sync<S, R>(
833+
self,
834+
) -> ServiceBuilder<
835+
Stack<
836+
crate::util::BoxCloneSyncServiceLayer<
837+
S,
838+
R,
839+
<L::Service as Service<R>>::Response,
840+
<L::Service as Service<R>>::Error,
841+
>,
842+
Identity,
843+
>,
844+
>
845+
where
846+
L: Layer<S> + Send + Sync + 'static,
847+
L::Service: Service<R> + Clone + Send + Sync + 'static,
848+
<L::Service as Service<R>>::Future: Send + Sync + 'static,
849+
{
850+
let layer = self.into_inner();
851+
852+
ServiceBuilder::new().layer(crate::util::BoxCloneSyncServiceLayer::new(layer))
853+
}
792854
}
793855

794856
impl<L: fmt::Debug> fmt::Debug for ServiceBuilder<L> {

0 commit comments

Comments
 (0)