-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
264 additions
and
243 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,184 +1,22 @@ | ||
pub enum Result<C: Continuation + ?Sized> { | ||
Done(C::Output), | ||
Stuck(Box<C>, C::Needed), | ||
} | ||
|
||
pub trait Provider { | ||
type Needed; | ||
type Found; | ||
#![deny(unused_crate_dependencies)] | ||
#![allow(clippy::derive_partial_eq_without_eq, clippy::get_first)] | ||
#![warn(clippy::used_underscore_binding)] | ||
|
||
fn provide(&self, needed: Self::Needed) -> Self::Found; | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
#[async_trait::async_trait] | ||
pub trait AsyncProvider { | ||
type Needed; | ||
type Found; | ||
extern crate self as fastn_continuation; | ||
|
||
async fn provide(&self, needed: Self::Needed) -> Self::Found; | ||
} | ||
mod provider; | ||
mod result; | ||
mod ur; | ||
|
||
#[cfg(feature = "async_provider")] | ||
#[async_trait::async_trait] | ||
pub trait AsyncProviderWith { | ||
type Needed; | ||
type Found; | ||
type Context; | ||
|
||
async fn provide(&self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found; | ||
} | ||
|
||
pub trait ProviderWith { | ||
type Needed; | ||
type Found; | ||
type Context; | ||
|
||
fn provide(&self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found; | ||
} | ||
pub use provider::{AsyncProvider, AsyncProviderWith}; | ||
pub use provider::{Provider, ProviderWith}; | ||
pub use result::Result; | ||
pub use ur::UR; | ||
|
||
pub trait Continuation { | ||
type Output; | ||
type Needed; | ||
type Found; | ||
fn continue_after(self, found: Self::Found) -> Result<Self>; | ||
} | ||
|
||
impl<C: Continuation> Result<C> { | ||
pub fn consume<P>(mut self, p: P) -> C::Output | ||
where | ||
P: Provider<Needed = C::Needed, Found = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(p.provide(needed)); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_fn<F>(mut self, f: F) -> C::Output | ||
where | ||
F: Fn(C::Needed) -> C::Found, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(f(needed)); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_with<P>(mut self, p: P) -> C::Output | ||
where | ||
P: ProviderWith<Needed = C::Needed, Found = C::Found, Context = C>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(mut ic, needed) => { | ||
let o = p.provide(&mut ic, needed); | ||
self = ic.continue_after(o); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_with_fn<F>(mut self, f: F) -> C::Output | ||
where | ||
F: Fn(&mut C, C::Needed) -> C::Found, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(mut ic, needed) => { | ||
let o = f(&mut ic, needed); | ||
self = ic.continue_after(o); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
pub async fn consume_async<P>(mut self, p: P) -> C::Output | ||
where | ||
P: AsyncProvider<Needed = C::Needed, Found = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(p.provide(needed).await); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn consume_async_fn<Fut>(mut self, f: impl Fn(C::Needed) -> Fut) -> C::Output | ||
where | ||
Fut: std::future::Future<Output = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(f(needed).await); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
pub async fn consume_with_async<P>(mut self, p: P) -> C::Output | ||
where | ||
P: AsyncProviderWith<Needed = C::Needed, Found = C::Found, Context = C>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(mut ic, needed) => { | ||
let o = p.provide(&mut ic, needed).await; | ||
self = ic.continue_after(o); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn consume_with_async_fn<Fut>( | ||
mut self, | ||
f: impl Fn(&mut C, C::Needed) -> Fut, | ||
) -> C::Output | ||
where | ||
Fut: std::future::Future<Output = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(mut ic, needed) => { | ||
let o = f(&mut ic, needed).await; | ||
self = ic.continue_after(o); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
pub trait Provider { | ||
type Needed; | ||
type Found; | ||
|
||
fn provide(&self, needed: Self::Needed) -> Self::Found; | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
#[async_trait::async_trait] | ||
pub trait AsyncProvider { | ||
type Needed; | ||
type Found; | ||
|
||
async fn provide(&self, needed: Self::Needed) -> Self::Found; | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
#[async_trait::async_trait] | ||
pub trait AsyncProviderWith { | ||
type Needed; | ||
type Found; | ||
type Context; | ||
|
||
async fn provide(&self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found; | ||
} | ||
|
||
pub trait ProviderWith { | ||
type Needed; | ||
type Found; | ||
type Context; | ||
|
||
fn provide(&self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found; | ||
} |
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,143 @@ | ||
pub enum Result<C: fastn_continuation::Continuation + ?Sized> { | ||
Done(C::Output), | ||
Stuck(Box<C>, C::Needed), | ||
} | ||
|
||
impl<C: fastn_continuation::Continuation> Result<C> { | ||
pub fn consume<P>(mut self, p: P) -> C::Output | ||
where | ||
P: fastn_continuation::Provider<Needed = C::Needed, Found = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(p.provide(needed)); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_fn<F>(mut self, f: F) -> C::Output | ||
where | ||
F: Fn(C::Needed) -> C::Found, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(f(needed)); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_with<P>(mut self, p: P) -> C::Output | ||
where | ||
P: fastn_continuation::ProviderWith<Needed = C::Needed, Found = C::Found, Context = C>, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(mut ic, needed) => { | ||
let o = p.provide(&mut ic, needed); | ||
self = ic.continue_after(o); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn consume_with_fn<F>(mut self, f: F) -> C::Output | ||
where | ||
F: Fn(&mut C, C::Needed) -> C::Found, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(mut ic, needed) => { | ||
let o = f(&mut ic, needed); | ||
self = ic.continue_after(o); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
pub async fn consume_async<P>(mut self, p: P) -> C::Output | ||
where | ||
P: fastn_continuation::AsyncProvider<Needed = C::Needed, Found = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(p.provide(needed).await); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn consume_async_fn<Fut>(mut self, f: impl Fn(C::Needed) -> Fut) -> C::Output | ||
where | ||
Fut: std::future::Future<Output = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(ic, needed) => { | ||
self = ic.continue_after(f(needed).await); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "async_provider")] | ||
pub async fn consume_with_async<P>(mut self, p: P) -> C::Output | ||
where | ||
P: fastn_continuation::AsyncProviderWith<Needed = C::Needed, Found = C::Found, Context = C>, | ||
{ | ||
loop { | ||
match self { | ||
fastn_continuation::Result::Stuck(mut ic, needed) => { | ||
let o = p.provide(&mut ic, needed).await; | ||
self = ic.continue_after(o); | ||
} | ||
fastn_continuation::Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn consume_with_async_fn<Fut>( | ||
mut self, | ||
f: impl Fn(&mut C, C::Needed) -> Fut, | ||
) -> C::Output | ||
where | ||
Fut: std::future::Future<Output = C::Found>, | ||
{ | ||
loop { | ||
match self { | ||
Result::Stuck(mut ic, needed) => { | ||
let o = f(&mut ic, needed).await; | ||
self = ic.continue_after(o); | ||
} | ||
Result::Done(c) => { | ||
return c; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.