platform-mem
A Rust library for low-level memory management with unified interface for allocator-backed and memory-mapped file storage.
Overview
platform-mem provides the RawMem trait that abstracts over different memory backends:
- Allocator-based memory (
Global,System,Alloc<T, A>) - uses Rust's allocator API - Memory-mapped files (
FileMapped,TempFile) - usesmmapfor persistent or temporary file-backed storage
This allows writing generic code that works with any memory backend, making it easy to switch between heap allocation and file-mapped storage.
Features
- Unified
RawMemtrait - common interface for growing, shrinking, and accessing memory - Type-erased memory via
ErasedMem- enables dynamic dispatch withBox<dyn ErasedMem<Item = T>> - Memory-mapped files - persistent storage with automatic page management
- Temporary file storage - anonymous file-backed memory that's cleaned up on drop
- Safe growth operations -
grow_filled,grow_zeroed,grow_from_slice, and more - Thread-safe - all memory types implement
Send + Sync - Async memory operations (optional) - async mmap access with
AsyncFileMemvia dedicated I/O thread
Installation
Add to your Cargo.toml:
[]
= "0.1"
Note: This crate uses Rust edition 2024 and works on stable Rust. It uses the allocator-api2 crate to provide allocator API functionality on stable Rust.
Optional Features
To enable async memory operations:
[]
= { = "0.1", = ["async"] }
Usage
Basic Example with Global Allocator
use ;
Memory-Mapped File Storage
use ;
Temporary File Storage
use ;
Generic Code with RawMem
use RawMem;
Type-Erased Memory with ErasedMem
use ;
Async File Memory (requires async feature)
use AsyncFileMem;
async
API Overview
RawMem Trait
The core trait providing memory operations:
| Method | Description |
|---|---|
allocated() |
Returns a slice of the initialized memory |
allocated_mut() |
Returns a mutable slice of the initialized memory |
grow(addition, fill) |
Grows memory by addition elements with custom initialization |
shrink(cap) |
Shrinks memory by cap elements |
grow_filled(cap, value) |
Grows and fills with cloned values |
grow_zeroed(cap) |
Grows and zero-initializes (unsafe for non-zeroable types) |
grow_from_slice(src) |
Grows and copies from a slice |
grow_with(addition, f) |
Grows and initializes with a closure |
grow_within(range) |
Grows by cloning a sub-range of existing data |
grow_assumed(cap) |
Grows assuming data is already initialized (unsafe) |
Memory Types
| Type | Description |
|---|---|
Global<T> |
Uses Rust's global allocator |
System<T> |
Uses the system allocator |
Alloc<T, A> |
Generic over any Allocator |
FileMapped<T> |
Memory-mapped file storage |
TempFile<T> |
Temporary file-backed memory |
AsyncFileMem<T> |
Async mmap access via dedicated I/O thread (requires async feature) |
Error Handling
The crate defines an Error enum with these variants:
CapacityOverflow- Requested capacity exceedsisize::MAXbytesOverGrow- Tried to grow more than available spaceAllocError- Allocator failed to allocate/reallocateSystem- I/O error from file operations
License
This project is released into the public domain under the Unlicense.
Related Projects
- doublets-rs - Doublet links data structure using this memory library
- LinksPlatform - The Links Platform organization