Skip to content

Commit 0603e14

Browse files
authored
stack: Add NewService::into_make_service (#618)
This change adds a utility for casting a NewService into a MakeService.
1 parent 342ab79 commit 0603e14

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

linkerd/stack/src/new_service.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
use crate::FutureService;
2+
use futures::future;
3+
use linkerd2_error::Never;
4+
use std::task::{Context, Poll};
25
use tower::util::{Oneshot, ServiceExt};
36

47
/// Immediately and infalliby creates (usually) a Service.
58
pub trait NewService<T> {
69
type Service;
710

811
fn new_service(&self, target: T) -> Self::Service;
12+
13+
fn into_make_service(self) -> IntoMakeService<Self>
14+
where
15+
Self: Sized,
16+
{
17+
IntoMakeService { new_service: self }
18+
}
919
}
1020

1121
/// A Layer that modifies inner `MakeService`s to be exposd as a `NewService`.
@@ -18,6 +28,12 @@ pub struct FromMakeService<S> {
1828
make_service: S,
1929
}
2030

31+
/// Modifies inner `MakeService`s to be exposd as a `NewService`.
32+
#[derive(Clone, Copy, Debug)]
33+
pub struct IntoMakeService<N> {
34+
new_service: N,
35+
}
36+
2137
// === impl NewService ===
2238

2339
impl<F, T, S> NewService<T> for F
@@ -53,3 +69,17 @@ where
5369
FutureService::new(self.make_service.clone().oneshot(target))
5470
}
5571
}
72+
73+
impl<T, N: NewService<T>> tower::Service<T> for IntoMakeService<N> {
74+
type Response = N::Service;
75+
type Error = Never;
76+
type Future = future::Ready<Result<N::Service, Never>>;
77+
78+
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Never>> {
79+
Poll::Ready(Ok(()))
80+
}
81+
82+
fn call(&mut self, target: T) -> Self::Future {
83+
future::ok(self.new_service.new_service(target))
84+
}
85+
}

0 commit comments

Comments
 (0)