-
Notifications
You must be signed in to change notification settings - Fork 5
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
5 changed files
with
95 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use futures::{SinkExt, StreamExt}; | ||
use tracing::error; | ||
|
||
use super::Literal; | ||
use crate::{directive::Functor, operation::Operation, runtime::Runtime, task::Task}; | ||
|
||
#[async_trait] | ||
impl<A: Send, B: Send + 'static> Functor<B> for Literal<A> { | ||
async fn f_map<Op: Operation<Input = A, Output = B>>( | ||
self, | ||
op: Op, | ||
runtime: &Runtime, | ||
) -> Result<Self::Target> { | ||
let (channel_identifier, mut sender, mut receiver) = | ||
runtime.lease_coordinated_task_channel::<Op, ()>().await?; | ||
|
||
let task = Task { | ||
routing_key: channel_identifier.clone(), | ||
metadata: (), | ||
op: op.clone(), | ||
input: self.0, | ||
}; | ||
|
||
sender.send(task).await?; | ||
sender.close().await?; | ||
|
||
if let Some((result, acker)) = receiver.next().await { | ||
match acker.ack().await { | ||
Ok(()) => return Ok(Literal(result.output)), | ||
// We don't error out on a failed ack, but filter out the message, as we assume | ||
// the message will be redelivered and we'll have another | ||
// opportunity to ack. | ||
Err(e) => { | ||
error!("Failed to ack result: {}", e); | ||
} | ||
} | ||
} | ||
|
||
anyhow::bail!("No results received") | ||
} | ||
} |
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,48 @@ | ||
/// A literal value. | ||
/// | ||
/// Can be used to execute operations over arbitrary values. | ||
/// | ||
/// ## Example | ||
/// ``` | ||
/// # use paladin::{ | ||
/// # operation::Operation, | ||
/// # directive::{Directive, Literal}, | ||
/// # opkind_derive::OpKind, | ||
/// # runtime::Runtime, | ||
/// # }; | ||
/// # use serde::{Deserialize, Serialize}; | ||
/// # use anyhow::Result; | ||
/// # | ||
/// # #[derive(Clone, Copy, Debug, Deserialize, Serialize)] | ||
/// struct MultiplyBy(i32); | ||
/// impl Operation for MultiplyBy { | ||
/// type Input = i32; | ||
/// type Output = i32; | ||
/// type Kind = MyOps; | ||
/// | ||
/// fn execute(&self, input: i32) -> Result<i32> { | ||
/// Ok(self.0 * input) | ||
/// } | ||
/// } | ||
/// # | ||
/// # #[derive(OpKind, Copy, Clone, Debug, Deserialize, Serialize)] | ||
/// # enum MyOps { | ||
/// # MultiplyBy(MultiplyBy), | ||
/// # } | ||
/// | ||
/// # #[tokio::main] | ||
/// # async fn main() -> Result<()> { | ||
/// # let runtime = Runtime::in_memory().await?; | ||
/// let computation = Literal(5).map(MultiplyBy(2)); | ||
/// let result = computation.run(&runtime).await?; | ||
/// assert_eq!(result, Literal(10)); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct Literal<T>(pub T); | ||
|
||
impl_hkt!(Literal); | ||
impl_lit!(Literal<T>); | ||
|
||
mod functor; |
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