This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(endpoint): reimplement combinators
- Loading branch information
1 parent
f767db8
commit 31da4e9
Showing
25 changed files
with
543 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::rc::Rc; | ||
use std::sync::Arc; | ||
|
||
pub trait Caller<T> { | ||
type Output; | ||
fn call(self, arg: T) -> Self::Output; | ||
} | ||
|
||
impl<F, T, R> Caller<T> for F | ||
where | ||
F: FnOnce(T) -> R, | ||
{ | ||
type Output = R; | ||
|
||
fn call(self, arg: T) -> Self::Output { | ||
(self)(arg) | ||
} | ||
} | ||
|
||
impl<F, T, R> Caller<T> for Rc<F> | ||
where | ||
F: Fn(T) -> R, | ||
{ | ||
type Output = R; | ||
|
||
fn call(self, arg: T) -> Self::Output { | ||
(*self)(arg) | ||
} | ||
} | ||
|
||
impl<F, T, R> Caller<T> for Arc<F> | ||
where | ||
F: Fn(T) -> R, | ||
{ | ||
type Output = R; | ||
|
||
fn call(self, arg: T) -> Self::Output { | ||
(*self)(arg) | ||
} | ||
} | ||
|
||
pub struct BoxedCaller<T: ?Sized>(Box<T>); | ||
|
||
impl<T: ?Sized> BoxedCaller<T> { | ||
pub fn new(caller: Box<T>) -> Self { | ||
BoxedCaller(caller) | ||
} | ||
} | ||
|
||
// TODO: FnBox? | ||
impl<F, T, R> Caller<T> for BoxedCaller<F> | ||
where | ||
F: FnMut(T) -> R, | ||
{ | ||
type Output = R; | ||
|
||
fn call(mut self, arg: T) -> Self::Output { | ||
(*self.0)(arg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use finchers_core::error::HttpError; | ||
use finchers_core::{Error, Input}; | ||
use futures::{Future, Poll}; | ||
use {Context, Endpoint, IntoEndpoint}; | ||
|
||
pub fn new<E>(endpoint: E) -> Abort<E::Endpoint> | ||
where | ||
E: IntoEndpoint, | ||
E::Item: HttpError, | ||
{ | ||
Abort { | ||
endpoint: endpoint.into_endpoint(), | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct Abort<E> { | ||
endpoint: E, | ||
} | ||
|
||
impl<E> Endpoint for Abort<E> | ||
where | ||
E: Endpoint, | ||
E::Item: HttpError, | ||
{ | ||
type Item = !; | ||
type Future = AbortFuture<E::Future>; | ||
|
||
fn apply(&self, input: &Input, ctx: &mut Context) -> Option<Self::Future> { | ||
let fut = self.endpoint.apply(input, ctx)?; | ||
Some(AbortFuture { fut }) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AbortFuture<T> { | ||
fut: T, | ||
} | ||
|
||
impl<T> Future for AbortFuture<T> | ||
where | ||
T: Future<Error = Error>, | ||
T::Item: HttpError, | ||
{ | ||
type Item = !; | ||
type Error = Error; | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
let item = try_ready!(self.fut.poll()); | ||
Err(Error::from(item).into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use finchers_core::error::HttpError; | ||
use finchers_core::{Caller, Error, Input}; | ||
use futures::{Future, Poll}; | ||
use {Context, Endpoint, IntoEndpoint}; | ||
|
||
pub fn new<E, F>(endpoint: E, f: F) -> AbortWith<E::Endpoint, F> | ||
where | ||
E: IntoEndpoint, | ||
F: Caller<E::Item> + Clone, | ||
F::Output: HttpError, | ||
{ | ||
AbortWith { | ||
endpoint: endpoint.into_endpoint(), | ||
f, | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct AbortWith<E, F> { | ||
endpoint: E, | ||
f: F, | ||
} | ||
|
||
impl<E, F> Endpoint for AbortWith<E, F> | ||
where | ||
E: Endpoint, | ||
F: Caller<E::Item> + Clone, | ||
F::Output: HttpError, | ||
{ | ||
type Item = !; | ||
type Future = AbortWithFuture<E::Future, F>; | ||
|
||
fn apply(&self, input: &Input, ctx: &mut Context) -> Option<Self::Future> { | ||
let fut = self.endpoint.apply(input, ctx)?; | ||
Some(AbortWithFuture { | ||
fut, | ||
f: Some(self.f.clone()), | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AbortWithFuture<T, F> { | ||
fut: T, | ||
f: Option<F>, | ||
} | ||
|
||
impl<T, F> Future for AbortWithFuture<T, F> | ||
where | ||
T: Future<Error = Error>, | ||
F: Caller<T::Item>, | ||
F::Output: HttpError, | ||
{ | ||
type Item = !; | ||
type Error = Error; | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
let item = try_ready!(self.fut.poll()); | ||
let f = self.f.take().expect("cannot resolve twice"); | ||
Err(f.call(item).into()) | ||
} | ||
} |
Oops, something went wrong.