Permalink
Browse files

ya dun goofed

  • Loading branch information...
Xion committed Apr 28, 2017
1 parent c0d31cb commit 19d37cef4bb2e917e54c862ef138c7da4378d03e
Showing with 15 additions and 56 deletions.
  1. +2 −2 src/caption/mod.rs
  2. +4 −44 src/ext.rs
  3. +9 −10 src/service.rs
View
@@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex, TryLockError};
use std::time::Duration;
use atomic::{Atomic, Ordering};
use futures::{Future, future};
use futures::{BoxFuture, Future, future};
use futures_cpupool::{self, CpuPool};
use hyper::StatusCode;
use image::{self, DynamicImage, FilterType, GenericImage};
@@ -97,7 +97,7 @@ impl Captioner {
impl Captioner {
/// Render an image macro as PNG.
/// The rendering is done in a separate thread.
pub fn render(&self, im: ImageMacro) -> Box<Future<Item=Vec<u8>, Error=CaptionError>> {
pub fn render(&self, im: ImageMacro) -> BoxFuture<Vec<u8>, CaptionError> {
let pool = match self.pool.try_lock() {
Ok(p) => p,
Err(TryLockError::WouldBlock) => {
View
@@ -1,61 +1,21 @@
//! Extension module, gluing together & enhancing the third-party libraries.
pub mod hyper {
use futures::{future, Stream};
use futures::{BoxFuture, future, Future, Stream};
use hyper::{Body, Error};
use super::futures::{ArcFuture, FutureExt};
/// Trait with additional methods for the Hyper Body object.
pub trait BodyExt {
fn into_bytes(self) -> ArcFuture<Vec<u8>, Error>;
fn into_bytes(self) -> BoxFuture<Vec<u8>, Error>;
}
impl BodyExt for Body {
fn into_bytes(self) -> ArcFuture<Vec<u8>, Error> {
fn into_bytes(self) -> BoxFuture<Vec<u8>, Error> {
self.fold(vec![], |mut buf, chunk| {
buf.extend_from_slice(&*chunk);
future::ok::<_, Error>(buf)
}).arc()
}
}
}
pub mod futures {
use std::sync::{Arc, Mutex, TryLockError};
use futures::{Async, Future, Poll};
/// Trait with additional methods for Futures.
pub trait FutureExt : Future + 'static {
fn arc(self) -> ArcFuture<Self::Item, Self::Error>;
}
impl<F: Future + 'static> FutureExt for F {
fn arc(self) -> ArcFuture<Self::Item, Self::Error> {
ArcFuture(Arc::new(Mutex::new(self)))
}
}
/// A type alias for Arc<Future>.
pub struct ArcFuture<T, E>(Arc<Mutex<Future<Item=T, Error=E>>>);
impl<T, E> Future for ArcFuture<T, E> {
type Item = T;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.0.try_lock() {
Ok(mut g) => g.poll(),
Err(TryLockError::WouldBlock) => Ok(Async::NotReady),
Err(TryLockError::Poisoned(e)) => {
panic!("ArcFuture mutex poisoned: {}", e);
},
}
}).boxed()
}
}
}
View
@@ -2,15 +2,14 @@
use std::error::Error;
use futures::{future, Future};
use futures::{BoxFuture, future, Future};
use hyper::{self, Get, Post, StatusCode};
use hyper::header::ContentType;
use hyper::server::{Service, Request, Response};
use serde_json;
use serde_qs;
use caption::{CAPTIONER, fonts, ImageMacro, templates};
use ext::futures::{ArcFuture, FutureExt};
use ext::hyper::BodyExt;
@@ -20,7 +19,7 @@ impl Service for Rofl {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = ArcFuture<Self::Response, Self::Error>;
type Future = BoxFuture<Self::Response, Self::Error>;
fn call(&self, req: Request) -> Self::Future {
// TODO: log the request after the response is served, in Common Log Format;
@@ -39,7 +38,7 @@ impl Service for Rofl {
let error_resp = match (req.method(), req.path()) {
_ => Response::new().with_status(StatusCode::NotFound),
};
future::ok(error_resp).arc()
future::ok(error_resp).boxed()
}
}
@@ -65,7 +64,7 @@ impl Rofl {
m => {
warn!("Unsupported HTTP method for caption request: {}", m);
return future::ok(
Response::new().with_status(StatusCode::MethodNotAllowed)).arc();
Response::new().with_status(StatusCode::MethodNotAllowed)).boxed();
},
};
@@ -74,7 +73,7 @@ impl Rofl {
Err(e) => {
error!("Failed to decode image macro: {}", e);
return future::ok(error_response(
StatusCode::BadRequest, "cannot decode request")).arc();
StatusCode::BadRequest, "cannot decode request")).boxed();
},
};
debug!("Decoded {:?}", im);
@@ -88,25 +87,25 @@ impl Rofl {
.or_else(|e| future::ok(
error_response(e.status_code(), format!("{}", e))
))
.arc()
.boxed()
})
.arc()
.boxed()
}
/// Handle the template listing request.
fn handle_list_templates(&self, _: Request) -> <Self as Service>::Future {
let template_names = templates::list();
let response = Response::new()
.with_body(json!(template_names).to_string());
future::ok(response).arc()
future::ok(response).boxed()
}
/// Handle the font listing request.
fn handle_list_fonts(&self, _: Request) -> <Self as Service>::Future {
let font_names = fonts::list();
let response = Response::new()
.with_body(json!(font_names).to_string());
future::ok(response).arc()
future::ok(response).boxed()
}
}

0 comments on commit 19d37ce

Please sign in to comment.