Skip to content

First pass at a select statement #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/context/macro_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::endpoint::ContextInternal;
use restate_sdk_shared_core::NotificationHandle;

// Sealed future trait, used by select statement
#[doc(hidden)]
pub trait SealedDurableFuture {
fn inner_context(&self) -> ContextInternal;
fn handle(&self) -> NotificationHandle;
}
18 changes: 13 additions & 5 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use crate::serde::{Deserialize, Serialize};
use std::future::Future;
use std::time::Duration;

#[doc(hidden)]
pub mod macro_support;
mod request;
mod run;
mod select;

pub use request::{CallFuture, InvocationHandle, Request, RequestTarget};
pub use run::{RunClosure, RunFuture, RunRetryPolicy};

Expand Down Expand Up @@ -249,7 +253,10 @@ impl<'ctx> WorkflowContext<'ctx> {
/// </details>
pub trait ContextTimers<'ctx>: private::SealedContext<'ctx> {
/// Sleep using Restate
fn sleep(&self, duration: Duration) -> impl Future<Output = Result<(), TerminalError>> + 'ctx {
fn sleep(
&self,
duration: Duration,
) -> impl DurableFuture<Output = Result<(), TerminalError>> + 'ctx {
private::SealedContext::inner_context(self).sleep(duration)
}
}
Expand Down Expand Up @@ -632,7 +639,7 @@ pub trait ContextAwakeables<'ctx>: private::SealedContext<'ctx> {
&self,
) -> (
String,
impl Future<Output = Result<T, TerminalError>> + Send + 'ctx,
impl DurableFuture<Output = Result<T, TerminalError>> + Send + 'ctx,
) {
self.inner_context().awakeable()
}
Expand Down Expand Up @@ -918,7 +925,7 @@ pub trait ContextPromises<'ctx>: private::SealedContext<'ctx> {
fn promise<T: Deserialize + 'static>(
&'ctx self,
key: &str,
) -> impl Future<Output = Result<T, TerminalError>> + 'ctx {
) -> impl DurableFuture<Output = Result<T, TerminalError>> + 'ctx {
self.inner_context().promise(key)
}

Expand Down Expand Up @@ -946,9 +953,10 @@ impl<'ctx, CTX: private::SealedContext<'ctx> + private::SealedCanUsePromises> Co
{
}

mod private {
use super::*;
pub trait DurableFuture: Future + macro_support::SealedDurableFuture {}

pub(crate) mod private {
use super::*;
pub trait SealedContext<'ctx> {
fn inner_context(&self) -> &'ctx ContextInternal;

Expand Down
10 changes: 8 additions & 2 deletions src/context/request.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::DurableFuture;

use crate::endpoint::ContextInternal;
use crate::errors::TerminalError;
use crate::serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -95,7 +97,7 @@ impl<'a, Req, Res> Request<'a, Req, Res> {
}

/// Call a service. This returns a future encapsulating the response.
pub fn call(self) -> impl CallFuture<Result<Res, TerminalError>> + Send
pub fn call(self) -> impl CallFuture<Response = Res> + Send
where
Req: Serialize + 'static,
Res: Deserialize + 'static,
Expand Down Expand Up @@ -132,4 +134,8 @@ pub trait InvocationHandle {
fn cancel(&self) -> impl Future<Output = Result<(), TerminalError>> + Send;
}

pub trait CallFuture<O>: Future<Output = O> + InvocationHandle {}
pub trait CallFuture:
DurableFuture<Output = Result<Self::Response, TerminalError>> + InvocationHandle
{
type Response;
}
Loading