Skip to content

Revised Macro Refactor: Impl Block Support and Backward Compatibility (#43) #67

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

BigFish2086
Copy link

This is a complete redo of the work in PR #53, due to project changes and requested revisions that changed the implementation direction. Closing the old PR to keep the history clean.

This PR addresses Issue #43 by refactoring the restate-sdk-rust macros to apply directly to impl blocks instead of requiring separate trait definitions. The change improves ergonomics, eliminates the need for FooBar/FooBarImpl naming conventions, and aligns the SDK with more idiomatic Rust patterns.

Current Macros:

  • #[service], #[object], and #[workflow] with support for optional attributes like
    vis = "pub" which is needed to control the visibility of the generated Serve and Client structs and
    name = "my_service" for overriding the service name.

  • #[handler] to mark which of the methods are service handlers. It also supports optional attributes like
    name = "my_handler" to override the handler name and shared in case of having an Object or a Workflow to support concurrent use of the handler.

Example:

use restate_sdk::prelude::*; pub struct Counter; #[restate_sdk::object(vis = "pub")] impl Counter { #[handler(name = "add_value")] async fn add(&self, _ctx: ObjectContext<'_>, val: u64) -> Result<u64, TerminalError> { unimplemented!() } #[handler(shared, name = "get_counter")] async fn get(&self, _ctx: SharedObjectContext<'_>,) -> Result<u64, TerminalError> { unimplemented!() } }

However, this "Redo" keeps the old syntax valid as well for backword compitability, so this still works as before:

use restate_sdk::prelude::*; #[restate_sdk::object] trait Counter { #[shared] async fn get() -> Result<u64, TerminalError>; async fn add(val: u64) -> Result<u64, TerminalError>; } struct CounterImpl; const COUNT: &str = "count"; impl Counter for CounterImpl { async fn get(&self, ctx: SharedObjectContext<'_>) -> Result<u64, TerminalError> { Ok(ctx.get::<u64>(COUNT).await?.unwrap_or(0)) } async fn add(&self, ctx: ObjectContext<'_>, val: u64) -> Result<u64, TerminalError> { let current = ctx.get::<u64>(COUNT).await?.unwrap_or(0); let new = current + val; ctx.set(COUNT, new); Ok(new) } }
- updated Cargo.toml to have mutliple binaries, one that used the old macro trait-syntax, and another one for the new macro struct-impl syntax, so we can test them later, so now `cargo build -p test-services` would generate 2 binaries trait-test-services & impl-test-services - updated Dockerfile: - to cache the building process through the new mount syntax as well as building in multiple stages - to have a --build-arg which is BIN that deteremins which macro syntax to test when running the restate-sdk-test-suite.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant