cleanup
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
use std::any::Any;
|
||||
use log::trace;
|
||||
use std::any::{type_name, Any};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StateVector {
|
||||
next_section: AtomicUsize,
|
||||
sections: RwLock<HashMap<SectionIdentifier, Box<RwLock<dyn Any + Send + Sync>>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
||||
pub struct SectionIdentifier(usize);
|
||||
|
||||
pub struct SectionWriter<'a, T> {
|
||||
@@ -18,8 +21,15 @@ pub struct SectionWriter<'a, T> {
|
||||
_phantom_data: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Debug for SectionWriter<'a, T> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "SectionWriter<T={}> {{ id: {:?}, state_vector: {:?} }}", type_name::<T>(), self.id, self.state_vector)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'static> SectionWriter<'a, T> {
|
||||
pub fn get_identifier(&self) -> SectionIdentifier {
|
||||
trace!("SectionWriter<T={}>::get_identifier(self: {self:?})", type_name::<T>());
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
@@ -27,6 +37,7 @@ impl<'a, T: 'static> SectionWriter<'a, T> {
|
||||
where
|
||||
F: FnOnce(&mut T) -> R,
|
||||
{
|
||||
trace!("SectionWriter<T={}>::update(self: {self:?}, f)", type_name::<T>());
|
||||
self.state_vector.sections.clear_poison();
|
||||
let sections = self.state_vector.sections.read().unwrap();
|
||||
let section = sections.get(&self.id).unwrap();
|
||||
@@ -38,6 +49,7 @@ impl<'a, T: 'static> SectionWriter<'a, T> {
|
||||
|
||||
impl StateVector {
|
||||
pub fn new() -> Self {
|
||||
trace!("StateVector::new()");
|
||||
Self {
|
||||
next_section: AtomicUsize::new(0usize),
|
||||
sections: RwLock::new(HashMap::new()),
|
||||
@@ -48,11 +60,17 @@ impl StateVector {
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
{
|
||||
trace!("StateVector::create_section<T={}>(self: {self:?}, initial_value)", type_name::<T>());
|
||||
let id = SectionIdentifier(self.next_section.fetch_add(1usize, Ordering::SeqCst));
|
||||
let lock = Box::new(RwLock::new(initial_value));
|
||||
|
||||
self.sections.clear_poison();
|
||||
self.sections.write().unwrap().insert(id.clone(), lock);
|
||||
let mut sections = self.sections.write().unwrap();
|
||||
if !sections.contains_key(&id) {
|
||||
sections.insert(id.clone(), lock);
|
||||
}
|
||||
|
||||
drop(sections);
|
||||
|
||||
SectionWriter {
|
||||
id,
|
||||
@@ -66,6 +84,7 @@ impl StateVector {
|
||||
T: 'static,
|
||||
F: FnOnce(&T) -> R,
|
||||
{
|
||||
trace!("StateVector::access_section<T={}, F={}, R={}>(self: {self:?}, id: {id:?}, f)", type_name::<T>(), type_name::<F>(), type_name::<R>());
|
||||
self.sections.clear_poison();
|
||||
let Ok(sections) = self.sections.read() else { return None; };
|
||||
let Some(section) = sections.get(id) else { return None; };
|
||||
|
||||
Reference in New Issue
Block a user