Expand description
Safe memory-mapped I/O with zero-copy persistent data structures
memmap3 is a complete drop-in replacement for memmap2 that adds a huge additional list of βsafeβ features. All memmap2 functionality is available, plus powerful new APIs that eliminate unsafe code while providing automatic persistence.
Β§π― Core Features Summary
- π Auto-Persistence: Changes persist automatically via memory mapping
- π Zero Unsafe: Safe Rust APIs for all operations
- β‘ Thread-Safe: Atomic fields work across processes
- π Convenience Operators:
<<for intuitive data manipulation - π Fixed-Size + Unlimited: Fixed layouts + segmented growth when needed
- π§© Rich Type System: Primitives, atomics, strings, collections, nested structs
- π Multidimensional: N-dimensional arrays for join-like operations
- π Drop-in Compatible: All memmap2 code works unchanged
Β§π Quick Start
Add to your Cargo.toml:
[dependencies]
memmap3 = "0.1"Create persistent data structures:
use memmap3::prelude::*;
#[mmap_struct]
struct Counter {
#[mmap(atomic)]
value: u64,
name: [u8; 32],
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create persistent data
let mut counter = MmapStruct::<Counter>::create("/tmp/counter.mmap")?;
&mut counter.name << "my_counter";
// Atomic operations work across processes
let old_value = counter.value.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
println!("Counter: {} -> {}", old_value, old_value + 1);
Ok(())
}Β§π§ The #mmap_struct Macro
Transform regular Rust structs into persistent, memory-mapped types:
use memmap3::prelude::*;
#[mmap_struct]
struct MyData {
// Regular fields (primitives, enums)
id: u64,
active: bool,
// Atomic fields (thread-safe across processes)
#[mmap(atomic)]
counter: u64,
// Auto-detected strings (default for [u8; N])
name: [u8; 32],
// Explicit raw binary data
#[mmap(raw)]
key: [u8; 32],
// Fixed-capacity vectors
#[mmap(vec)]
scores: [u32; 10],
// String arrays
#[mmap(string_array)]
tags: [[u8; 16]; 5],
// Unlimited growth storage
#[mmap(segmented)]
events: [u64; 0],
}Β§π Convenience Operators
Smooth, intuitive syntax for common operations:
// String assignment
&mut my_string << "Hello";
// Vector append
&mut my_vec << 42u32;
// HashMap insertion
&mut my_map << ("key".to_string(), "value".to_string());
// HashMap key assignment using macro
hashmap_index!(my_map["key2".to_string()] << "value2".to_string());
// HashMap access
let value = &my_map[&"key".to_string()];
// Segmented append
&mut my_segmented << 123u64;Β§ποΈ Architecture Overview
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β memmap3 Safe Layer β
βββββββββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββββββββββ€
β #[mmap_struct] β Collections β Atomic Types β
β ββ Validation β ββ HashMap β ββ MmapAtomicU64 β
β ββ Auto-Layoutβ ββ Vec β ββ MmapAtomicI32 β
β ββ Type Safetyβ ββ String β ββ MmapAtomicBool β
βββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββββββββββββ€
β memmap2 Foundation β
β ββ Cross-platform memory mapping β
β ββ File lifecycle management β
β ββ Low-level safety guarantees β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΒ§π API Categories
Β§Core Types
MmapStruct<T>- Safe wrapper for memory-mapped structsmmap_struct- Attribute macro for creating mappable structs
Β§Collections
MmapHashMap<K,V>- Persistent hash map with O(1) operationsMmapVec<T>- Persistent vector with dynamic growthMmapString- Persistent UTF-8 string with auto-sizing
Β§Atomic Types
MmapAtomicU64,MmapAtomicI32,MmapAtomicBool- Thread-safe primitives- All standard atomic types with memory-mapped persistence
Β§Utilities
ByteArrayExt- String helpers for byte arraysSegmented<T>- Advanced segmented storage for large datasets
Β§π‘ Usage Patterns
Β§1. Simple Persistent Storage
use memmap3::prelude::*;
#[mmap_struct]
struct Config {
max_connections: u32,
timeout_ms: u32,
debug_mode: bool,
server_name: [u8; 64],
}
let mut config = MmapStruct::<Config>::create("/tmp/app.config")?;
config.max_connections = 1000;
config.timeout_ms = 5000;
config.debug_mode = false;
&mut config.server_name << "my-server";
// Values persist across application restarts
config.max_connections = 2000;Β§2. Atomic Operations Across Processes
use memmap3::prelude::*;
use std::sync::atomic::Ordering;
#[mmap_struct]
struct Metrics {
#[mmap(atomic)]
requests_served: u64,
#[mmap(atomic)]
errors_count: u64,
#[mmap(atomic)]
last_restart: u64,
}
let metrics = MmapStruct::<Metrics>::create("/tmp/metrics.mmap")?;
// Thread-safe operations work across processes
metrics.requests_served.fetch_add(1, Ordering::SeqCst);
// Check if another process incremented the counter
let current = metrics.requests_served.load(Ordering::SeqCst);
println!("Total requests: {}", current);Β§3. Persistent Collections
use memmap3::prelude::*;
// Persistent hash map
let mut cache = MmapHashMap::<&str, &str>::create("/tmp/cache.mmap")?;
cache.insert("user123", "Alice Smith")?;
// Persistent vector (fixed capacity)
let mut log = MmapVec::<u64, 1000>::new();
log.push(1234567890); // timestamp
// Persistent string (fixed size)
let mut name = MmapString::<64>::new();
&mut name << "Alice";Β§4. String Operations
use memmap3::prelude::*;
#[mmap_struct]
struct UserInfo {
id: u64,
name: [u8; 64], // Auto-detected as string
email: [u8; 128], // Auto-detected as string
}
let mut user = MmapStruct::<UserInfo>::create("/tmp/user.mmap")?;
user.id = 12345;
// String operations using << operator
&mut user.name << "Alice Smith";
&mut user.email << "alice@example.com";
// Read strings back
println!("User: {} ({})", user.name.as_str(), user.email.as_str());Β§π§ Feature Flags
serde- Enable Serde serialization supportmemmap3 = { version = "0.1", features = ["serde"] }
Β§π Compare with Alternatives
| Feature | memmap3 | memmap2 | mmap-rs | redb |
|---|---|---|---|---|
| Safe API | β | β | β | β |
| Zero-copy | β | β | β | β |
| Drop-in compatible | β | N/A | β | β |
| Atomic operations | β | β | β | β |
| Cross-platform | β | β | β | β |
| Collections | β | β | β | β |
Β§π Learning Resources
- Examples: See
examples/directory for comprehensive code samples - Tests: Integration tests in
tests/show real-world usage patterns - Benchmarks: Performance comparisons in
benches/directory
Β§β οΈ Safety Guarantees
memmap3 provides multiple layers of safety:
- Compile-time validation through the
#[mmap_struct]macro - Runtime bounds checking for all array and collection access
- Memory layout verification ensuring struct compatibility
- Atomic operation safety for concurrent access patterns
- UTF-8 validation for all string operations
Note: While memmap3 eliminates unsafe code in user applications, memory-mapped files shared between processes require coordination to prevent data races. Use atomic operations for shared state.
Re-exportsΒ§
pub use types::ByteArrayExt;pub use types::HashMapIterator;pub use types::MmapAtomicBool;pub use types::MmapAtomicI8;pub use types::MmapAtomicI16;pub use types::MmapAtomicI32;pub use types::MmapAtomicI64;pub use types::MmapAtomicIsize;pub use types::MmapAtomicU8;pub use types::MmapAtomicU16;pub use types::MmapAtomicU32;pub use types::MmapAtomicU64;pub use types::MmapAtomicUsize;pub use types::MmapHashMap;pub use types::MmapString;pub use types::MmapStringArray;pub use types::MmapStruct;pub use types::MmapVec;pub use types::RecordMeta;pub use types::Segmented;pub use types::SegmentedIter;pub use types::SegmentedMarker;pub use types::SegmentedRecord;pub use types::StructLayout;
ModulesΒ§
- features
- Optional features for memmap3
- prelude
- Prelude module for convenient imports
- types
- Safe memory-mapped data structures for persistent storage
MacrosΒ§
- hashmap_
index - Macro to enable
my_map["key"] << "value"syntax
StructsΒ§
- Mmap
- A handle to an immutable memory mapped buffer.
- MmapMut
- A handle to a mutable memory mapped buffer.
- Mmap
Options - A memory map builder, providing advanced options and flags for specifying memory map behavior.
- MmapRaw
- A handle to a raw memory mapped buffer.
- Mmap
RawDescriptor - Remap
Options - Options for
Mmap::remapandMmapMut::remap.
EnumsΒ§
- Advice
- Values supported by
Mmap::adviseandMmapMut::advisefunctions. - Unchecked
Advice - Values supported by
Mmap::unchecked_adviseandMmapMut::unchecked_advisefunctions.
TraitsΒ§
Attribute MacrosΒ§
- mmap_
struct - Attribute macro for memory-mapped structs.