use super::*; use std::collections::HashMap; use generational_arena::{ Arena, Index as ArenaIndex, }; use std::sync::Arc; use tokio::sync::RwLock; use std::ops::{Deref, DerefMut}; pub mod session; pub mod user; pub mod post; pub mod body; mod service; pub use service::*; mod freeze; pub use freeze::*; /// Entire post state container #[derive(Debug)] pub struct Imouto { all: Arena>>, user_map: HashMap>, } impl Imouto { /// Create a new empty container pub fn new() -> Self { Self { all: Arena::new(), user_map: HashMap::new(), } } } #[derive(Debug)] /// Entire program state struct Oneesan { posts: RwLock, } /// Shares whole program state #[derive(Debug, Clone)] pub struct State(Arc); impl State { /// Create a new empty state. pub fn new() -> Self { Self(Arc::new(Oneesan { posts: RwLock::new(Imouto::new()), })) } /// Get a reference to the post state container pub async fn imouto(&self) -> tokio::sync::RwLockReadGuard<'_, Imouto> { self.0.posts.read().await } /// Get a mutable reference to the post state container pub async fn imouto_mut(&self) -> tokio::sync::RwLockWriteGuard<'_, Imouto> { self.0.posts.write().await } }