| Crates.io | sparse-mem |
| lib.rs | sparse-mem |
| version | 0.2.0 |
| created_at | 2025-06-14 20:24:18.632266+00 |
| updated_at | 2025-07-02 19:55:47.300643+00 |
| description | Fixed-capacity, generation-tracked sparse array implementation optimized for the Swamp VM |
| homepage | |
| repository | https://github.com/swamp/swamp |
| max_upload_size | |
| id | 1712639 |
| size | 41,860 |
Welcome to sparse-mem, a low-level memory layout implementation for
fixed-capacity, generation-tracked sparse arrays designed specifically for the
Swamp VM.
This crate provides the raw memory layout and operations for a sparse array structure that:
Important note: This is not a standard Rust collection! It's designed to work with raw memory layouts designed for VM internals. If you're looking for a more typical Rust sparse slot map implementation with all the Rust safety guarantees, check out my other crate:
👉 sparse-slot (github)
This is a low-level crate meant for VM implementers. You'll be working with raw pointers and unsafe code (living on the edge!):
use std::ptr;
// Allocate memory (you need to handle this part)
let capacity = 10;
let element_size = 4;
let total_size = sparse_mem::layout_size(capacity, element_size);
let memory = vec![0u8; total_size];
unsafe {
// Initialize the sparse array
sparse_mem::init(memory.as_mut_ptr(), capacity, element_size);
// Allocate a slot
if let Some((id, generation)) = sparse_mem::allocate(memory.as_mut_ptr()) {
// Use the id and generation to insert data
let my_data: [u8; 4] = [1, 2, 3, 4];
sparse_mem::insert(memory.as_mut_ptr(), id, my_data.as_ptr());
// Later, check if still valid and remove
if sparse_mem::is_alive(memory.as_mut_ptr(), id, generation) {
sparse_mem::remove(memory.as_mut_ptr(), id, generation);
}
}
}
Since this crate deals with direct raw memory manipulation, almost all functions
are unsafe. Use with caution! ⚠️
It probably only makes sense if you are:
This is an open source project with a single copyright holder. Even though the code is publicly available under LICENSE, I am not accepting external contributions at this time.
You are welcome to:
If you have suggestions or find bugs, please feel free to open an issue for discussion. While I cannot accept pull requests, I value your feedback and engagement with the project.
Thank you for your understanding and interest in the project! 🙏
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2025 Peter Bjorklund. All rights reserved