Skip to content

Commit c2cf4af

Browse files
Rust docs
1 parent a08ee31 commit c2cf4af

File tree

15 files changed

+250
-7
lines changed

15 files changed

+250
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Documentation](https://img.shields.io/badge/doc-reference-blue)](https://docs.restate.dev)
1+
[![Documentation](https://img.shields.io/docsrs/restate-sdk)](https://docs.rs/restate-sdk)
22
[![crates.io](https://img.shields.io/crates/v/restate_sdk.svg)](https://crates.io/crates/restate-sdk/)
33
[![Examples](https://img.shields.io/badge/view-examples-blue)](https://github.com/restatedev/examples)
44
[![Discord](https://img.shields.io/discord/1128210118216007792?logo=discord)](https://discord.gg/skW3AZ6uGd)

macros/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// the Business Source License, use of this software will be governed
99
// by the Apache License, Version 2.0.
1010

11-
//! Some parts copied from https://github.com/dtolnay/thiserror/blob/39aaeb00ff270a49e3c254d7b38b10e934d3c7a5/impl/src/ast.rs
12-
//! License Apache-2.0 or MIT
11+
// Some parts copied from https://github.com/dtolnay/thiserror/blob/39aaeb00ff270a49e3c254d7b38b10e934d3c7a5/impl/src/ast.rs
12+
// License Apache-2.0 or MIT
1313

1414
use syn::ext::IdentExt;
1515
use syn::parse::{Parse, ParseStream};

macros/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// the Business Source License, use of this software will be governed
99
// by the Apache License, Version 2.0.
1010

11-
//! Some parts of this codebase were taken from https://github.com/google/tarpc/blob/b826f332312d3702667880a464e247556ad7dbfe/plugins/src/lib.rs
12-
//! License MIT
11+
// Some parts of this codebase were taken from https://github.com/google/tarpc/blob/b826f332312d3702667880a464e247556ad7dbfe/plugins/src/lib.rs
12+
// License MIT
1313

1414
extern crate proc_macro;
1515

@@ -22,6 +22,7 @@ use proc_macro::TokenStream;
2222
use quote::ToTokens;
2323
use syn::parse_macro_input;
2424

25+
/// Entry-point macro to define a Restate service.
2526
#[proc_macro_attribute]
2627
pub fn service(_: TokenStream, input: TokenStream) -> TokenStream {
2728
let svc = parse_macro_input!(input as Service);
@@ -31,6 +32,7 @@ pub fn service(_: TokenStream, input: TokenStream) -> TokenStream {
3132
.into()
3233
}
3334

35+
/// Entry-point macro to define a Restate object.
3436
#[proc_macro_attribute]
3537
pub fn object(_: TokenStream, input: TokenStream) -> TokenStream {
3638
let svc = parse_macro_input!(input as Object);
@@ -40,6 +42,7 @@ pub fn object(_: TokenStream, input: TokenStream) -> TokenStream {
4042
.into()
4143
}
4244

45+
/// Entry-point macro to define a Restate workflow.
4346
#[proc_macro_attribute]
4447
pub fn workflow(_: TokenStream, input: TokenStream) -> TokenStream {
4548
let svc = parse_macro_input!(input as Workflow);

src/context/mod.rs

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Types exposing Restate functionalities to service handlers.
2+
13
use crate::endpoint::{ContextInternal, InputMetadata};
24
use crate::errors::{HandlerResult, TerminalError};
35
use crate::serde::{Deserialize, Serialize};
@@ -11,6 +13,7 @@ pub use request::{Request, RequestTarget};
1113
pub use run::RunClosure;
1214
pub type HeaderMap = http::HeaderMap<String>;
1315

16+
/// Service handler context.
1417
pub struct Context<'ctx> {
1518
random_seed: u64,
1619
#[cfg(feature = "rand")]
@@ -20,10 +23,12 @@ pub struct Context<'ctx> {
2023
}
2124

2225
impl<'ctx> Context<'ctx> {
26+
/// Get request headers.
2327
pub fn headers(&self) -> &HeaderMap {
2428
&self.headers
2529
}
2630

31+
/// Get request headers.
2732
pub fn headers_mut(&mut self) -> &HeaderMap {
2833
&mut self.headers
2934
}
@@ -41,6 +46,7 @@ impl<'ctx> From<(&'ctx ContextInternal, InputMetadata)> for Context<'ctx> {
4146
}
4247
}
4348

49+
/// Object shared handler context.
4450
pub struct SharedObjectContext<'ctx> {
4551
key: String,
4652
random_seed: u64,
@@ -51,14 +57,17 @@ pub struct SharedObjectContext<'ctx> {
5157
}
5258

5359
impl<'ctx> SharedObjectContext<'ctx> {
60+
/// Get object key.
5461
pub fn key(&self) -> &str {
5562
&self.key
5663
}
5764

65+
/// Get request headers.
5866
pub fn headers(&self) -> &HeaderMap {
5967
&self.headers
6068
}
6169

70+
/// Get request headers.
6271
pub fn headers_mut(&mut self) -> &HeaderMap {
6372
&mut self.headers
6473
}
@@ -77,6 +86,7 @@ impl<'ctx> From<(&'ctx ContextInternal, InputMetadata)> for SharedObjectContext<
7786
}
7887
}
7988

89+
/// Object handler context.
8090
pub struct ObjectContext<'ctx> {
8191
key: String,
8292
random_seed: u64,
@@ -87,14 +97,17 @@ pub struct ObjectContext<'ctx> {
8797
}
8898

8999
impl<'ctx> ObjectContext<'ctx> {
100+
/// Get object key.
90101
pub fn key(&self) -> &str {
91102
&self.key
92103
}
93104

105+
/// Get request headers.
94106
pub fn headers(&self) -> &HeaderMap {
95107
&self.headers
96108
}
97109

110+
/// Get request headers.
98111
pub fn headers_mut(&mut self) -> &HeaderMap {
99112
&mut self.headers
100113
}
@@ -113,6 +126,7 @@ impl<'ctx> From<(&'ctx ContextInternal, InputMetadata)> for ObjectContext<'ctx>
113126
}
114127
}
115128

129+
/// Workflow shared handler context.
116130
pub struct SharedWorkflowContext<'ctx> {
117131
key: String,
118132
random_seed: u64,
@@ -136,19 +150,23 @@ impl<'ctx> From<(&'ctx ContextInternal, InputMetadata)> for SharedWorkflowContex
136150
}
137151

138152
impl<'ctx> SharedWorkflowContext<'ctx> {
153+
/// Get workflow key.
139154
pub fn key(&self) -> &str {
140155
&self.key
141156
}
142157

158+
/// Get request headers.
143159
pub fn headers(&self) -> &HeaderMap {
144160
&self.headers
145161
}
146162

163+
/// Get request headers.
147164
pub fn headers_mut(&mut self) -> &HeaderMap {
148165
&mut self.headers
149166
}
150167
}
151168

169+
/// Workflow handler context.
152170
pub struct WorkflowContext<'ctx> {
153171
key: String,
154172
random_seed: u64,
@@ -172,19 +190,23 @@ impl<'ctx> From<(&'ctx ContextInternal, InputMetadata)> for WorkflowContext<'ctx
172190
}
173191

174192
impl<'ctx> WorkflowContext<'ctx> {
193+
/// Get workflow key.
175194
pub fn key(&self) -> &str {
176195
&self.key
177196
}
178197

198+
/// Get request headers.
179199
pub fn headers(&self) -> &HeaderMap {
180200
&self.headers
181201
}
182202

203+
/// Get request headers.
183204
pub fn headers_mut(&mut self) -> &HeaderMap {
184205
&mut self.headers
185206
}
186207
}
187208

209+
/// Trait exposing Restate timers functionalities.
188210
pub trait ContextTimers<'ctx>: private::SealedContext<'ctx> {
189211
/// Sleep using Restate
190212
fn sleep(&self, duration: Duration) -> impl Future<Output = Result<(), TerminalError>> + 'ctx {
@@ -194,7 +216,9 @@ pub trait ContextTimers<'ctx>: private::SealedContext<'ctx> {
194216

195217
impl<'ctx, CTX: private::SealedContext<'ctx>> ContextTimers<'ctx> for CTX {}
196218

219+
/// Trait exposing Restate service to service communication functionalities.
197220
pub trait ContextClient<'ctx>: private::SealedContext<'ctx> {
221+
/// Create a [`Request`].
198222
fn request<Req, Res>(
199223
&self,
200224
request_target: RequestTarget,
@@ -203,20 +227,95 @@ pub trait ContextClient<'ctx>: private::SealedContext<'ctx> {
203227
Request::new(self.inner_context(), request_target, req)
204228
}
205229

230+
/// Create a service client. The service client is generated by the [`restate_sdk_macros::service`] macro with the same name of the trait suffixed with `Client`.
231+
///
232+
/// ```rust
233+
/// # use std::time::Duration;
234+
/// # use restate_sdk::prelude::*;
235+
///
236+
/// #[restate_sdk::service]
237+
/// trait MyService {
238+
/// fn handle() -> HandlerResult<()>;
239+
/// }
240+
///
241+
/// # async fn handler(ctx: Context<'_>) {
242+
/// let client = ctx.service_client::<MyServiceClient>();
243+
///
244+
/// // Do request
245+
/// let result = client.handle().call().await;
246+
///
247+
/// // Just send the request, don't wait the response
248+
/// client.handle().send();
249+
///
250+
/// // Schedule the request to be executed later
251+
/// client.handle().send_with_delay(Duration::from_secs(60));
252+
/// }
253+
///
254+
/// ```
206255
fn service_client<C>(&self) -> C
207256
where
208257
C: IntoServiceClient<'ctx>,
209258
{
210259
C::create_client(self.inner_context())
211260
}
212261

262+
/// Create an object client. The object client is generated by the [`restate_sdk_macros::object`] macro with the same name of the trait suffixed with `Client`.
263+
///
264+
/// ```rust
265+
/// # use std::time::Duration;
266+
/// # use restate_sdk::prelude::*;
267+
///
268+
/// #[restate_sdk::object]
269+
/// trait MyObject {
270+
/// fn handle() -> HandlerResult<()>;
271+
/// }
272+
///
273+
/// # async fn handler(ctx: Context<'_>) {
274+
/// let client = ctx.object_client::<MyObjectClient>("my-key");
275+
///
276+
/// // Do request
277+
/// let result = client.handle().call().await;
278+
///
279+
/// // Just send the request, don't wait the response
280+
/// client.handle().send();
281+
///
282+
/// // Schedule the request to be executed later
283+
/// client.handle().send_with_delay(Duration::from_secs(60));
284+
/// }
285+
///
286+
/// ```
213287
fn object_client<C>(&self, key: impl Into<String>) -> C
214288
where
215289
C: IntoObjectClient<'ctx>,
216290
{
217291
C::create_client(self.inner_context(), key.into())
218292
}
219293

294+
/// Create an workflow client. The workflow client is generated by the [`restate_sdk_macros::workflow`] macro with the same name of the trait suffixed with `Client`.
295+
///
296+
/// ```rust
297+
/// # use std::time::Duration;
298+
/// # use restate_sdk::prelude::*;
299+
///
300+
/// #[restate_sdk::workflow]
301+
/// trait MyWorkflow {
302+
/// fn handle() -> HandlerResult<()>;
303+
/// }
304+
///
305+
/// # async fn handler(ctx: Context<'_>) {
306+
/// let client = ctx.workflow_client::<MyWorkflowClient>("my-key");
307+
///
308+
/// // Do request
309+
/// let result = client.handle().call().await;
310+
///
311+
/// // Just send the request, don't wait the response
312+
/// client.handle().send();
313+
///
314+
/// // Schedule the request to be executed later
315+
/// client.handle().send_with_delay(Duration::from_secs(60));
316+
/// }
317+
///
318+
/// ```
220319
fn workflow_client<C>(&self, key: impl Into<String>) -> C
221320
where
222321
C: IntoWorkflowClient<'ctx>,
@@ -225,20 +324,29 @@ pub trait ContextClient<'ctx>: private::SealedContext<'ctx> {
225324
}
226325
}
227326

327+
/// Trait used by codegen to create the service client.
228328
pub trait IntoServiceClient<'ctx>: Sized {
229329
fn create_client(ctx: &'ctx ContextInternal) -> Self;
230330
}
231331

332+
/// Trait used by codegen to use the object client.
232333
pub trait IntoObjectClient<'ctx>: Sized {
233334
fn create_client(ctx: &'ctx ContextInternal, key: String) -> Self;
234335
}
235336

337+
/// Trait used by codegen to use the workflow client.
236338
pub trait IntoWorkflowClient<'ctx>: Sized {
237339
fn create_client(ctx: &'ctx ContextInternal, key: String) -> Self;
238340
}
239341

240342
impl<'ctx, CTX: private::SealedContext<'ctx>> ContextClient<'ctx> for CTX {}
241343

344+
/// Trait exposing Restate awakeables functionalities.
345+
///
346+
/// Awakeables can be used to implement external asynchronous systems interactions,
347+
/// for example you can send a Kafka record including the awakeable id returned by [`ContextAwakeables::awakeable`],
348+
/// and then let another service consume from Kafka the responses of given external system interaction by using
349+
/// [`ContextAwakeables::resolve_awakeable`] or [`ContextAwakeables::reject_awakeable`].
242350
pub trait ContextAwakeables<'ctx>: private::SealedContext<'ctx> {
243351
/// Create an awakeable
244352
fn awakeable<T: Deserialize + 'static>(
@@ -263,8 +371,10 @@ pub trait ContextAwakeables<'ctx>: private::SealedContext<'ctx> {
263371

264372
impl<'ctx, CTX: private::SealedContext<'ctx>> ContextAwakeables<'ctx> for CTX {}
265373

374+
/// Trait exposing Restate functionalities to deal with non-deterministic operations.
266375
pub trait ContextSideEffects<'ctx>: private::SealedContext<'ctx> {
267-
/// Run a non-deterministic operation
376+
/// Run a non-deterministic operation and record its result.
377+
///
268378
fn run<R, F, T>(
269379
&self,
270380
name: &'ctx str,
@@ -278,15 +388,23 @@ pub trait ContextSideEffects<'ctx>: private::SealedContext<'ctx> {
278388
self.inner_context().run(name, run_closure)
279389
}
280390

391+
/// Return a random seed inherently predictable, based on the invocation id, which is not secret.
392+
///
393+
/// This value is stable during the invocation lifecycle, thus across retries.
281394
fn random_seed(&self) -> u64 {
282395
private::SealedContext::random_seed(self)
283396
}
284397

398+
/// Return a [`rand::Rng`] instance inherently predictable, seeded with [`ContextSideEffects::random_seed`].
399+
///
400+
/// This instance is useful to generate identifiers, idempotency keys, and for uniform sampling from a set of options.
401+
/// If a cryptographically secure value is needed, please generate that externally using [`ContextSideEffects::run`].
285402
#[cfg(feature = "rand")]
286403
fn rand(&mut self) -> &mut rand::prelude::StdRng {
287404
private::SealedContext::rand(self)
288405
}
289406

407+
/// Return a random [`uuid::Uuid`], generated using [`ContextSideEffects::rand`].
290408
#[cfg(all(feature = "rand", feature = "uuid"))]
291409
fn rand_uuid(&mut self) -> uuid::Uuid {
292410
let rand = private::SealedContext::rand(self);
@@ -296,6 +414,7 @@ pub trait ContextSideEffects<'ctx>: private::SealedContext<'ctx> {
296414

297415
impl<'ctx, CTX: private::SealedContext<'ctx>> ContextSideEffects<'ctx> for CTX {}
298416

417+
/// Trait exposing Restate K/V read functionalities.
299418
pub trait ContextReadState<'ctx>: private::SealedContext<'ctx> {
300419
/// Get state
301420
fn get<T: Deserialize + 'static>(
@@ -316,6 +435,7 @@ impl<'ctx, CTX: private::SealedContext<'ctx> + private::SealedCanReadState> Cont
316435
{
317436
}
318437

438+
/// Trait exposing Restate K/V write functionalities.
319439
pub trait ContextWriteState<'ctx>: private::SealedContext<'ctx> {
320440
/// Set state
321441
fn set<T: Serialize + 'static>(&self, key: &str, t: T) {
@@ -338,6 +458,13 @@ impl<'ctx, CTX: private::SealedContext<'ctx> + private::SealedCanWriteState> Con
338458
{
339459
}
340460

461+
/// Trait exposing Restate promise functionalities.
462+
///
463+
/// A promise is a durable, distributed version of a Rust oneshot channel.
464+
/// Restate keeps track of the promises across restarts/failures.
465+
///
466+
/// You can use this feature to implement interaction between different workflow handlers, e.g. to
467+
/// send a signal from a shared handler to the workflow handler.
341468
pub trait ContextPromises<'ctx>: private::SealedContext<'ctx> {
342469
/// Create a promise
343470
fn promise<T: Deserialize + 'static>(

0 commit comments

Comments
 (0)