Skip to content

Commit

Permalink
use \'asyc_trait in TryFutureExt
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Jul 24, 2019
1 parent 1191ca4 commit 5e054cb
Showing 1 changed file with 30 additions and 54 deletions.
84 changes: 30 additions & 54 deletions src/future.rs
Expand Up @@ -45,51 +45,17 @@ pub trait FutureExt: Future {
}
}

#[async_trait]
pub trait TryFuture: Future {
type Ok;
type Error;

async fn and_then<U, F, FutB>(self, f: F) -> Result<U, Self::Error>
where F: FnOnce(Self::Ok) -> FutB + Send,
FutB: Future<Output = Result<U, Self::Error>> + Send,
Self: Sized;

async fn or_else<U, F, FutB>(self, f: F) -> Result<Self::Ok, U>
where F: FnOnce(Self::Error) -> FutB + Send,
FutB: Future<Output = Result<Self::Ok, U>> + Send,
Self: Sized;

async fn map_ok<U, F>(self, f: F) -> Result<U, Self::Error>
where F: FnOnce(Self::Ok) -> U + Send,
Self: Sized;

async fn map_err<U, F>(self, f: F) -> Result<Self::Ok, U>
where F: FnOnce(Self::Error) -> U + Send,
Self: Sized;

async fn err_into<U>(self) -> Result<Self::Ok, U>
where Self::Error: Into<U>,
Self: Sized;

async fn unwrap_or_else<F>(self, f: F) -> Self::Ok
where F: FnOnce(Self::Error) -> Self::Ok + Send,
Self: Sized;
}
impl<T, E, Fut: ?Sized> TryFutureExt<T, E> for Fut where Fut: Future<Output = Result<T, E>> {}

#[async_trait]
impl<T, E, Fut> TryFuture for Fut
where Fut: ?Sized + Future<Output = Result<T, E>> + Send,
T: Send + 'static,
E: Send + 'static,
{
type Ok = T;
type Error = E;
pub trait TryFutureExt<T, E>: Future<Output = Result<T, E>> {

async fn and_then<U, F, FutB>(self, f: F) -> Result<U, Self::Error>
where F: FnOnce(Self::Ok) -> FutB + Send,
FutB: Future<Output = Result<U, Self::Error>> + Send,
Self: Sized
async fn and_then<U, F, FutB>(self, f: F) -> Result<U, E>
where F: FnOnce(T) -> FutB + Send,
FutB: Future<Output = Result<U, E>> + Send,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
match self.await {
Ok(ok) => {
Expand All @@ -100,10 +66,12 @@ impl<T, E, Fut> TryFuture for Fut
}
}

async fn or_else<U, F, FutB>(self, f: F) -> Result<Self::Ok, U>
where F: FnOnce(Self::Error) -> FutB + Send,
FutB: Future<Output = Result<Self::Ok, U>> + Send,
async fn or_else<U, F, FutB>(self, f: F) -> Result<T, U>
where F: FnOnce(E) -> FutB + Send,
FutB: Future<Output = Result<T, U>> + Send,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
match self.await {
Ok(ok) => Ok(ok),
Expand All @@ -114,30 +82,38 @@ impl<T, E, Fut> TryFuture for Fut
}
}

async fn map_ok<U, F>(self, f: F) -> Result<U, Self::Error>
where F: FnOnce(Self::Ok) -> U + Send,
Self: Sized
async fn map_ok<U, F>(self, f: F) -> Result<U, E>
where F: FnOnce(T) -> U + Send,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
self.await.map(f)
}

async fn map_err<U, F>(self, f: F) -> Result<Self::Ok, U>
where F: FnOnce(Self::Error) -> U + Send,
async fn map_err<U, F>(self, f: F) -> Result<T, U>
where F: FnOnce(E) -> U + Send,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
self.await.map_err(f)
}

async fn err_into<U>(self) -> Result<Self::Ok, U>
where Self::Error: Into<U>,
async fn err_into<U>(self) -> Result<T, U>
where E: Into<U>,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
self.await.map_err(Into::into)
}

async fn unwrap_or_else<F>(self, f: F) -> Self::Ok
where F: FnOnce(Self::Error) -> Self::Ok + Send,
async fn unwrap_or_else<F>(self, f: F) -> T
where F: FnOnce(E) -> T + Send,
Self: Sized,
T: Send + 'async_trait,
E: Send + 'async_trait,
{
self.await.unwrap_or_else(f)
}
Expand Down

0 comments on commit 5e054cb

Please sign in to comment.