joerl 0.7.1

An Erlang-inspired actor model library for Rust
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Core actor trait and context.
//!
//! Actors are the fundamental unit of computation in joerl. Each actor:
//! - Has a unique Pid
//! - Processes messages sequentially from its mailbox
//! - Can spawn other actors
//! - Can send messages to other actors
//! - Can link to and monitor other actors

use crate::Pid;
use crate::mailbox::Mailbox;
use crate::message::{Envelope, ExitReason, Message, Signal};
use async_trait::async_trait;

/// Actor trait that must be implemented by all actors.
///
/// This trait defines the actor's behavior. The `handle_message` method
/// is called for each message received by the actor.
///
/// # Examples
///
/// ```
/// use joerl::{Actor, ActorContext, Message};
/// use async_trait::async_trait;
///
/// struct MyActor;
///
/// #[async_trait]
/// impl Actor for MyActor {
///     async fn started(&mut self, ctx: &mut ActorContext) {
///         // Initialization logic
///     }
///
///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
///         if let Some(text) = msg.downcast_ref::<String>() {
///             println!("Received: {}", text);
///         }
///     }
/// }
/// ```
#[async_trait]
pub trait Actor: Send + 'static {
    /// Called when the actor is started, before processing any messages.
    ///
    /// Override this method to perform initialization logic.
    async fn started(&mut self, _ctx: &mut ActorContext) {}

    /// Called when a message is received.
    ///
    /// This is the main message handling method that must be implemented.
    async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext);

    /// Called when a signal is received.
    ///
    /// Override this to handle EXIT, DOWN, and other system signals.
    /// Default behavior is to stop on EXIT signals if not trapping exits.
    async fn handle_signal(&mut self, signal: Signal, ctx: &mut ActorContext) {
        match signal {
            Signal::Exit { from, reason } => {
                if !ctx.is_trapping_exits() && !reason.is_normal() {
                    tracing::warn!(
                        "Actor {} received EXIT from {}: {}",
                        ctx.pid(),
                        from,
                        reason
                    );
                    ctx.stop(reason);
                }
            }
            Signal::Down {
                reference: _,
                pid,
                reason,
            } => {
                tracing::debug!("Actor {} received DOWN for {}: {}", ctx.pid(), pid, reason);
            }
            Signal::Stop => {
                ctx.stop(ExitReason::Normal);
            }
            Signal::Kill => {
                ctx.stop(ExitReason::Killed);
            }
        }
    }

    /// Called when the actor is stopping.
    ///
    /// Override this to perform cleanup logic. The reason parameter
    /// indicates why the actor is stopping.
    async fn stopped(&mut self, _reason: &ExitReason, _ctx: &mut ActorContext) {}
}

/// Context provided to actors for interacting with the actor system.
///
/// The context provides access to:
/// - The actor's own Pid
/// - Methods for sending messages
/// - Methods for spawning, linking, and monitoring other actors
/// - Lifecycle control (stop, restart)
pub struct ActorContext {
    pid: Pid,
    mailbox: Mailbox,
    trap_exit: bool,
    should_stop: bool,
    stop_reason: Option<ExitReason>,
    system: Option<std::sync::Weak<crate::ActorSystem>>,
}

impl ActorContext {
    /// Creates a new actor context.
    pub(crate) fn new(pid: Pid, mailbox: Mailbox) -> Self {
        Self {
            pid,
            mailbox,
            trap_exit: false,
            should_stop: false,
            stop_reason: None,
            system: None,
        }
    }

    /// Sets the system reference (called internally during spawn).
    pub(crate) fn set_system(&mut self, system: std::sync::Weak<crate::ActorSystem>) {
        self.system = Some(system);
    }

    /// Returns the Pid of this actor.
    ///
    /// The Pid uniquely identifies this actor in the system and can be
    /// used to send messages or establish links/monitors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use async_trait::async_trait;
    ///
    /// struct MyActor;
    ///
    /// #[async_trait]
    /// impl Actor for MyActor {
    ///     async fn started(&mut self, ctx: &mut ActorContext) {
    ///         let my_pid = ctx.pid();
    ///         println!("My PID is: {}", my_pid);
    ///     }
    ///     
    ///     async fn handle_message(&mut self, _msg: Message, _ctx: &mut ActorContext) {}
    /// }
    /// ```
    pub fn pid(&self) -> Pid {
        self.pid
    }

    /// Returns true if the actor is trapping exit signals.
    ///
    /// When trapping exits, the actor will receive EXIT signals as
    /// regular signals instead of being terminated automatically.
    pub fn is_trapping_exits(&self) -> bool {
        self.trap_exit
    }

    /// Sets whether to trap exit signals.
    ///
    /// When trapping exits, EXIT signals from linked actors are delivered
    /// to `handle_signal` instead of causing the actor to terminate.
    /// This allows actors to handle failures gracefully.
    ///
    /// In Erlang: `process_flag(trap_exit, true)`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message, Signal, ExitReason};
    /// use async_trait::async_trait;
    ///
    /// struct Supervisor;
    ///
    /// #[async_trait]
    /// impl Actor for Supervisor {
    ///     async fn started(&mut self, ctx: &mut ActorContext) {
    ///         // Enable exit trapping to handle child failures
    ///         ctx.trap_exit(true);
    ///     }
    ///
    ///     async fn handle_message(&mut self, _msg: Message, _ctx: &mut ActorContext) {}
    ///
    ///     async fn handle_signal(&mut self, signal: Signal, _ctx: &mut ActorContext) {
    ///         if let Signal::Exit { from, reason } = signal {
    ///             println!("Child {} exited: {}", from, reason);
    ///             // Handle the failure instead of dying
    ///         }
    ///     }
    /// }
    /// ```
    pub fn trap_exit(&mut self, trap: bool) {
        self.trap_exit = trap;
    }

    /// Stops the actor with the given reason.
    ///
    /// Calling this method marks the actor for termination. The actor will
    /// finish processing the current message, then its `stopped` hook will
    /// be called before the actor terminates.
    ///
    /// # Arguments
    ///
    /// * `reason` - The reason for stopping, which will be sent to linked actors
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message, ExitReason};
    /// use async_trait::async_trait;
    ///
    /// struct Worker;
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
    ///         if let Some(cmd) = msg.downcast_ref::<&str>() {
    ///             if *cmd == "shutdown" {
    ///                 ctx.stop(ExitReason::Normal);
    ///             } else if *cmd == "panic" {
    ///                 ctx.stop(ExitReason::Panic("Something went wrong".into()));
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    pub fn stop(&mut self, reason: ExitReason) {
        self.should_stop = true;
        self.stop_reason = Some(reason);
    }

    /// Returns true if the actor should stop.
    pub(crate) fn should_stop(&self) -> bool {
        self.should_stop
    }

    /// Returns the stop reason if the actor should stop.
    pub(crate) fn stop_reason(&self) -> Option<&ExitReason> {
        self.stop_reason.as_ref()
    }

    /// Receives the next message or signal from the mailbox.
    pub(crate) async fn recv(&mut self) -> Option<Envelope> {
        self.mailbox.recv().await
    }

    /// Closes the actor's mailbox.
    #[allow(dead_code)]
    pub(crate) fn close_mailbox(&mut self) {
        self.mailbox.close();
    }

    /// Sends a message to another actor.
    ///
    /// This is the primary way for actors to communicate. The sender Pid is
    /// automatically tracked from this context.
    ///
    /// # Arguments
    ///
    /// * `to` - The Pid of the target actor
    /// * `msg` - The message to send
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The target actor doesn't exist
    /// - The target actor's mailbox is full
    /// - For remote messages: serialization fails or node is unreachable
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, ActorSystem, Message, Pid};
    /// use async_trait::async_trait;
    ///
    /// struct Worker {
    ///     peer: Option<Pid>,
    /// }
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
    ///         if let Some(peer_pid) = self.peer {
    ///             // Send reply to peer
    ///             ctx.send(peer_pid, Box::new("Reply".to_string())).await.ok();
    ///         }
    ///     }
    /// }
    /// ```
    pub async fn send(&self, to: Pid, msg: Message) -> crate::Result<()> {
        if let Some(system_weak) = &self.system
            && let Some(system) = system_weak.upgrade()
        {
            return system.send(to, msg).await;
        }
        Err(crate::ActorError::SendFailed(to))
    }

    /// Looks up a process by name.
    ///
    /// Returns the Pid if the name is registered, or None otherwise.
    ///
    /// In Erlang: `whereis(Name)`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use async_trait::async_trait;
    ///
    /// struct Worker;
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
    ///         if let Some(manager_pid) = ctx.whereis("manager") {
    ///             ctx.send(manager_pid, Box::new("status")).await.ok();
    ///         }
    ///     }
    /// }
    /// ```
    pub fn whereis(&self, name: &str) -> Option<Pid> {
        if let Some(system_weak) = &self.system
            && let Some(system) = system_weak.upgrade()
        {
            return system.whereis(name);
        }
        None
    }

    /// Schedules a message to be sent after a delay.
    ///
    /// Returns a TimerRef that can be used to cancel the timer.
    ///
    /// In Erlang: `erlang:send_after(Time, Dest, Message)`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use joerl::scheduler::Destination;
    /// use async_trait::async_trait;
    /// use std::time::Duration;
    ///
    /// struct Worker;
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
    ///         // Schedule a reminder to ourselves
    ///         ctx.send_after(
    ///             Destination::Pid(ctx.pid()),
    ///             Box::new("reminder"),
    ///             Duration::from_secs(5)
    ///         );
    ///     }
    /// }
    /// ```
    pub fn send_after(
        &self,
        dest: crate::scheduler::Destination,
        msg: Message,
        duration: std::time::Duration,
    ) -> Option<crate::scheduler::TimerRef> {
        if let Some(system_weak) = &self.system
            && let Some(system) = system_weak.upgrade()
        {
            return Some(system.send_after(dest, msg, duration));
        }
        None
    }

    /// Selectively receive a message matching the predicate.
    ///
    /// This is similar to Erlang's `receive` with pattern matching.
    /// Messages that don't match remain in the mailbox and will be
    /// checked again on subsequent receive calls or regular message processing.
    ///
    /// This allows actors to wait for specific messages while leaving
    /// other messages in the mailbox for later processing.
    ///
    /// In Erlang: `receive Pattern -> Body end`
    ///
    /// # Arguments
    ///
    /// * `predicate` - Function that returns `Some(T)` if message matches
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use async_trait::async_trait;
    ///
    /// #[derive(Clone)]
    /// struct Response {
    ///     id: u64,
    ///     data: String,
    /// }
    ///
    /// struct RpcActor {
    ///     next_id: u64,
    /// }
    ///
    /// #[async_trait]
    /// impl Actor for RpcActor {
    ///     async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
    ///         // Make a request with an ID
    ///         let req_id = self.next_id;
    ///         self.next_id += 1;
    ///         
    ///         // ... send request somewhere ...
    ///         
    ///         // Wait for specific response
    ///         let response = ctx.receive(|msg| {
    ///             msg.downcast_ref::<Response>()
    ///                 .filter(|r| r.id == req_id)
    ///                 .cloned()
    ///         }).await;
    ///         
    ///         // Meanwhile, other messages stay in mailbox
    ///     }
    /// }
    /// ```
    pub async fn receive<F, T>(&mut self, predicate: F) -> Option<T>
    where
        F: FnMut(&Message) -> Option<T>,
    {
        self.mailbox.recv_matching(predicate, None).await
    }

    /// Selectively receive a message with timeout.
    ///
    /// Like `receive()`, but returns `None` if no matching message
    /// arrives within the timeout duration.
    ///
    /// In Erlang: `receive Pattern -> Body after Timeout -> TimeoutBody end`
    ///
    /// # Arguments
    ///
    /// * `predicate` - Function that returns `Some(T)` if message matches
    /// * `timeout` - Maximum time to wait
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use async_trait::async_trait;
    /// use std::time::Duration;
    ///
    /// #[derive(Clone)]
    /// struct Ack {
    ///     id: u64,
    /// }
    ///
    /// struct Worker;
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, _msg: Message, ctx: &mut ActorContext) {
    ///         // Wait for ack, but timeout after 5 seconds
    ///         let ack = ctx.receive_timeout(
    ///             |msg| msg.downcast_ref::<Ack>().cloned(),
    ///             Duration::from_secs(5)
    ///         ).await;
    ///         
    ///         match ack {
    ///             Some(ack) => println!("Got ack: {}", ack.id),
    ///             None => println!("Timeout waiting for ack"),
    ///         }
    ///     }
    /// }
    /// ```
    pub async fn receive_timeout<F, T>(
        &mut self,
        predicate: F,
        timeout: std::time::Duration,
    ) -> Option<T>
    where
        F: FnMut(&Message) -> Option<T>,
    {
        self.mailbox.recv_matching(predicate, Some(timeout)).await
    }

    /// Try to receive a matching message without blocking.
    ///
    /// Returns immediately with `Some(T)` if a matching message is found,
    /// or `None` if no match is available.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::{Actor, ActorContext, Message};
    /// use async_trait::async_trait;
    ///
    /// struct Worker;
    ///
    /// #[async_trait]
    /// impl Actor for Worker {
    ///     async fn handle_message(&mut self, _msg: Message, ctx: &mut ActorContext) {
    ///         // Check if there's a ready message without waiting
    ///         if let Some(ready) = ctx.try_receive(|msg| {
    ///             msg.downcast_ref::<String>()
    ///                 .filter(|s| s.as_str() == "ready")
    ///                 .cloned()
    ///         }) {
    ///             println!("Got ready signal: {}", ready);
    ///         }
    ///     }
    /// }
    /// ```
    pub fn try_receive<F, T>(&mut self, predicate: F) -> Option<T>
    where
        F: FnMut(&Message) -> Option<T>,
    {
        self.mailbox.try_recv_matching(predicate)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mailbox::{DEFAULT_MAILBOX_CAPACITY, Mailbox};

    struct TestActor {
        messages_received: usize,
    }

    #[async_trait]
    impl Actor for TestActor {
        async fn handle_message(&mut self, _msg: Message, _ctx: &mut ActorContext) {
            self.messages_received += 1;
        }
    }

    #[tokio::test]
    async fn test_actor_context_pid() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let ctx = ActorContext::new(pid, mailbox);
        assert_eq!(ctx.pid(), pid);
    }

    #[tokio::test]
    async fn test_actor_context_trap_exit() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);

        assert!(!ctx.is_trapping_exits());
        ctx.trap_exit(true);
        assert!(ctx.is_trapping_exits());
    }

    #[tokio::test]
    async fn test_actor_context_stop() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);

        assert!(!ctx.should_stop());
        ctx.stop(ExitReason::Normal);
        assert!(ctx.should_stop());
        assert_eq!(ctx.stop_reason(), Some(&ExitReason::Normal));
    }

    #[tokio::test]
    async fn test_actor_handle_exit_signal() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);
        let mut actor = TestActor {
            messages_received: 0,
        };

        // Non-trapped exit should stop actor
        let signal = Signal::Exit {
            from: Pid::new(),
            reason: ExitReason::Panic("error".to_string()),
        };
        actor.handle_signal(signal, &mut ctx).await;
        assert!(ctx.should_stop());
    }

    #[tokio::test]
    async fn test_actor_trap_exit_signal() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);
        let mut actor = TestActor {
            messages_received: 0,
        };

        ctx.trap_exit(true);

        // Trapped exit should NOT stop actor
        let signal = Signal::Exit {
            from: Pid::new(),
            reason: ExitReason::Panic("error".to_string()),
        };
        actor.handle_signal(signal, &mut ctx).await;
        assert!(!ctx.should_stop());
    }

    #[tokio::test]
    async fn test_actor_stop_signal() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);
        let mut actor = TestActor {
            messages_received: 0,
        };

        actor.handle_signal(Signal::Stop, &mut ctx).await;
        assert!(ctx.should_stop());
        assert_eq!(ctx.stop_reason(), Some(&ExitReason::Normal));
    }

    #[tokio::test]
    async fn test_actor_kill_signal() {
        let pid = Pid::new();
        let (mailbox, _sender) = Mailbox::new(DEFAULT_MAILBOX_CAPACITY);
        let mut ctx = ActorContext::new(pid, mailbox);
        let mut actor = TestActor {
            messages_received: 0,
        };

        actor.handle_signal(Signal::Kill, &mut ctx).await;
        assert!(ctx.should_stop());
        assert_eq!(ctx.stop_reason(), Some(&ExitReason::Killed));
    }
}