Crates.io | prealloc |
lib.rs | prealloc |
version | 0.1.0 |
source | src |
created_at | 2024-08-10 23:15:15.82843 |
updated_at | 2024-08-10 23:15:15.82843 |
description | Build time heap-like memory preallocation. |
homepage | |
repository | https://github.com/debuti/prealloc |
max_upload_size | |
id | 1332791 |
size | 27,734 |
Prealloc allows access to static memory as if it were a heap
Add this dependency to your Cargo.toml
:
[dependencies]
prealloc = "0.1.0"
Create a JSON configuration file (ex. config.json
) that defines the items, their types, and counts:
[
{
"name": "MyStruct",
"type": "my_project::MyStruct",
"size": 3
},
{
"name": "MyEnumUseCaseA",
"type": "my_project::MyEnum",
"size": 5
},
{
"name": "MyEnumUseCaseB",
"type": "my_project::MyEnum",
"size": 2
}
]
use prealloc::prealloc_from_config;
prealloc_from_config!("path/to/config.json");
#[derive(Debug)]
#[allow(dead_code)]
pub enum MyEnum {
L(u32),
R,
}
impl Drop for MyEnum {
fn drop(&mut self) {
println!("Dropping MyEnum located at {self:p}");
}
}
fn main() {
for idx in 0..=5 {
// The dispatch_static macro retrieves one preallocated element and initializes it.
if let Some(item) = dispatch_static!(MyEnumUseCaseA, MyEnum::L(33)) {
println!("Retrieved {idx}: {item:p}");
} else {
println!("MyEnumUseCaseA depleted");
}
}
}
Discover more examples with cargo run --example
on this crate.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See the LICENSE-APACHE and LICENSE-MIT files for details.