mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Added store
This commit is contained in:
@ -34,6 +34,7 @@ pub mod terrain;
|
|||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod vol;
|
pub mod vol;
|
||||||
pub mod volumes;
|
pub mod volumes;
|
||||||
|
pub mod store;
|
||||||
|
|
||||||
/// The networking module containing high-level wrappers of `TcpListener` and
|
/// The networking module containing high-level wrappers of `TcpListener` and
|
||||||
/// `TcpStream` (`PostOffice` and `PostBox` respectively) and data types used by
|
/// `TcpStream` (`PostOffice` and `PostBox` respectively) and data types used by
|
||||||
|
61
common/src/store.rs
Normal file
61
common/src/store.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
use std::{
|
||||||
|
fmt,
|
||||||
|
hash,
|
||||||
|
cmp::{PartialEq, Eq},
|
||||||
|
marker::PhantomData,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Id<T>(usize, PhantomData<T>);
|
||||||
|
|
||||||
|
impl<T> Id<T> {
|
||||||
|
pub fn id(&self) -> usize { self.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Copy for Id<T> {}
|
||||||
|
impl<T> Clone for Id<T> {
|
||||||
|
fn clone(&self) -> Self { Self(self.0, PhantomData) }
|
||||||
|
}
|
||||||
|
impl<T> Eq for Id<T> {}
|
||||||
|
impl<T> PartialEq for Id<T> {
|
||||||
|
fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
|
||||||
|
}
|
||||||
|
impl<T> fmt::Debug for Id<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Id<{}>({})", std::any::type_name::<T>(), self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> hash::Hash for Id<T> {
|
||||||
|
fn hash<H: hash::Hasher>(&self, h: &mut H) {
|
||||||
|
self.0.hash(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Store<T> {
|
||||||
|
items: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for Store<T> {
|
||||||
|
fn default() -> Self { Self { items: Vec::new() } }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Store<T> {
|
||||||
|
pub fn get(&self, id: Id<T>) -> &T { self.items.get(id.0).unwrap() }
|
||||||
|
|
||||||
|
pub fn get_mut(&mut self, id: Id<T>) -> &mut T { self.items.get_mut(id.0).unwrap() }
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = &T> { self.items.iter() }
|
||||||
|
|
||||||
|
pub fn iter_ids(&self) -> impl Iterator<Item = (Id<T>, &T)> {
|
||||||
|
self
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, item)| (Id(i, PhantomData), item))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, item: T) -> Id<T> {
|
||||||
|
let id = Id(self.items.len(), PhantomData);
|
||||||
|
self.items.push(item);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user