You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yuurei/src/state/mod.rs

70 lines
1.3 KiB

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<Arc<RwLock<post::Post>>>,
user_map: HashMap<user::UserID, MaybeVec<ArenaIndex>>,
}
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<Imouto>,
}
/// Shares whole program state
#[derive(Debug, Clone)]
pub struct State(Arc<Oneesan>);
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
}
}