Skip to content

Commit 9e9f5b4

Browse files
authored
Merge pull request #155 from Metaswitch/trait-composite-service
Use a trait to define CompositeService
2 parents 23a8ed6 + 0e42da9 commit 9e9f5b4

3 files changed

Lines changed: 40 additions & 98 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111

1212
### Fixed
1313

14+
## [6.2.0] - 2022-06-25
15+
### Added
16+
- Use a trait for connection on CompositeService to allow users to define
17+
their own supported connection types
18+
1419
## [6.1.1] - 2022-02-01
1520
### Fixed
1621
- Remove private, unused dependency on `chrono`
@@ -185,7 +190,8 @@ No changes. We now think we've got enough to declare this crate stable.
185190
## [0.5.0] - 2017-09-18
186191
- Start of changelog.
187192

188-
[Unreleased]: https://github.com/Metaswitch/swagger-rs/compare/6.1.1...HEAD
193+
[Unreleased]: https://github.com/Metaswitch/swagger-rs/compare/6.2.0...HEAD
194+
[6.2.0]: https://github.com/Metaswitch/swagger-rs/compare/6.1.1...6.2.0
189195
[6.1.1]: https://github.com/Metaswitch/swagger-rs/compare/6.1.0...6.1.1
190196
[6.1.0]: https://github.com/Metaswitch/swagger-rs/compare/6.0.0...6.1.0
191197
[6.0.0]: https://github.com/Metaswitch/swagger-rs/compare/5.1.0...6.0.0

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "swagger"
3-
version = "6.1.1"
3+
version = "6.2.0"
44
authors = ["Metaswitch Networks Ltd"]
55
license = "Apache-2.0"
66
description = "A set of common utilities for Rust code generated by swagger-codegen"

src/composites.rs

Lines changed: 32 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use hyper::service::Service;
77
use hyper::{Request, Response, StatusCode};
88
use std::fmt;
99
use std::future::Future;
10+
use std::net::SocketAddr;
1011
use std::ops::{Deref, DerefMut};
1112
use std::task::{Context, Poll};
1213

@@ -27,6 +28,32 @@ impl<B: Default> NotFound<B> for B {
2728
}
2829
}
2930

31+
/// Connection which has a remote address, which can thus be composited.
32+
pub trait HasRemoteAddr {
33+
/// Get the remote address for the connection to pass
34+
/// to the composited service
35+
fn remote_addr(&self) -> Option<SocketAddr>;
36+
}
37+
38+
impl<'a> HasRemoteAddr for &'a Option<SocketAddr> {
39+
fn remote_addr(&self) -> Option<SocketAddr> {
40+
**self
41+
}
42+
}
43+
44+
impl<'a> HasRemoteAddr for &'a hyper::server::conn::AddrStream {
45+
fn remote_addr(&self) -> Option<SocketAddr> {
46+
Some(hyper::server::conn::AddrStream::remote_addr(self))
47+
}
48+
}
49+
50+
#[cfg(feature = "uds")]
51+
impl HasRemoteAddr for &'a tokio::net::UnixStream {
52+
fn remote_addr(&self) -> Option<SocketAddr> {
53+
None
54+
}
55+
}
56+
3057
/// Trait implemented by services which can be composited.
3158
///
3259
/// Wraps tower_service::Service
@@ -151,98 +178,10 @@ where
151178
}
152179
}
153180

154-
use std::net::SocketAddr;
155-
156-
impl<'a, ReqBody, ResBody, Error, MakeError> Service<&'a Option<SocketAddr>>
157-
for CompositeMakeService<Option<SocketAddr>, ReqBody, ResBody, Error, MakeError>
158-
where
159-
ReqBody: 'static,
160-
ResBody: NotFound<ResBody> + 'static,
161-
MakeError: Send + 'static,
162-
Error: 'static,
163-
{
164-
type Error = MakeError;
165-
type Response = CompositeService<ReqBody, ResBody, Error>;
166-
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
167-
168-
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
169-
for service in &mut self.0 {
170-
match service.1.poll_ready(cx) {
171-
Poll::Ready(Ok(_)) => {}
172-
Poll::Ready(Err(e)) => {
173-
return Poll::Ready(Err(e));
174-
}
175-
Poll::Pending => {
176-
return Poll::Pending;
177-
}
178-
}
179-
}
180-
Poll::Ready(Ok(()))
181-
}
182-
183-
fn call(&mut self, target: &'a Option<SocketAddr>) -> Self::Future {
184-
let mut services = Vec::with_capacity(self.0.len());
185-
for (path, service) in &mut self.0 {
186-
let path: &'static str = path;
187-
services.push(service.call(*target).map_ok(move |s| (path, s)));
188-
}
189-
Box::pin(futures::future::join_all(services).map(|results| {
190-
let services: Result<Vec<_>, MakeError> = results.into_iter().collect();
191-
192-
Ok(CompositeService(services?))
193-
}))
194-
}
195-
}
196-
197-
impl<'a, ReqBody, ResBody, Error, MakeError> Service<&'a hyper::server::conn::AddrStream>
198-
for CompositeMakeService<Option<SocketAddr>, ReqBody, ResBody, Error, MakeError>
199-
where
200-
ReqBody: 'static,
201-
ResBody: NotFound<ResBody> + 'static,
202-
MakeError: Send + 'static,
203-
Error: 'static,
204-
{
205-
type Error = MakeError;
206-
type Response = CompositeService<ReqBody, ResBody, Error>;
207-
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
208-
209-
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
210-
for service in &mut self.0 {
211-
match service.1.poll_ready(cx) {
212-
Poll::Ready(Ok(_)) => {}
213-
Poll::Ready(Err(e)) => {
214-
return Poll::Ready(Err(e));
215-
}
216-
Poll::Pending => {
217-
return Poll::Pending;
218-
}
219-
}
220-
}
221-
Poll::Ready(Ok(()))
222-
}
223-
224-
fn call(&mut self, target: &'a hyper::server::conn::AddrStream) -> Self::Future {
225-
let mut services = Vec::with_capacity(self.0.len());
226-
for (path, service) in &mut self.0 {
227-
let path: &'static str = path;
228-
services.push(
229-
service
230-
.call(Some(target.remote_addr()))
231-
.map_ok(move |s| (path, s)),
232-
);
233-
}
234-
Box::pin(futures::future::join_all(services).map(|results| {
235-
let services: Result<Vec<_>, MakeError> = results.into_iter().collect();
236-
237-
Ok(CompositeService(services?))
238-
}))
239-
}
240-
}
241-
242-
#[cfg(feature = "uds")]
243-
impl<'a, ReqBody, ResBody, Error, MakeError> Service<&'a tokio::net::UnixStream>
181+
impl<ReqBody, ResBody, Error, MakeError, Connection> Service<Connection>
244182
for CompositeMakeService<Option<SocketAddr>, ReqBody, ResBody, Error, MakeError>
245183
where
184+
Connection: HasRemoteAddr,
246185
ReqBody: 'static,
247186
ResBody: NotFound<ResBody> + 'static,
248187
MakeError: Send + 'static,
@@ -267,15 +206,12 @@ where
267206
Poll::Ready(Ok(()))
268207
}
269208

270-
fn call(&mut self, _target: &'a tokio::net::UnixStream) -> Self::Future {
209+
fn call(&mut self, target: Connection) -> Self::Future {
271210
let mut services = Vec::with_capacity(self.0.len());
211+
let addr = target.remote_addr();
272212
for (path, service) in &mut self.0 {
273213
let path: &'static str = path;
274-
services.push(
275-
service
276-
.call(None)
277-
.map_ok(move |s| (path, s)),
278-
);
214+
services.push(service.call(addr).map_ok(move |s| (path, s)));
279215
}
280216
Box::pin(futures::future::join_all(services).map(|results| {
281217
let services: Result<Vec<_>, MakeError> = results.into_iter().collect();

0 commit comments

Comments
 (0)