mirror of
https://github.com/fafhrd91/actix-net
synced 2024-12-03 15:12:13 +01:00
13049b80ca
* Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
250 lines
5.5 KiB
Rust
250 lines
5.5 KiB
Rust
use std::future::Future;
|
|
use std::marker::PhantomData;
|
|
use std::pin::Pin;
|
|
use std::task::{Context, Poll};
|
|
|
|
use pin_project::pin_project;
|
|
|
|
use super::{Service, ServiceFactory};
|
|
|
|
/// Service for the `map_err` combinator, changing the type of a service's
|
|
/// error.
|
|
///
|
|
/// This is created by the `ServiceExt::map_err` method.
|
|
pub(crate) struct MapErr<A, F, E> {
|
|
service: A,
|
|
f: F,
|
|
_t: PhantomData<E>,
|
|
}
|
|
|
|
impl<A, F, E> MapErr<A, F, E> {
|
|
/// Create new `MapErr` combinator
|
|
pub fn new(service: A, f: F) -> Self
|
|
where
|
|
A: Service,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
Self {
|
|
service,
|
|
f,
|
|
_t: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> Clone for MapErr<A, F, E>
|
|
where
|
|
A: Clone,
|
|
F: Clone,
|
|
{
|
|
fn clone(&self) -> Self {
|
|
MapErr {
|
|
service: self.service.clone(),
|
|
f: self.f.clone(),
|
|
_t: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> Service for MapErr<A, F, E>
|
|
where
|
|
A: Service,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
type Request = A::Request;
|
|
type Response = A::Response;
|
|
type Error = E;
|
|
type Future = MapErrFuture<A, F, E>;
|
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
self.service.poll_ready(ctx).map_err(&self.f)
|
|
}
|
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
|
MapErrFuture::new(self.service.call(req), self.f.clone())
|
|
}
|
|
}
|
|
|
|
#[pin_project]
|
|
pub(crate) struct MapErrFuture<A, F, E>
|
|
where
|
|
A: Service,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
f: F,
|
|
#[pin]
|
|
fut: A::Future,
|
|
}
|
|
|
|
impl<A, F, E> MapErrFuture<A, F, E>
|
|
where
|
|
A: Service,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
MapErrFuture { f, fut }
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> Future for MapErrFuture<A, F, E>
|
|
where
|
|
A: Service,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
type Output = Result<A::Response, E>;
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
let this = self.project();
|
|
this.fut.poll(cx).map_err(this.f)
|
|
}
|
|
}
|
|
|
|
/// Factory for the `map_err` combinator, changing the type of a new
|
|
/// service's error.
|
|
///
|
|
/// This is created by the `NewServiceExt::map_err` method.
|
|
pub(crate) struct MapErrNewService<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
a: A,
|
|
f: F,
|
|
e: PhantomData<E>,
|
|
}
|
|
|
|
impl<A, F, E> MapErrNewService<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
/// Create new `MapErr` new service instance
|
|
pub(crate) fn new(a: A, f: F) -> Self {
|
|
Self {
|
|
a,
|
|
f,
|
|
e: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
|
where
|
|
A: ServiceFactory + Clone,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
fn clone(&self) -> Self {
|
|
Self {
|
|
a: self.a.clone(),
|
|
f: self.f.clone(),
|
|
e: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> ServiceFactory for MapErrNewService<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
type Request = A::Request;
|
|
type Response = A::Response;
|
|
type Error = E;
|
|
|
|
type Config = A::Config;
|
|
type Service = MapErr<A::Service, F, E>;
|
|
type InitError = A::InitError;
|
|
type Future = MapErrNewServiceFuture<A, F, E>;
|
|
|
|
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
|
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
|
}
|
|
}
|
|
|
|
#[pin_project]
|
|
pub(crate) struct MapErrNewServiceFuture<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
#[pin]
|
|
fut: A::Future,
|
|
f: F,
|
|
}
|
|
|
|
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E,
|
|
{
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
MapErrNewServiceFuture { f, fut }
|
|
}
|
|
}
|
|
|
|
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
|
where
|
|
A: ServiceFactory,
|
|
F: Fn(A::Error) -> E + Clone,
|
|
{
|
|
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
let this = self.project();
|
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
|
Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
|
|
} else {
|
|
Poll::Pending
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use futures::future::{err, lazy, ok, Ready};
|
|
|
|
use super::*;
|
|
use crate::{into_factory, into_service, Service};
|
|
|
|
struct Srv;
|
|
|
|
impl Service for Srv {
|
|
type Request = ();
|
|
type Response = ();
|
|
type Error = ();
|
|
type Future = Ready<Result<(), ()>>;
|
|
|
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
Poll::Ready(Err(()))
|
|
}
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
err(())
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_poll_ready() {
|
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
assert_eq!(res, Poll::Ready(Err("error")));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_call() {
|
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
|
let res = srv.call(()).await;
|
|
assert!(res.is_err());
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_new_service() {
|
|
let new_srv = into_factory(|| ok::<_, ()>(Srv)).map_err(|_| "error");
|
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
|
let res = srv.call(()).await;
|
|
assert!(res.is_err());
|
|
assert_eq!(res.err().unwrap(), "error");
|
|
}
|
|
}
|