Files
@ 1f0d50fa632f
Branch filter:
Location: CSY/reowolf/src/protocol/arena.rs - annotation
1f0d50fa632f
1.8 KiB
application/rls-services+xml
first pass on arena/index refactoring
44a98be4e4b4 3f236d37dad1 3f236d37dad1 3f236d37dad1 44a98be4e4b4 3f236d37dad1 33ea10021de4 3f236d37dad1 3f236d37dad1 44a98be4e4b4 c2b16314e873 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 33ea10021de4 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 33ea10021de4 33ea10021de4 3f236d37dad1 b194b811d0cc b194b811d0cc b194b811d0cc 379cffa23df8 379cffa23df8 379cffa23df8 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 3f236d37dad1 33ea10021de4 | use crate::common::*;
use core::hash::Hash;
use core::marker::PhantomData;
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Id<T> {
pub(crate) index: u32,
_phantom: PhantomData<T>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub(crate) struct Arena<T> {
store: Vec<T>,
}
//////////////////////////////////
impl<T> Debug for Id<T> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.debug_struct("Id").field("index", &self.index).finish()
}
}
impl<T> Clone for Id<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Id<T> {}
impl<T> PartialEq for Id<T> {
fn eq(&self, other: &Self) -> bool {
self.index.eq(&other.index)
}
}
impl<T> Eq for Id<T> {}
impl<T> Hash for Id<T> {
fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
self.index.hash(h);
}
}
impl<T> Arena<T> {
pub fn new() -> Self {
Self { store: vec![] }
}
pub fn alloc_with_id(&mut self, f: impl FnOnce(Id<T>) -> T) -> Id<T> {
use std::convert::TryFrom;
let id = Id {
index: u32::try_from(self.store.len()).expect("Out of capacity!"),
_phantom: Default::default(),
};
self.store.push(f(id));
id
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.store.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.store.iter_mut()
}
pub fn len(&self) -> usize {
self.store.len()
}
}
impl<T> core::ops::Index<Id<T>> for Arena<T> {
type Output = T;
fn index(&self, id: Id<T>) -> &Self::Output {
self.store.index(id.index as usize)
}
}
impl<T> core::ops::IndexMut<Id<T>> for Arena<T> {
fn index_mut(&mut self, id: Id<T>) -> &mut Self::Output {
self.store.index_mut(id.index as usize)
}
}
|