Skip to content

Commit

Permalink
UR belongs to fastn_continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 8, 2024
1 parent ba2ba11 commit 3a15d09
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 243 deletions.
1 change: 1 addition & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Compiler {
false
};
if resolved {
ci.resolve_it(&self.arena)
fastn_unresolved::resolve_it(&mut ci, &self.arena)
}
new_content.push(ci);
}
Expand Down
184 changes: 11 additions & 173 deletions v0.5/fastn-continuation/src/lib.rs
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;
}
}
}
}
}
33 changes: 33 additions & 0 deletions v0.5/fastn-continuation/src/provider.rs
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;
}
143 changes: 143 additions & 0 deletions v0.5/fastn-continuation/src/result.rs
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;
}
}
}
}
}
Loading

0 comments on commit 3a15d09

Please sign in to comment.