1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 19:47:43 +02:00

service trait takes request type parameter (#232)

This commit is contained in:
Rob Ede
2020-12-27 04:28:00 +00:00
committed by GitHub
parent 518bf3f6a6
commit 3ab8c3eb69
28 changed files with 1142 additions and 1136 deletions

View File

@ -2,7 +2,10 @@ use std::collections::HashMap;
use std::{fmt, io, net};
use actix_rt::net::TcpStream;
use actix_service as actix;
use actix_service::{
fn_service, IntoServiceFactory as IntoBaseServiceFactory,
ServiceFactory as BaseServiceFactory,
};
use actix_utils::counter::CounterGuard;
use futures_util::future::{ok, Future, FutureExt, LocalBoxFuture};
use log::error;
@ -141,12 +144,10 @@ impl InternalServiceFactory for ConfiguredService {
let name = names.remove(&token).unwrap().0;
res.push((
token,
Box::new(StreamService::new(actix::fn_service(
move |_: TcpStream| {
error!("Service {:?} is not configured", name);
ok::<_, ()>(())
},
))),
Box::new(StreamService::new(fn_service(move |_: TcpStream| {
error!("Service {:?} is not configured", name);
ok::<_, ()>(())
}))),
));
};
}
@ -208,8 +209,8 @@ impl ServiceRuntime {
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
pub fn service<T, F>(&mut self, name: &str, service: F)
where
F: actix::IntoServiceFactory<T>,
T: actix::ServiceFactory<Config = (), Request = TcpStream> + 'static,
F: IntoBaseServiceFactory<T, TcpStream>,
T: BaseServiceFactory<TcpStream, Config = ()> + 'static,
T::Future: 'static,
T::Service: 'static,
T::InitError: fmt::Debug,
@ -237,8 +238,8 @@ impl ServiceRuntime {
}
type BoxedNewService = Box<
dyn actix::ServiceFactory<
Request = (Option<CounterGuard>, StdStream),
dyn BaseServiceFactory<
(Option<CounterGuard>, StdStream),
Response = (),
Error = (),
InitError = (),
@ -252,15 +253,14 @@ struct ServiceFactory<T> {
inner: T,
}
impl<T> actix::ServiceFactory for ServiceFactory<T>
impl<T> BaseServiceFactory<(Option<CounterGuard>, StdStream)> for ServiceFactory<T>
where
T: actix::ServiceFactory<Config = (), Request = TcpStream>,
T: BaseServiceFactory<TcpStream, Config = ()>,
T::Future: 'static,
T::Service: 'static,
T::Error: 'static,
T::InitError: fmt::Debug + 'static,
{
type Request = (Option<CounterGuard>, StdStream);
type Response = ();
type Error = ();
type Config = ();

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use std::task::{Context, Poll};
use actix_rt::spawn;
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
use actix_service::{Service, ServiceFactory as BaseServiceFactory};
use actix_utils::counter::CounterGuard;
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
use futures_util::{FutureExt, TryFutureExt};
@ -13,7 +13,7 @@ use super::Token;
use crate::socket::{FromStream, StdStream};
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
type Factory: actix::ServiceFactory<Config = (), Request = Stream>;
type Factory: BaseServiceFactory<Stream, Config = ()>;
fn create(&self) -> Self::Factory;
}
@ -28,31 +28,34 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box<
dyn Service<
Request = (Option<CounterGuard>, StdStream),
(Option<CounterGuard>, StdStream),
Response = (),
Error = (),
Future = Ready<Result<(), ()>>,
>,
>;
pub(crate) struct StreamService<T> {
service: T,
pub(crate) struct StreamService<S, I> {
service: S,
_phantom: PhantomData<I>,
}
impl<T> StreamService<T> {
pub(crate) fn new(service: T) -> Self {
StreamService { service }
impl<S, I> StreamService<S, I> {
pub(crate) fn new(service: S) -> Self {
StreamService {
service,
_phantom: PhantomData,
}
}
}
impl<T, I> Service for StreamService<T>
impl<S, I> Service<(Option<CounterGuard>, StdStream)> for StreamService<S, I>
where
T: Service<Request = I>,
T::Future: 'static,
T::Error: 'static,
S: Service<I>,
S::Future: 'static,
S::Error: 'static,
I: FromStream,
{
type Request = (Option<CounterGuard>, StdStream);
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;
@ -144,7 +147,7 @@ where
impl<F, T, I> ServiceFactory<I> for F
where
F: Fn() -> T + Send + Clone + 'static,
T: actix::ServiceFactory<Config = (), Request = I>,
T: BaseServiceFactory<I, Config = ()>,
I: FromStream,
{
type Factory = T;