From 370cbd0c24991153e5300f51aba282b713efb8a0 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Tue, 3 Oct 2017 10:36:21 +0200 Subject: [PATCH] Add default implementations for dispatch methods in Resource trait --- src/web/resource.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/web/resource.rs b/src/web/resource.rs index d6ad882..373e43b 100644 --- a/src/web/resource.rs +++ b/src/web/resource.rs @@ -12,9 +12,34 @@ pub type ResponseFuture = Box Vec; - fn head(&self) -> ResponseFuture; - fn get(self: Box) -> ResponseFuture; - fn put(self: Box, body: hyper::Body) -> ResponseFuture; + + fn head(&self) -> ResponseFuture { + Box::new(futures::finished(self.method_not_allowed())) + } + + fn get(self: Box) -> ResponseFuture { + Box::new(futures::finished(self.method_not_allowed())) + } + + fn put(self: Box, body: hyper::Body) -> ResponseFuture + where Self: 'static + { + use futures::{Future, Stream}; + + // TODO Cleanup by moving to the built in never type, !, when it stabilizes + enum Never {}; + impl std::convert::From for hyper::Error { + fn from(_: Never) -> hyper::Error { + panic!() + } + } + + Box::new(body + .fold((), |_, _| -> Result<(), Never> { Ok(()) }) + .map_err(Into::into) + .and_then(move |_| futures::finished(self.method_not_allowed())) + ) + } fn options(&self) -> Response { Response::new()