hannibal 0.16.2

A small actor library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# Hannibal Actor Library - Agent Context Document

**Version:** 0.15.0  
**Edition:** Rust 2024  
**MSRV:** 1.92  
**License:** MIT/Apache-2.0

## Project Overview

Hannibal is a small, modern actor library for async Rust that simplifies concurrent programming by encapsulating state and communication patterns. It was completely rewritten from scratch in v0.12 (originally forked from xactor).

### Key Motivation
In async Rust, you often spawn tasks and create channels manually, which becomes cumbersome in larger projects. Hannibal provides:
- Typed message handling with responses
- Automatic lifecycle management
- Multiple message type support per actor
- Clean abstractions over channels and tasks

### Repository Structure
```
hannibal/
├── src/                    # Main library code
│   ├── actor.rs           # Actor trait and core actor logic
│   ├── addr.rs            # Address types for communicating with actors
│   ├── handler.rs         # Handler and StreamHandler traits
│   ├── context.rs         # Context API for actors
│   ├── event_loop.rs      # Core event loop implementation
│   ├── channel.rs         # Channel abstraction layer
│   ├── broker.rs          # Pub/sub message broker
│   ├── error.rs           # Error types
│   └── runtime.rs         # Runtime abstractions
├── hannibal-derive/       # Procedural macros (#[derive(Actor)], #[message], etc.)
├── examples/              # 14 comprehensive examples
├── tests/                 # Integration tests
└── hannibal-examples/     # Separate examples workspace
```

## Core Concepts

### The Actor Model

An **Actor** is:
- A struct that holds state
- Runs in its own async task
- Communicates exclusively through messages
- Has lifecycle hooks (`started`, `stopped`, `cancelled`)

### Central Architecture

```
┌─────────────┐
│   Actor     │ ──── Your struct with state
└─────────────┘
       │
       ↓
┌─────────────┐
│ EventLoop   │ ──── Owns actor, receives messages
└─────────────┘
       │
       ↓
┌─────────────┐
│   Channel   │ ──── Message queue (bounded/unbounded)
└─────────────┘
       ↑
       │
┌─────────────┐
│    Addr<A>  │ ──── Handle to send messages
└─────────────┘
```

### Key Entities

1. **Actor** - Trait implemented by your struct; defines lifecycle hooks
2. **Context** - Passed to handlers; provides API for intervals, children, self-messaging, stopping
3. **Addr<A>** - Strong address to an actor (keeps actor alive)
4. **WeakAddr<A>** - Weak address (doesn't keep actor alive)
5. **Sender<M>** - Send messages by message type (not actor type)
6. **Caller<M>** - Call and get responses by message type
7. **EventLoop** - Owns actor and receiver; dispatches messages
8. **Payload** - Internal enum wrapping all messages and control signals
9. **OwningAddr<A>** - Allows retrieving the actor after it stops

## Core Traits and Types

### Actor Trait
```rust
pub trait Actor: Sized + Send + 'static {
    const NAME: &'static str = "hannibal::Actor";
    
    // Lifecycle hooks (all optional)
    fn started(&mut self, ctx: &mut Context<Self>) -> impl Future<Output = DynResult> + Send;
    fn stopped(&mut self, ctx: &mut Context<Self>) -> impl Future<Output = ()> + Send;
    fn cancelled(&mut self, ctx: &mut Context<Self>) -> impl Future<Output = ()> + Send;
}
```

### Message Trait
```rust
pub trait Message: 'static + Send {
    type Response: 'static + Send;
}
```

Use the `#[message]` or `#[message(response = T)]` attribute:
```rust
#[message]
struct Greet(&'static str);  // Response = ()

#[message(response = i32)]
struct Add(i32, i32);  // Response = i32
```

### Handler Trait
```rust
pub trait Handler<M: Message>: Actor {
    fn handle(
        &mut self,
        ctx: &mut Context<Self>,
        msg: M,
    ) -> impl Future<Output = M::Response> + Send;
}
```

### StreamHandler Trait
For actors that process streams:
```rust
pub trait StreamHandler<M: 'static>: Actor {
    fn handle(&mut self, ctx: &mut Context<Self>, msg: M) 
        -> impl Future<Output = ()> + Send;
    fn finished(&mut self, ctx: &mut Context<Self>) 
        -> impl Future<Output = ()> + Send;
}
```

### Service Trait
For globally accessible singleton actors:
```rust
pub trait Service: Actor + Default {
    async fn from_registry() -> Addr<Self>;
    fn try_from_registry() -> Option<Addr<Self>>;
}
```

## Core APIs

### Spawning Actors

```rust
// Simple spawn
let addr = MyActor::default().spawn();

// Owning spawn (get actor back after stop)
let owning_addr = MyActor::default().spawn_owning();

// Using builder for configuration
let addr = hannibal::setup_actor(MyActor::default())
    .bounded(10)                    // Bounded channel
    .recreate_from_default()        // Auto-restart on failure
    .spawn();

// Spawn on a stream
let addr = hannibal::setup_actor(MyActor::default())
    .on_stream(my_stream)
    .spawn();
```

### Sending Messages

```rust
// Fire-and-forget
addr.send(Greet("Alice")).await?;

// Call and await response
let result = addr.call(Add(1, 2)).await?;

// Try send (non-blocking)
addr.try_send(Greet("Bob"))?;

// Ping to check if alive
addr.ping().await?;
```

### Using Senders and Callers

```rust
// Type-erased messaging (by message type, not actor type)
let sender: Sender<Greet> = addr.sender();
sender.send(Greet("Alice")).await?;

let caller: Caller<Add> = addr.caller();
let result = caller.call(Add(1, 2)).await?;

// Weak variants (don't keep actor alive)
let weak_sender = addr.weak_sender::<Greet>();
let weak_caller = addr.weak_caller::<Add>();
```

### Context API

Available in lifecycle hooks and handlers:

```rust
impl Handler<MyMessage> for MyActor {
    async fn handle(&mut self, ctx: &mut Context<Self>, msg: MyMessage) {
        // Lifecycle control
        ctx.stop()?;
        
        // Intervals and timers
        ctx.interval(PingMsg, Duration::from_secs(1));
        ctx.delayed_send(|| StopMsg, Duration::from_secs(10));
        ctx.delayed_exec(async { /* ... */ }, Duration::from_millis(500));
        
        // Child actors
        ctx.add_child(child_addr);
        ctx.register_child::<MyMsg>(child_addr);
        ctx.send_to_children(BroadcastMsg);
        
        // Broker subscriptions
        ctx.subscribe::<Topic>().await?;
        ctx.unsubscribe::<Topic>().await?;
        
        // Self-addressing
        let weak_addr = ctx.weak_address();
        let addr = ctx.address();
    }
}
```

### Services and Broker

```rust
// Service: global singleton
#[derive(Default, Actor, Service)]
struct MyService { /* ... */ }

let addr = MyService::from_registry().await;
addr.call(GetData).await?;

// Broker: pub/sub
#[derive(Clone, Message)]
struct Topic(String);

// Subscribe in actor's started() hook
ctx.subscribe::<Topic>().await?;

// Publish from anywhere
Broker::publish(Topic("hello".into())).await?;
```

## Features and Configuration

### Cargo Features

```toml
[features]
default = ["tokio_runtime"]

# Runtimes
tokio_runtime = ["runtime", "dep:tokio", "async-global-executor/tokio"]
async_runtime = ["runtime", "dep:async-io", "dep:async-global-executor"]

# Channel implementations
async_channel = ["dep:async-channel"]  # Use async-channel instead of futures
futures_channel = []                   # Use futures::channel (default)
```

### Channel Configuration

```rust
// Bounded channel (backpressure when full)
let addr = hannibal::setup_actor(actor).bounded(100).spawn();

// Unbounded channel (no backpressure)
let addr = hannibal::setup_actor(actor).unbounded().spawn();

// Default is unbounded
let addr = actor.spawn();
```

### Restart Strategies

```rust
impl RestartableActor for MyActor {}

let addr = hannibal::setup_actor(MyActor::default())
    .recreate_from_default()  // Restart using Default::default()
    .spawn();

// Manually restart
addr.restart()?;
```

### Timeouts

```rust
let addr = hannibal::setup_actor(actor)
    .abort_after(Duration::from_secs(5))  // Timeout for message handling
    .spawn();
```

## Common Patterns

### Basic Actor with State

```rust
use hannibal::prelude::*;

#[derive(Actor)]
struct Counter(i32);

#[message(response = i32)]
struct Increment;

impl Handler<Increment> for Counter {
    async fn handle(&mut self, _ctx: &mut Context<Self>, _: Increment) -> i32 {
        self.0 += 1;
        self.0
    }
}

let mut addr = Counter(0).spawn();
let count = addr.call(Increment).await?;
```

### Actor with Initialization

```rust
impl Actor for MyActor {
    async fn started(&mut self, ctx: &mut Context<Self>) -> DynResult<()> {
        // Setup intervals, subscribe to topics, spawn children
        ctx.interval(Tick, Duration::from_secs(1));
        ctx.subscribe::<GlobalEvent>().await?;
        Ok(())
    }
    
    async fn stopped(&mut self, _ctx: &mut Context<Self>) {
        // Cleanup resources
        println!("Actor stopped");
    }
}
```

### Stream Processing

```rust
#[derive(Default, Actor)]
struct StreamProcessor;

impl StreamHandler<String> for StreamProcessor {
    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: String) {
        println!("Received: {}", msg);
    }
    
    async fn finished(&mut self, ctx: &mut Context<Self>) {
        ctx.stop().unwrap();
    }
}

let stream = futures::stream::iter(vec!["a", "b", "c"]);
let addr = hannibal::setup_actor(StreamProcessor)
    .on_stream(stream)
    .spawn();
addr.await?; // Waits for stream to finish
```

### Parent-Child Actor Hierarchies

```rust
impl Actor for Parent {
    async fn started(&mut self, ctx: &mut Context<Self>) -> DynResult<()> {
        // Children are stopped when parent stops
        ctx.add_child(Child::default().spawn());
        
        // Register children by message type for broadcasting
        ctx.register_child::<Update>(Child::default().spawn());
        Ok(())
    }
}

impl Handler<BroadcastUpdate> for Parent {
    async fn handle(&mut self, ctx: &mut Context<Self>, msg: BroadcastUpdate) {
        ctx.send_to_children(Update(msg.0));
    }
}
```

## Error Handling

```rust
pub enum ActorError {
    AsyncSendError(futures::channel::mpsc::SendError),
    Canceled(futures::channel::oneshot::Canceled),
    AlreadyStopped,
    ServiceStillRunning,
    Timeout,
}

pub type Result<T> = std::result::Result<T, ActorError>;
pub type DynResult<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
```

## Testing

### Running Tests

```bash
# All tests with default features
cargo test

# Test with specific runtime
cargo test --no-default-features --features tokio_runtime
cargo test --no-default-features --features async_runtime

# Use nextest (preferred for stress testing)
just test
```

### Test Utilities

```rust
#[test_log::test(tokio::test)]
async fn test_actor() {
    let addr = MyActor::default().spawn();
    let result = addr.call(MyMessage).await.unwrap();
    assert_eq!(result, expected);
}
```

## Development Guidelines

### Code Quality

The project enforces strict linting:
```toml
[lints.rust]
unsafe_code = "forbid"
trivial_numeric_casts = "forbid"
unused_import_braces = "forbid"

[lints.clippy]
dbg_macro = "warn"
expect_used = "warn"
indexing_slicing = "warn"
missing_panics_doc = "warn"
```

### No `unwrap()` in Library Code
Use `?` or explicit error handling. Tests can use `unwrap()`.

### Logging
Uses the `log` crate with structured logging:
```rust
log::trace!("calling actor {}", std::any::type_name::<M>());
log::debug!("received {}", msg.0);
```

### Conventions

- **Commits:** Follow [Conventional Commits](https://www.conventionalcommits.org/)
- **Versioning:** SemVer 2.0 with `cargo-semver-checks`
- **Documentation:** All public APIs must have doc comments
- **Examples:** Add examples for new features

## CI Pipeline

```bash
just ci  # Runs full CI locally
```

The CI includes:
1. Clippy with multiple feature combinations
2. Tests with all runtime/channel combinations
3. Doc tests
4. Semver compatibility checks
5. Example builds

## Key Files

- **`src/lib.rs`** - Public API and module structure
- **`src/actor.rs`** - Actor trait and spawning logic
- **`src/addr.rs`** - Address types and messaging
- **`src/event_loop.rs`** - Core event loop
- **`src/context.rs`** - Context API implementation
- **`src/handler.rs`** - Handler traits
- **`src/broker.rs`** - Broker implementation
- **`examples/`** - Best learning resource (14 examples)
- **`how_hannibal_works.md`** - Architecture deep-dive

## Quick Reference

### Import Pattern
```rust
use hannibal::prelude::*;
```

### Derive Macros
```rust
#[derive(Actor)]           // Implement Actor with defaults
#[derive(Service)]         // Implement Service trait
#[derive(Message)]         // Implement Message with Response = ()

#[message]                 // Response = ()
#[message(response = T)]   // Response = T

#[hannibal::main]          // Main function macro (sets up runtime)
```

### Spawning Shortcuts
```rust
actor.spawn()              -> Addr<A>
actor.spawn_owning()       -> OwningAddr<A>
hannibal::setup_actor(actor)     -> Builder
```

### Address Types
- `Addr<A>` - Strong, actor-typed
- `WeakAddr<A>` - Weak, actor-typed
- `OwningAddr<A>` - Strong, returns actor on stop
- `Sender<M>` - Strong, message-typed
- `WeakSender<M>` - Weak, message-typed
- `Caller<M>` - Strong, message-typed (with response)
- `WeakCaller<M>` - Weak, message-typed (with response)

## Examples to Study

1. **`examples/simple.rs`** - Basic actor, sending, calling
2. **`examples/builder.rs`** - Builder configuration
3. **`examples/streams.rs`** - Stream processing
4. **`examples/broker.rs`** - Pub/sub patterns
5. **`examples/children.rs`** - Actor hierarchies
6. **`examples/intervals.rs`** - Timers and intervals
7. **`examples/storage-service.rs`** - Service pattern
8. **`examples/timeout.rs`** - Timeout handling
9. **`examples/owning_addr.rs`** - Retrieving actor after stop

## Architecture Notes

- Actors run in separate tasks via `async-global-executor` or Tokio
- Messages are sent through channels (futures::mpsc or async-channel)
- The EventLoop owns the actor and processes Payload enums
- Context provides actor self-reference and system interaction
- Services use a global registry (type-keyed HashMap)
- Broker uses weak senders to avoid keeping subscribers alive

## When Adding Features

1. Consider both `tokio_runtime` and `async_runtime` compatibility
2. Add examples demonstrating the feature
3. Update documentation and this agent.md
4. Add tests for both bounded and unbounded channels
5. Test with both channel implementations if relevant
6. Update CHANGELOG.md following conventional commits

## Known Limitations

- Stream handlers cannot be restarted (tied to stream lifecycle)
- Actors must be `Send + 'static`
- Messages must be `Send + 'static`