Skip to content

Commit

Permalink
async providers
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 8, 2024
1 parent 2d661be commit 8b75351
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
14 changes: 14 additions & 0 deletions v0.5/Cargo.lock

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

1 change: 1 addition & 0 deletions v0.5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ homepage = "https://fastn.com"
# using the latest dependency, and what is the plan to move to the latest version.

arcstr = "1"
async-trait = "0.1"
fastn-builtins = { path = "../fastn-builtins" }
fastn-compiler = { path = "fastn-compiler" }
fastn-continuation = { path = "fastn-continuation" }
Expand Down
4 changes: 4 additions & 0 deletions v0.5/fastn-continuation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ license.workspace = true
repository.workspace = true
homepage.workspace = true

[features]
async_provider = ["async-trait"]

[dependencies]
async-trait = { workspace = true, optional = true }
74 changes: 56 additions & 18 deletions v0.5/fastn-continuation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ pub trait Provider {
fn provide(&self, input: Self::Input) -> Self::Output;
}

#[cfg(feature = "async_provider")]
#[async_trait::async_trait]
pub trait AsyncProvider {
type Input;
type Output;

async fn provide(&self, input: Self::Input) -> Self::Output;
}

#[cfg(feature = "async_provider")]
#[async_trait::async_trait]
pub trait AsyncProviderWith {
type Input;
type Output;
type Context;

async fn provide(&self, context: &mut Self::Context, input: Self::Input) -> Self::Output;
}

pub trait ProviderWith {
type Input;
type Output;
Expand All @@ -18,22 +37,6 @@ pub trait ProviderWith {
fn provide(&self, context: &mut Self::Context, input: Self::Input) -> Self::Output;
}

// impl<I, O> Provider for dyn Fn(I) -> O {
// type Input = I;
// type Output = O;
// fn provide(&self, input: Self::Input) -> Self::Output {
// self(input)
// }
// }

// impl<I, O> Provider for fn(I) -> O {
// type Input = I;
// type Output = O;
// fn provide(&self, input: Self::Input) -> Self::Output {
// self(input)
// }
// }

pub trait Continuation {
type Output;
type NeededInput;
Expand Down Expand Up @@ -108,7 +111,24 @@ impl<C: Continuation> Result<C> {
}
}

pub async fn consume_async<Fut>(mut self, f: impl Fn(C::NeededInput) -> Fut) -> C::Output
#[cfg(feature = "async_provider")]
pub async fn consume_async<P>(mut self, p: P) -> C::Output
where
P: AsyncProvider<Input = C::NeededInput, Output = C::NeededOutput>,
{
loop {
match self {
Result::Stuck(ic, input) => {
self = ic.continue_after(p.provide(input).await);
}
Result::Done(c) => {
return c;
}
}
}
}

pub async fn consume_async_fn<Fut>(mut self, f: impl Fn(C::NeededInput) -> Fut) -> C::Output
where
Fut: std::future::Future<Output = C::NeededOutput>,
{
Expand All @@ -124,7 +144,25 @@ impl<C: Continuation> Result<C> {
}
}

pub async fn consume_with_async<Fut>(
#[cfg(feature = "async_provider")]
pub async fn consume_with_async<P>(mut self, p: P) -> C::Output
where
P: AsyncProviderWith<Input = C::NeededInput, Output = C::NeededOutput, Context = C>,
{
loop {
match self {
Result::Stuck(mut ic, input) => {
let o = p.provide(&mut ic, input).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::NeededInput) -> Fut,
) -> C::Output
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ async fn main() {
let command = fastn::commands::parse();
let mut package = fastn_package::Package::reader().consume_fn(fastn::full_filler);
let router = fastn_router::Router::reader()
.consume_async(fastn::full_filler_async)
.consume_async_fn(fastn::full_filler_async)
.await;
// read config here and pass to everyone?
// do common build stuff here
Expand Down

0 comments on commit 8b75351

Please sign in to comment.