%20(1).png)
⚡ What is Sprite?
Sprite is a Rust library for building fault-tolerant, concurrent actor systems with a clean, declarative API inspired by React.
Instead of writing complex supervision trees and manual error handling, you write actor components — pure setup closures that declare state, message handlers, and children. If an actor panics, Sprite automatically resurrects it in under 50 nanoseconds with its state intact.
🚀 Quick Start
use ;
🧩 Core Concepts
| Concept | React Equivalent | What it does |
|---|---|---|
Engine |
createRoot |
The runtime that mounts and manages actors |
engine.spawn(name, |ctx| { ... }) |
<Component /> |
Creates an actor from a setup closure |
ctx.use_state(key, initial) |
useState |
Declares typed state that survives crashes |
ctx.use_scratch(initial) |
useRef |
Ephemeral value wiped on recovery |
ctx.on_message(|msg| { ... }) |
onClick / event handler |
Registers a message handler |
ctx.on_panic(| | { ... }) |
componentDidCatch |
Runs after each panic recovery |
ctx.on_mount(| | { ... }) |
useEffect([], ...) |
Runs once on first start |
ctx.on_unmount(| | { ... }) |
cleanup function | Runs on graceful shutdown |
ctx.spawn(name, ...) |
child components | Spawns nested actors |
ctx.send_to(id, msg) |
props callback | Sends a message to another actor |
ctx.send_named(name, msg) |
— | Sends to a named actor |
ctx.poll() |
— | Non-blocking message check |
ctx.sleep(dur) |
— | Yield without blocking thread |
Handle::send(msg) |
setState / props |
Sends a message to an actor |
Handle::send_msg(any) |
— | Send any IntoMessage type |
Handle::request(msg, timeout) |
fetch |
Request/response pattern |
🔥 Fault Tolerance
[ Panic in Actor ]
│
▼
catch_unwind catches it (~0.05ms)
│
▼
Arena reset (~1ns)
│
▼
State already intact (0ns — lives in shared store)
│
▼
Setup closure re-runs (~1ms)
│
▼
Actor Fully Restored (< 50ns for recovery core)
State created via use_state lives in a shared store outside the actor thread, so it survives panics automatically. The actor's scratch allocations (via bumpalo) are reset, but your data is safe.
🛡️ Circuit Breaker
If an actor panics more than 10 times in 5 seconds, Sprite trips a circuit breaker and halts it — preventing infinite crash loops.
engine.spawn;
📦 Installation
[]
= { = "https://github.com/dragprog/sprite-engine" }
🏗️ Advanced Examples
Actor Pool (load balancing)
use ;
let engine = new;
let pool = new;
for i in 0..100
Broadcast
engine.broadcast;
Named Actors
engine.spawn;
engine.send_named;
assert_eq!;
Timer
use Timer;
use Duration;
send_after;
Builder API
use ;
let handle = new
.arena_size
.max_recoveries
.recovery_window
.spawn;
🧪 Running Tests
📊 Benchmarks
| Benchmark | Target |
|---|---|
recovery_speed |
Panic → full restore |
mailbox_throughput |
Messages / second |
🤝 Contributing
- Fork the repo (
git checkout -b feature/cool-thing). - Keep the API surface minimal and ergonomic.
- Ensure tests pass (
cargo test). - Submit a PR with a concise breakdown.
📄 License
Distributed under the Apache License 2.0. See LICENSE for details.