handle_rs 0.1.1

a lib for using handle array in rust more easier
Documentation
  • Coverage
  • 5.26%
    1 out of 19 items documented0 out of 16 items with examples
  • Size
  • Source code size: 12.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 764.41 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ctzcs

handle_rs

Handle-based array with free-list and alive iteration.

Features

  • Sentinel at index 0 to represent invalid/null handle
  • Free-list reuse to avoid reallocations
  • alive_iter / alive_iter_mut filter out invalid entries for safe iteration

Usage

use std::fmt::{Display, Formatter};
use handle_rs::{Handle, HandleArray, IHandleArrayItem};

#[derive(Debug)]
struct Item {
    pub value: f64,
    pub handle: Handle,
}

impl IHandleArrayItem for Item {
    fn get_handle(&self) -> Handle { self.handle }
    fn set_handle(&mut self, handle: Handle) { self.handle = handle; }
}

impl Default for Item {
    fn default() -> Self {
        Item { value: 0.0, handle: Handle { index: 0, generation: 0 } }
    }
}

impl Display for Item {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "value: {} , handle: {}", self.value, self.handle)
    }
}

fn main() {
    let mut ha = HandleArray::<Item>::new(1000);
    let h0 = ha.add_item(Item { value: 0.1, handle: Handle::default() });
    let _ = ha.add_item(Item { value: 0.2, handle: Handle::default() });
    println!("{}", ha.get(h0));

    for (_, item) in ha.alive_iter_mut() {
        item.value += 0.1;
    }

    for (_, item) in ha.alive_iter() {
        println!("alive {}", item);
    }

    ha.remove_item(h0);
    for (_, item) in ha.alive_iter() {
        println!("alive {}", item);
    }
}