Skip to main content

Crate handle_rs

Crate handle_rs 

Source
Expand description

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

Notes:

  • Index 0 will be used to check whether if it is valid;null will be get_handle().idx == 0
  • alive_iter/alive_iter_mut will filter handle with idx==0, then you can iterate item which is valid

§example

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

pub fn handle_array_sample(){
    //new HandleArray
    let mut ha = HandleArray::<Item>::new(1000);
    //add_item
    let handle0 = ha.add_item(Item {value:0.1,handle:Handle::default()});
    let handle1 = ha.add_item(Item {value:0.2,handle:Handle::default()});
    let handle2 = ha.add_item(Item {value:0.3,handle:Handle::default()});
    println!("{}",ha.get(handle0));
    //alive_iter_mut
    println!("--change value--");
    for turtle in ha.alive_iter_mut() {
        turtle.1.value += 0.1;
    }
    //alive_iter
    for turtle in ha.alive_iter() {
        println!("{}", turtle.1);
    }
    //remove_item
    ha.remove_item(handle0);
    println!("--remove now---");
    for turtle in ha.alive_iter() {
        println!("alive {}", turtle.1);
    }
    println!("--add again---");
    let handle3 = ha.add_item(Item {value:0.5,handle:Handle::default()});
    let handle4 = ha.add_item(Item {value:0.6,handle:Handle::default()});
    for turtle in ha.alive_iter() {
        println!("alive {}", turtle.1);
    }
}



#[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)
    }
}

Structs§

Handle
HandleArray
HandleArrayIter
HandleArrayIterMut

Traits§

IHandleArrayItem