Skip to content

Commit 9201bfb

Browse files
authored
Add Service impl for Pipeline, PipelineSvc (#694)
1 parent e6f62d4 commit 9201bfb

File tree

6 files changed

+145
-6
lines changed

6 files changed

+145
-6
lines changed

.github/workflows/windows.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ jobs:
6060

6161
- name: Install OpenSSL
6262
run: |
63+
$env:VCPKG_MANIFEST_MODE = 'OFF'
6364
vcpkg integrate install
64-
vcpkg install openssl:x64-windows
65+
vcpkg install --clean-after-build openssl:x64-windows
6566
Copy-Item C:\vcpkg\installed\x64-windows\bin\libcrypto-3-x64.dll C:\vcpkg\installed\x64-windows\bin\libcrypto.dll
6667
Copy-Item C:\vcpkg\installed\x64-windows\bin\libssl-3-x64.dll C:\vcpkg\installed\x64-windows\bin\libssl.dll
6768
Get-ChildItem C:\vcpkg\installed\x64-windows\bin

ntex-service/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [3.8.0] - 2025-12-16
4+
5+
* Add Service impl for Pipeline, PipelineSvc
6+
37
## [3.7.3] - 2025-12-14
48

59
* Fix handling nested readiness checks

ntex-service/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-service"
3-
version = "3.7.3"
3+
version = "3.8.0"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "ntex service"
66
keywords = ["network", "framework", "async", "futures"]
@@ -25,5 +25,5 @@ slab = { workspace = true }
2525
foldhash = { workspace = true }
2626

2727
[dev-dependencies]
28-
ntex = "3.0.0-pre.6"
28+
ntex = "3.0.0-pre.7"
2929
ntex-util = "3"

ntex-service/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
3333
pub use self::fn_shutdown::fn_shutdown;
3434
pub use self::map_config::{map_config, unit_config};
3535
pub use self::middleware::{Identity, Middleware, Middleware2, Stack, apply, apply2};
36-
pub use self::pipeline::{Pipeline, PipelineBinding, PipelineCall};
36+
pub use self::pipeline::{Pipeline, PipelineBinding, PipelineCall, PipelineSvc};
3737

3838
#[allow(unused_variables)]
3939
/// An asynchronous function of `Request` to a `Response`.

ntex-service/src/pipeline.rs

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{cell, fmt, future::Future, marker, pin::Pin, rc::Rc, task::Context, task::Poll};
22

3-
use crate::{Service, ServiceCtx, ctx::WaitersRef};
3+
use crate::{IntoService, Service, ServiceCtx, ctx::WaitersRef};
44

55
#[derive(Debug)]
66
/// Container for a service.
@@ -167,6 +167,79 @@ impl<S: fmt::Debug> fmt::Debug for PipelineState<S> {
167167
}
168168
}
169169

170+
#[derive(Debug)]
171+
/// Service wrapper for Pipeline
172+
pub struct PipelineSvc<S> {
173+
inner: Pipeline<S>,
174+
}
175+
176+
impl<S> PipelineSvc<S> {
177+
#[inline]
178+
/// Construct new PipelineSvc
179+
pub fn new(inner: Pipeline<S>) -> Self {
180+
Self { inner }
181+
}
182+
}
183+
184+
impl<S, Req> Service<Req> for PipelineSvc<S>
185+
where
186+
S: Service<Req>,
187+
{
188+
type Response = S::Response;
189+
type Error = S::Error;
190+
191+
#[inline]
192+
async fn call(
193+
&self,
194+
req: Req,
195+
_: ServiceCtx<'_, Self>,
196+
) -> Result<Self::Response, Self::Error> {
197+
self.inner.call(req).await
198+
}
199+
200+
#[inline]
201+
async fn ready(&self, _: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
202+
self.inner.ready().await
203+
}
204+
205+
#[inline]
206+
async fn shutdown(&self) {
207+
self.inner.shutdown().await
208+
}
209+
210+
#[inline]
211+
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
212+
self.inner.poll(cx)
213+
}
214+
}
215+
216+
impl<S> From<S> for PipelineSvc<S> {
217+
#[inline]
218+
fn from(svc: S) -> Self {
219+
PipelineSvc {
220+
inner: Pipeline::new(svc),
221+
}
222+
}
223+
}
224+
225+
impl<S> Clone for PipelineSvc<S> {
226+
fn clone(&self) -> Self {
227+
PipelineSvc {
228+
inner: self.inner.clone(),
229+
}
230+
}
231+
}
232+
233+
impl<S, R> IntoService<PipelineSvc<S>, R> for Pipeline<S>
234+
where
235+
S: Service<R>,
236+
{
237+
#[inline]
238+
fn into_service(self) -> PipelineSvc<S> {
239+
PipelineSvc::new(self)
240+
}
241+
}
242+
170243
/// Bound container for a service.
171244
pub struct PipelineBinding<S, R>
172245
where
@@ -421,3 +494,64 @@ where
421494
})
422495
}
423496
}
497+
498+
#[cfg(test)]
499+
mod tests {
500+
use std::{cell::Cell, rc::Rc};
501+
502+
use super::*;
503+
504+
#[derive(Debug, Default, Clone)]
505+
struct Srv(Rc<Cell<usize>>);
506+
507+
impl Service<()> for Srv {
508+
type Response = ();
509+
type Error = ();
510+
511+
async fn ready(&self, _: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
512+
Ok(())
513+
}
514+
515+
async fn call(&self, _: (), _: ServiceCtx<'_, Self>) -> Result<(), ()> {
516+
Ok(())
517+
}
518+
519+
async fn shutdown(&self) {
520+
self.0.set(self.0.get() + 1);
521+
}
522+
}
523+
524+
#[ntex::test]
525+
async fn pipeline_service() {
526+
let cnt_sht = Rc::new(Cell::new(0));
527+
let srv = Pipeline::new(
528+
Pipeline::new(Srv(cnt_sht.clone()).map(|_| "ok"))
529+
.into_service()
530+
.clone(),
531+
);
532+
let res = srv.call(()).await;
533+
assert!(res.is_ok());
534+
assert_eq!(res.unwrap(), "ok");
535+
536+
let res = srv.ready().await;
537+
assert_eq!(res, Ok(()));
538+
539+
srv.shutdown().await;
540+
assert_eq!(cnt_sht.get(), 1);
541+
let _ = format!("{srv:?}");
542+
543+
let cnt_sht = Rc::new(Cell::new(0));
544+
let svc = Srv(cnt_sht.clone()).map(|_| "ok");
545+
let srv = Pipeline::new(PipelineSvc::from(&svc));
546+
let res = srv.call(()).await;
547+
assert!(res.is_ok());
548+
assert_eq!(res.unwrap(), "ok");
549+
550+
let res = srv.ready().await;
551+
assert_eq!(res, Ok(()));
552+
553+
srv.shutdown().await;
554+
assert_eq!(cnt_sht.get(), 1);
555+
let _ = format!("{srv:?}");
556+
}
557+
}

ntex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ no-test-logging = []
7070
ntex-codec = "1"
7171
ntex-http = "1"
7272
ntex-router = "1"
73-
ntex-service = "3.7.3"
73+
ntex-service = "3.8.0"
7474
ntex-macros = "0.2"
7575
ntex-util = "3.3"
7676
ntex-bytes = "1"

0 commit comments

Comments
 (0)