Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified Handle trait #1275

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future::Future;
use std::task::{Context, Poll};
use std::cell::{Ref, RefMut};
use std::rc::Rc;
use std::{fmt, net};
Expand All @@ -9,7 +11,9 @@ use actix_http::{
ResponseHead,
};
use actix_router::{IntoPattern, Path, Resource, ResourceDef, Url};
use actix_service::{IntoServiceFactory, ServiceFactory};
use actix_service::{IntoServiceFactory, ServiceFactory, Service};

use futures::future::{ok, Ready};

use crate::config::{AppConfig, AppService};
use crate::data::Data;
Expand Down Expand Up @@ -487,6 +491,58 @@ impl WebService {
guards: self.guards,
}
}

pub fn handler<S>(self, handler: S) -> impl HttpServiceFactory
where
S: Handle + Clone + 'static,
{

self.finish(Handler {
service: handler
})

}
}


pub trait Handle {
type Future: Future<Output= Result<ServiceResponse, Error>>;

fn call(&mut self, req: ServiceRequest) -> Self::Future;
}

#[derive(Clone)]
pub struct Handler<S: Handle + Clone> {
service: S
}

impl<S: Handle + Clone> Service for Handler<S> {
type Request = ServiceRequest;
type Response = ServiceResponse;
type Error = Error;
type Future = S::Future;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: ServiceRequest) -> Self::Future {
self.service.call(req)
}
}

impl <S: Handle + Clone> ServiceFactory for Handler<S> {
type Request = ServiceRequest;
type Response = ServiceResponse;
type Error = Error;
type Future = Ready<Result<Handler<S>, ()>>;
type Config = ();
type InitError = ();
type Service = Handler<S>;

fn new_service(&self, _config: ()) -> Self::Future {
ok(self.clone())
}
}

struct WebServiceImpl<T> {
Expand Down