Approach B: protocol traits + protocol_impl! macro#153
Approach B: protocol traits + protocol_impl! macro#153ElFantasma wants to merge 14 commits intomainfrom
Conversation
… API Introduces a unified API redesign addressing #144, #145, and #129: - Handler<M> trait with per-message typed results (RPITIT, not object-safe) - Receiver<M>/Recipient<M> for type-erased cross-actor messaging - #[actor] proc macro that generates Handler<M> impls from #[handler] methods - Any-based named registry (register/whereis/unregister) - messages! declarative macro for defining message structs - Context<A> replaces ActorRef<A> in handler signatures - New examples: chat_room (Recipient), service_discovery (registry) - All existing examples migrated to new API
…rait # Conflicts: # Cargo.lock # Cargo.toml # concurrency/src/message.rs # concurrency/src/tasks/mod.rs # concurrency/src/threads/mod.rs # examples/bank/src/main.rs # examples/bank/src/messages.rs # examples/bank/src/server.rs # examples/bank_threads/src/main.rs # examples/bank_threads/src/messages.rs # examples/bank_threads/src/server.rs # examples/blocking_genserver/main.rs # examples/busy_genserver_warning/main.rs # examples/chat_room/Cargo.toml # examples/chat_room/src/main.rs # examples/chat_room/src/room.rs # examples/chat_room/src/user.rs # examples/name_server/src/main.rs # examples/name_server/src/messages.rs # examples/name_server/src/server.rs # examples/service_discovery/Cargo.toml # examples/service_discovery/src/main.rs # examples/signal_test/src/main.rs # examples/signal_test_threads/src/main.rs # examples/updater/src/main.rs # examples/updater/src/messages.rs # examples/updater/src/server.rs # examples/updater_threads/src/main.rs # examples/updater_threads/src/messages.rs
🤖 Kimi Code ReviewReview SummaryThis is a major API redesign PR that addresses two critical issues (#144 and #145) by replacing the old enum-based Actor trait with a new Handler system. The changes are comprehensive and well-structured. Key Improvements
Code Quality
Specific Issues Found
Performance Implications
Migration PathThe PR includes complete migration of all examples to the new API, demonstrating backward compatibility isn't maintained (which is acceptable for a 0.x version). The examples show the new API is significantly more ergonomic. Documentation
RecommendationThis PR represents a significant improvement to the spawned framework. The new API is more type-safe, more ergonomic, and better aligned with Rust idioms. The implementation is solid with only minor issues that don't affect correctness. Approve with the following suggestions:
Automated review by Kimi (Moonshot AI) · custom prompt |
🤖 Codex Code ReviewFindings
Notes
If you want, I can propose a minimal patch for the cancellation wakeup issue (tasks + threads). Automated review by OpenAI Codex · custom prompt |
Greptile SummaryThis PR implements "Approach B" for the spawned actor framework, introducing protocol traits with the Key changes:
Architecture impact: All 34 tests pass according to the PR description, and examples have been verified to run correctly. Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| macros/src/lib.rs | Added #[actor] proc macro for generating Handler implementations - clean implementation, properly handles sync/async methods |
| concurrency/src/message.rs | New file with Message trait and protocol_impl! declarative macro - well-documented, handles all use cases (send/request, async/sync) |
| concurrency/src/registry.rs | New named actor registry with global HashMap - proper locking, type-safe downcast, good test coverage |
| concurrency/src/tasks/actor.rs | Major refactor to Handler-based architecture, added Context::actor_ref(), Response<T> for object-safe traits - well-structured changes |
| concurrency/src/threads/actor.rs | Parallel refactor for threads module with sync handlers - consistent with tasks module design |
| concurrency/src/error.rs | Simplified error enum to ActorStopped and RequestTimeout - cleaner, more focused API |
| examples/chat_room/src/protocols.rs | Protocol trait definitions for chat room example - clean separation of concerns, no circular dependencies |
| examples/chat_room/src/room.rs | ChatRoom actor implementation with protocol_impl! - demonstrates the new pattern effectively |
| examples/chat_room/src/user.rs | User actor using ctx.actor_ref() for self-registration - demonstrates key new feature properly |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[User: Define Protocol Trait] --> B[protocol_impl! Macro]
B --> C[Generate trait impl on ActorRef]
D[User: Define Actor + Messages] --> E[actor Proc Macro]
E --> F[Generate Handler impls]
G[Cross-boundary Call] --> H{Protocol Trait Method}
H -->|send fn| I[ActorRef::send]
H -->|request fn| J[ActorRef::request_raw]
J --> K[Response T wrapper]
I --> L[MessageEnvelope]
K --> L
L --> M[Actor Message Queue]
M --> N[Handler::handle]
N --> O[Actor State Update]
P[ctx.actor_ref] --> Q[Self-registration]
Q --> R[Pass ActorRef to Other Actors]
Last reviewed commit: 04d86e1
Summary
protocol_impl!declarative macro that implements user-defined protocol traits onActorRef<A>by mapping methods to messagesContext::actor_ref()so actors can obtain their ownActorReffrom within handlers (needed for self-registration)Response<T>for object-safe async request-response on protocol traitsactor_api!macroShared infrastructure from
feat/actor-macro-registry:Handler<M>,#[actor]proc macro,send_messages!/request_messages!,Receiver<M>/Recipient<M>, named registry.Key pattern: Cross-boundary communication via user-defined trait objects (
BroadcasterRef,ParticipantRef). Zero direct dependencies between actor modules — both depend only on a sharedprotocols.rs.Test plan
cargo test -p spawned-concurrency— 34 tests passingcargo run -p chat_roomcargo run -p chat_room_threadscargo run -p ping_pongcargo run -p ping_pong_threadscargo run -p bankcargo run -p service_discoverygit grep "actor_api!"→ 0 results in code