diff --git a/youmubot-db/src/lib.rs b/youmubot-db/src/lib.rs index aa82582..ccf8576 100644 --- a/youmubot-db/src/lib.rs +++ b/youmubot-db/src/lib.rs @@ -3,6 +3,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*}; use std::collections::HashMap; use std::path::Path; +use std::sync::Arc; /// GuildMap defines the guild-map type. /// It is basically a HashMap from a GuildId to a data structure. @@ -10,7 +11,7 @@ pub type GuildMap = HashMap; /// The generic DB type we will be using. pub struct DB(std::marker::PhantomData); impl serenity::prelude::TypeMapKey for DB { - type Value = FileDatabase; + type Value = Arc>; } impl DB @@ -24,32 +25,33 @@ where dbg!(e); db.save() })?; - data.insert::>(db); + data.insert::>(Arc::new(db)); Ok(()) } /// Open a previously inserted DB. - pub fn open(data: &ShareMap) -> DBWriteGuard<'_, T> { - data.get::().expect("DB initialized").into() + pub fn open(data: &ShareMap) -> DBWriteGuard { + data.get::().expect("DB initialized").clone().into() } } /// The write guard for our FileDatabase. /// It wraps the FileDatabase in a write-on-drop lock. -pub struct DBWriteGuard<'a, T>(&'a FileDatabase) +#[derive(Debug)] +pub struct DBWriteGuard(Arc>) where T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned; -impl<'a, T> From<&'a FileDatabase> for DBWriteGuard<'a, T> +impl From>> for DBWriteGuard where T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, { - fn from(v: &'a FileDatabase) -> Self { + fn from(v: Arc>) -> Self { DBWriteGuard(v) } } -impl<'a, T> DBWriteGuard<'a, T> +impl DBWriteGuard where T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, { @@ -62,12 +64,3 @@ where (*self).0.borrow_data_mut() } } - -impl<'a, T> Drop for DBWriteGuard<'a, T> -where - T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, -{ - fn drop(&mut self) { - self.0.save().expect("Save succeed") - } -} diff --git a/youmubot/src/commands/admin/soft_ban.rs b/youmubot/src/commands/admin/soft_ban.rs index f142773..24f48ad 100644 --- a/youmubot/src/commands/admin/soft_ban.rs +++ b/youmubot/src/commands/admin/soft_ban.rs @@ -33,8 +33,7 @@ pub fn soft_ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResu }; let guild = msg.guild_id.ok_or(Error::from("Command is guild only"))?; - let db = ctx.data.read(); - let db = SoftBans::open(&*db); + let db = SoftBans::open(&*ctx.data.read()); let mut db = db.borrow_mut()?; let mut server_ban = db.get_mut(&guild).and_then(|v| match v { ServerSoftBans::Unimplemented => None, @@ -95,8 +94,7 @@ pub fn soft_ban_init(ctx: &mut Context, msg: &Message, mut args: Args) -> Comman ))); } // Check if we already set up - let db = ctx.data.read(); - let db = SoftBans::open(&*db); + let db = SoftBans::open(&*ctx.data.read()); let mut db = db.borrow_mut()?; let server = db .get(&guild.id) diff --git a/youmubot/src/commands/osu/mod.rs b/youmubot/src/commands/osu/mod.rs index 159acfe..4f63d2d 100644 --- a/youmubot/src/commands/osu/mod.rs +++ b/youmubot/src/commands/osu/mod.rs @@ -95,8 +95,7 @@ pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { let user: Option = osu.user(UserID::Auto(user), |f| f)?; match user { Some(u) => { - let db = ctx.data.read(); - let db = OsuSavedUsers::open(&db); + let db = OsuSavedUsers::open(&*ctx.data.read()); let mut db = db.borrow_mut()?; db.insert(