Skip to content

Commit 0bb9862

Browse files
committed
more macros to reduce boilerplate code and common improvements
1 parent c5c3979 commit 0bb9862

25 files changed

Lines changed: 1308 additions & 128 deletions

Cargo.lock

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
resolver = "2"
33
members = [
44
"src/uactor",
5+
"src/uactor-derive",
56
]
67

78
[workspace.dependencies]

README.md

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,100 @@
11
# uActor
2+
23
## Overview
3-
The fastest and most modular actor system that doesn’t force you to pay for what you don’t need
4+
5+
The fastest and most modular actor system that doesn't force you to pay for what you don't need.
6+
7+
## Quick Start
8+
9+
Define messages with `#[derive(Message)]`, implement handlers with `#[uactor::actor]` / `#[uactor::handler]`:
10+
11+
```rust
12+
#[derive(uactor::Message)]
13+
struct Increment;
14+
15+
#[derive(uactor::Message, Debug)]
16+
struct GetCount(Reply<CountResponse>);
17+
18+
struct CounterActor { count: u32 }
19+
20+
impl Actor for CounterActor {
21+
type Context = Context;
22+
type RouteMessage = CounterActorMsg;
23+
type Inject = ();
24+
type State = ();
25+
}
26+
27+
#[uactor::actor]
28+
impl CounterActor {
29+
#[uactor::handler]
30+
async fn handle_increment(&mut self, _msg: Increment) -> HandleResult {
31+
self.count += 1;
32+
Ok(())
33+
}
34+
35+
#[uactor::handler]
36+
async fn handle_get(&self, GetCount(reply): GetCount) -> HandleResult {
37+
let _ = reply.send(CountResponse(self.count));
38+
Ok(())
39+
}
40+
}
41+
42+
uactor::generate_actor_ref!(CounterActor, { Increment, GetCount });
43+
```
44+
45+
See the full runnable version: [Example: Macro handlers](src/uactor/examples/macro_handlers.rs)
446

547
## Examples
48+
649
Examples can be found [here](src/uactor/examples).
750

8-
### Features
9-
1. Simplified creation of a tokio actor topic oriented actor
51+
## Features
52+
53+
1. Simplified creation of tokio-based topic-oriented actors
1054
2. Minimum boilerplate code
11-
3. Support different tokio channels including `watch`, `broadcast`, `oneshot`, `mpsc`.
12-
4. Each actor is able to listen up to 30 channels.
13-
5. Added support of actors with single real channel and routing messages to the defined handler
14-
[Example: Single channel](src/uactor/examples/single_channel_actor.rs)
15-
6. Added tick (actor call each n seconds/millis/etc) support
16-
[Example: Interval](src%2Fuactor%2Fexamples%2Finterval.rs)
17-
7. Implemented Dependency Injection on pre-start stage to solve cross-references problem ("Actor#1" needs a reference to the "Actor#2", and "Actor#2" needs a reference to "Actor#1")
18-
[Example: dependency injection](src/uactor/examples/dependency_injection.rs)
55+
3. Support for different tokio channels including `watch`, `broadcast`, `oneshot`, `mpsc`
56+
4. Each actor is able to listen up to 30 channels
57+
5. Single channel routing with `generate_actor_ref!`
58+
[Example: Single channel](src/uactor/examples/single_channel_actor.rs)
59+
6. Tick support (actor called each n seconds/millis/etc)
60+
[Example: Interval](src/uactor/examples/interval.rs)
61+
7. Dependency Injection on pre-start stage to solve cross-references ("Actor#1" needs "Actor#2" and vice versa)
62+
[Example: Dependency injection](src/uactor/examples/dependency_injection.rs)
1963
8. Integration with tokio/tracing, including tracing of actor lifecycle, messages, and handlers
20-
9. Implemented support for actors for which it is necessary to work with multiple message sources (channels) [Example: Multi channel](./src/uactor/examples/multiple_incoming_channels.rs)
21-
10. Implemented shared state for actors [Example: Shared state](./src/uactor/examples/shared_state.rs)
64+
9. Multiple message sources (channels) per actor
65+
[Example: Multi channel](src/uactor/examples/multiple_incoming_channels.rs)
66+
10. Shared state for actors
67+
[Example: Shared state](src/uactor/examples/shared_state.rs)
68+
69+
### Derive and macro support
70+
71+
- **`#[derive(Message)]`** -- implement the `Message` trait without boilerplate.
72+
Also available as `message_impl!(MsgA, MsgB)` for multiple types at once.
73+
74+
- **`#[uactor::actor]` + `#[uactor::handler]`** -- define message handlers as simple methods
75+
instead of manual `impl Handler<M>` for each message type.
76+
77+
Handler parameters:
78+
- First parameter (after optional `&mut self` / `&self`) -- the message; its type determines `Handler<Type>`
79+
- `ctx` -- maps to `&mut Self::Context`
80+
- `state` -- maps to `&Self::State`
81+
- Any other parameter -- accessed as a field from the `Inject` struct by name
82+
83+
- **`generate_actor_ref!` with aliased variants** -- map primitive or external types
84+
to named enum variants:
85+
```rust
86+
uactor::generate_actor_ref!(MyActor, { PingMsg, NextId: i32, Label: String });
87+
// Generates: enum MyActorMsg { PingMsg(PingMsg), NextId(i32), Label(String) }
88+
```
89+
90+
[Example: Macro handlers](src/uactor/examples/macro_handlers.rs)
2291

2392
### Actor lifecycle
93+
2494
![Lifecycle.png](docs/assets/Lifecycle.png)
2595

26-
### Other projects:
96+
### Other projects
97+
2798
1. Actix
2899
2. Ractor
29100
3. Tokactor
@@ -37,4 +108,4 @@ This project is licensed under the [MIT license](LICENSE).
37108

38109
Unless you explicitly state otherwise, any contribution intentionally submitted
39110
for inclusion in uActor by you, shall be licensed as MIT, without any additional
40-
terms or conditions.
111+
terms or conditions.

src/uactor-derive/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "uactor-derive"
3+
version = "0.16.2"
4+
edition = "2021"
5+
repository = "https://github.com/EnvOut/uactor"
6+
license = "MIT"
7+
workspace = "../../"
8+
description = "Derive macros for uactor"
9+
10+
[lib]
11+
proc-macro = true
12+
13+
[dependencies]
14+
syn = "2"
15+
quote = "1"
16+
proc-macro2 = "1"

0 commit comments

Comments
 (0)