added test/example data structure SingletonStore

this probably won"t ever be useful but w/e

Fortune for transfer's current commit: Small curse − 小凶
master
Avril 3 years ago
parent f22bda2f9a
commit d0b58f6140
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -20,6 +20,7 @@ use color_eyre::{
use futures::Future; use futures::Future;
mod sock; mod sock;
mod store;
mod config; mod config;
fn setup() -> eyre::Result<()> fn setup() -> eyre::Result<()>

@ -0,0 +1,58 @@
//! Arbitrary object store
use std::any::{
TypeId,
Any,
};
use std::collections::HashMap;
/// Arbitrarily typed singleton store.
///
/// Stores one object per type.
#[derive(Debug)]
pub struct SingletonStore
{
objects: HashMap<TypeId, Box<dyn Any + 'static>>,
}
#[inline] fn unbox_to<T: Any>(b: Box<dyn Any + 'static>) -> Option<T>
{
b.downcast().map(|ok| *ok).ok()
}
impl SingletonStore
{
pub fn has<T: Any>(&self) -> bool
{
self.objects.contains_key(&TypeId::of::<T>())
}
pub fn remove<T: Any>(&mut self) -> Option<T>
{
self.objects.remove(&TypeId::of::<T>())
.and_then(|x| x.downcast().ok())
.map(|x| *x)
}
pub fn insert<T: Any>(&mut self, value: T) -> Option<T>
{
self.objects.insert(TypeId::of::<T>(), Box::new(value))
.and_then(unbox_to)
}
pub fn get_mut<T: Any>(&mut self) -> Option<&mut T>
{
self.objects.get_mut(&TypeId::of::<T>())
.and_then(|x| x.downcast_mut())
}
pub fn get<T: Any>(&self) -> Option<&T>
{
self.objects.get(&TypeId::of::<T>())
.and_then(|x| x.downcast_ref())
}
pub fn new() -> Self
{
Self {
objects: HashMap::new(),
}
}
}
Loading…
Cancel
Save