Skip to content

Documentation for Rust SDK #34

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 25 commits into from
Dec 4, 2024
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
3 changes: 3 additions & 0 deletions examples/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod my_service;
pub mod my_virtual_object;
pub mod my_workflow;
22 changes: 22 additions & 0 deletions examples/services/my_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use restate_sdk::prelude::*;

#[restate_sdk::service]
pub trait MyService {
async fn my_handler(greeting: String) -> Result<String, HandlerError>;
}

pub struct MyServiceImpl;

impl MyService for MyServiceImpl {
async fn my_handler(&self, _ctx: Context<'_>, greeting: String) -> Result<String, HandlerError> {
Ok(format!("{greeting}!"))
}
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
HttpServer::new(Endpoint::builder().bind(MyServiceImpl.serve()).build())
.listen_and_serve("0.0.0.0:9080".parse().unwrap())
.await;
}
39 changes: 39 additions & 0 deletions examples/services/my_virtual_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use restate_sdk::prelude::*;

#[restate_sdk::object]
pub trait MyVirtualObject {
async fn my_handler(name: String) -> Result<String, HandlerError>;
#[shared]
async fn my_concurrent_handler(name: String) -> Result<String, HandlerError>;
}

pub struct MyVirtualObjectImpl;

impl MyVirtualObject for MyVirtualObjectImpl {
async fn my_handler(
&self,
ctx: ObjectContext<'_>,
greeting: String,
) -> Result<String, HandlerError> {
Ok(format!("Greetings {} {}", greeting, ctx.key()))
}
async fn my_concurrent_handler(
&self,
ctx: SharedObjectContext<'_>,
greeting: String,
) -> Result<String, HandlerError> {
Ok(format!("Greetings {} {}", greeting, ctx.key()))
}
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
HttpServer::new(
Endpoint::builder()
.bind(MyVirtualObjectImpl.serve())
.build(),
)
.listen_and_serve("0.0.0.0:9080".parse().unwrap())
.await;
}
35 changes: 35 additions & 0 deletions examples/services/my_workflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use restate_sdk::prelude::*;

#[restate_sdk::workflow]
pub trait MyWorkflow {
async fn run(req: String) -> Result<String, HandlerError>;
#[shared]
async fn interact_with_workflow() -> Result<(), HandlerError>;
}

pub struct MyWorkflowImpl;

impl MyWorkflow for MyWorkflowImpl {
async fn run(&self, _ctx: WorkflowContext<'_>, _req: String) -> Result<String, HandlerError> {
// implement workflow logic here

Ok(String::from("success"))
}
async fn interact_with_workflow(
&self,
_ctx: SharedWorkflowContext<'_>,
) -> Result<(), HandlerError> {
// implement interaction logic here
// e.g. resolve a promise that the workflow is waiting on

Ok(())
}
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
HttpServer::new(Endpoint::builder().bind(MyWorkflowImpl.serve()).build())
.listen_and_serve("0.0.0.0:9080".parse().unwrap())
.await;
}
Loading
Loading