prealloc 0.1.0

Build time heap-like memory preallocation.
Documentation
  • Coverage
  • 100%
    1 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 27.25 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 22s Average build duration of successful builds.
  • all releases: 22s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • debuti/prealloc
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • debuti

Prealloc allows access to static memory as if it were a heap

Quickstart

Installation

Add this dependency to your Cargo.toml:

[dependencies]

prealloc = "0.1.0"

Configuration File

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
    }
]

Example

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.

License

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.