logicaffeine-system
Platform IO and system services for LOGOS.
This crate provides platform-agnostic IO operations, persistence, networking, and concurrency primitives with feature-gated heavy dependencies. The core is lean by default—only enable what you need.
Design Philosophy
- Lean by default: No network, no persistence, no parallelism in the default build
- Feature-gated capabilities: Heavy dependencies (libp2p, rayon, memmap2) are opt-in
- Dual platform support: Native Rust and WASM with platform-specific implementations
- CRDT-first persistence: Journal-based storage designed for conflict-free data types
Feature Flags
| Feature | Dependencies | Description |
|---|---|---|
| (default) | — | Lean core with basic IO only |
persistence |
memmap2, sha2 | File I/O, VFS abstraction, journal-based storage |
networking |
libp2p, futures | P2P networking via libp2p with mDNS discovery |
concurrency |
rayon, bumpalo | Parallel computation, channels, zone-based memory |
full |
all above | All features enabled |
distributed |
networking + persistence | Combined features for Distributed<T> |
Module Overview
Core (Always Available)
| Module | Description |
|---|---|
io |
Console I/O: show(), println(), read_line(), Showable trait |
fmt |
String formatting utilities |
Native-Only (Not on WASM)
| Module | Description |
|---|---|
time |
Timestamps and delays: now(), sleep() |
env |
Environment variables and args: get(), args() |
random |
Random number generation: randomInt(), randomFloat() |
Feature-Gated
| Module | Feature | Description |
|---|---|---|
file |
persistence |
Simple synchronous file I/O |
fs |
persistence |
Async VFS abstraction with atomic writes |
storage |
persistence |
Persistent<T> - journal-based CRDT storage |
network |
networking |
P2P mesh networking with GossipSub |
crdt |
networking |
Synced<T> - auto-replicated CRDT wrapper |
concurrency |
concurrency |
Task spawning, channels, cooperative yielding |
memory |
concurrency |
Zone-based memory allocation |
distributed |
distributed |
Distributed<T> - persistent + networked CRDT |
Public API
I/O (io)
use ;
show; // Prints: 42
show; // Prints: hello (no quotes)
show; // Prints: [1, 2, 3]
println;
let name = read_line;
The Showable trait provides natural formatting—primitives display without decoration, collections with brackets, and CRDTs show their logical values.
Time (time) — Native Only
use time;
let start = now; // Milliseconds since Unix epoch
sleep; // Block for 1 second
let elapsed = now - start;
Environment (env) — Native Only
use env;
if let Some = get
for arg in args
Random (random) — Native Only
use random;
let dice = randomInt; // Inclusive range [1, 6]
let chance = randomFloat; // Range [0.0, 1.0)
File I/O (file) — Requires persistence
use file;
write?;
let content = read?;
Virtual File System (fs) — Requires persistence
Platform-agnostic async file operations:
use ;
use Arc;
let vfs: = new;
vfs.write.await?;
let data = vfs.read.await?;
vfs.append.await?; // Atomic append
let entries = vfs.list_dir.await?;
Platform implementations:
NativeVfs: Native filesystem via tokio with atomic writesOpfsVfs: Browser Origin Private File System (WASM)
Persistent Storage (storage) — Requires persistence
Journal-based crash-resilient storage for CRDTs:
use Persistent;
use GCounter;
let vfs = new;
let counter = mount.await?;
counter.mutate.await?;
counter.compact.await?; // Reduce journal size
Journal format:
┌─────────────┬─────────────┬─────────────────┐
│ Length (4B) │ CRC32 (4B) │ Payload (N B) │
└─────────────┴─────────────┴─────────────────┘
Concurrency (concurrency) — Requires concurrency
Go-like concurrency primitives:
use ;
// Spawn async task
let handle = spawn;
if handle.is_finished
handle.abort; // Cancel if needed
// Bounded channel (Go-like)
let = new;
tx.send.await?;
let msg = rx.recv.await;
// Cooperative yielding (10ms threshold)
for i in 0..1_000_000
Memory Zones (memory) — Requires concurrency
Arena allocation with bulk deallocation:
use Zone;
// Heap zone for temporary allocations
let zone = new_heap; // 1 MB arena
let x = zone.alloc;
let slice = zone.alloc_slice;
// Mapped zone for zero-copy file access (requires persistence)
let zone = new_mapped?;
let bytes = zone.as_slice;
Networking (network) — Requires networking
P2P mesh networking with libp2p:
use ;
use ;
// Start listening
listen.await?;
// Connect to peer
connect.await?;
// Point-to-point messaging
let peer = new?;
send.await?;
// Pub/sub broadcast
gossip_subscribe.await;
gossip_publish.await?;
Synced CRDTs (crdt) — Requires networking
Auto-replicated CRDT wrapper (ephemeral, no persistence):
use Synced;
use GCounter;
let synced = new.await;
synced.mutate.await;
let value = synced.get.await;
Distributed CRDTs (distributed) — Requires distributed
Persistent + networked CRDT (survives restarts):
use Distributed;
use GCounter;
// Disk-only (same as Persistent<T>)
let counter = mount.await?;
// Disk + Network sync
let counter = mount.await?;
// Mutations are persisted AND broadcast
counter.mutate.await?;
Data flow:
Local mutation: RAM → Journal → Network
Remote update: Network → RAM → Journal
Usage Examples
Cargo.toml
# Lean build (default)
[]
= "0.6"
# With persistence
[]
= { = "0.6", = ["persistence"] }
# Full features
[]
= { = "0.6", = ["full"] }
Persistent Counter
use Persistent;
use NativeVfs;
use GCounter;
use Arc;
async
Producer-Consumer with Pipes
use ;
async
Platform Support
| Feature | Native | WASM |
|---|---|---|
io (show, println, read_line) |
✓ | ✓ |
fmt |
✓ | ✓ |
time |
✓ | ✗ |
env |
✓ | ✗ |
random |
✓ | ✗ |
file |
✓ | ✗ |
fs (NativeVfs) |
✓ | — |
fs (OpfsVfs) |
— | ✓ |
storage (Persistent) |
✓ | ✓ |
concurrency |
✓ | ✗ |
memory |
✓ | ✗ |
network |
✓ | ✗ |
crdt (Synced) |
✓ | ✗ |
distributed (disk-only) |
✓ | ✓ |
distributed (networked) |
✓ | ✗ |
Key Design Patterns
Journal-Based Crash-Resilient Storage
All persistent state uses append-only journals with CRC32 checksums:
- Snapshots replace state during compaction
- Deltas record incremental updates
- Truncated entries are ignored (WAL semantics)
- Auto-compaction when entry count exceeds threshold
Showable Trait
Natural formatting for LOGOS values:
- Primitives: displayed as-is (
42,true,hello) - Collections: bracket notation (
[1, 2, 3]) - Options:
nothingfor None, value for Some - CRDTs: logical value (
GCountershows count, not internal state)
Zone-Based Memory
"Hotel California" rule—values enter but don't escape:
- Heap zones: Fast bump allocation, O(1) bulk deallocation
- Mapped zones: Zero-copy file access via mmap
Cooperative Multitasking
The check_preemption() function yields after 10ms of computation, balancing responsiveness against context-switch overhead.
License
Business Source License 1.1 (BUSL-1.1)
- Free for individuals and organizations with <25 employees
- Commercial license required for organizations with 25+ employees offering Logic Services
- Converts to MIT on December 24, 2029
See LICENSE for full terms.