Rust

Actor Framework in Rust

Design and implement a custom, trait-based, fully composable actor framework in Rust, built step by step from first principles.

⏱️ 17h 20min
📦 52 modules
🎯 Intermediate

What You'll Build

We'll design and implement a complete actor framework in Rust from scratch — no external actor libraries, just async Rust primitives. Starting with the fundamental Actor trait, we'll progressively build a runtime, mailbox, context, address system, and event/interrupt/request-response patterns.

Along the way we'll explore key concurrency patterns: message passing via channels, trait-based polymorphism with async_trait, type-erased envelopes, lifecycle management with watch channels, and the Future trait for custom awaitable types. By the end, our framework supports fire-and-forget events, graceful interruption, and strongly-typed request-response interactions with a Calculator example to prove it all works.

Actor Framework in Rust cover

Learning Objectives

  • Understand the actor model and its Rust implementation

  • Build an async runtime with Tokio-based message passing

  • Design type-erased message envelopes for heterogeneous mailboxes

  • Implement actor lifecycle management with status tracking

  • Create an event system with typed handlers

  • Build a graceful interruption mechanism

  • Refactor a single crate into a Cargo workspace

  • Implement a request-response pattern with oneshot channels

  • Build a custom Future type for awaitable responses

Prerequisites

  • Rust fundamentals (traits, generics, ownership)

  • Basic async Rust (async/await, Tokio)

  • Familiarity with channels and message passing

Assembly Steps

1

Project Baseline

2

Actor and Runtime Foundation

3

Event Loop Skeleton

4

Async Runtime Transition

5

Message Trait Abstraction

6

Tokio Context and Mailbox

7

Envelope Type Alias

8

Mailbox-Driven Event Loop

9

Standalone Spawning

10

First Actor Test

11

Actor Address

12

Message Dispatch

13

Error-Safe Sending

14

Event Handler Trait

15

Event Wrapper Type

16

Event-to-Message Bridge

17

Event Dispatch Logic

18

Test Actor Event Handler

19

Sending Events in Tests

20

Async Test with Delay

21

Status Tracking Channel

22

Status Query Helper

23

Join Awaitable

24

Proper Test Synchronization

25

Completion Notification

26

Active Flag Loop Guard

27

Shutdown Logging

28

Mailbox Close and Stop

29

Interrupt Hook

30

Context-Aware Handlers

31

Error Propagation Pipeline

32

Contextual Event Handling

33

Interrupt Message Type

34

Interrupt via Address

35

Fallible Event Handlers

36

Workspace Migration

37

Extension Crate Scaffold

38

Request Handler Trait

39

Async Request Handling

40

Request-Response Contract

41

Interaction Wrapper

42

Request Messaging Bridge

43

Oneshot Response Channel

44

Address Extension Trait

45

Responder Type

46

Awaitable Responder

47

Calculator Test Harness

48

Request and Handler Binding

49

End-to-End Interaction Test

50

Default Received Handler

51

Fallible Responses

52

Context-Aware Requests

Technologies

Rust Actor Model Async Trait Tokio Message Passing Concurrency Trait Object Type Erasure Oneshot Channel Future Cargo Workspace