From 516ee046784acb4a6dc97b844ff3af9a54308e30 Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 21 Mar 2018 16:40:15 +0100 Subject: Replace every Mutex with RwLock --- src/plugins/tell/mod.rs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'src/plugins/tell/mod.rs') diff --git a/src/plugins/tell/mod.rs b/src/plugins/tell/mod.rs index 851fa94..be2e17b 100644 --- a/src/plugins/tell/mod.rs +++ b/src/plugins/tell/mod.rs @@ -1,9 +1,8 @@ use irc::client::prelude::*; - -use std::time::Duration; -use std::sync::Mutex; +use antidote::RwLock; use time; +use std::time::Duration; use chrono::NaiveDateTime; use humantime::format_duration; @@ -18,24 +17,15 @@ use self::error::*; pub mod database; use self::database::Database; -macro_rules! try_lock { - ( $m:expr ) => { - match $m.lock() { - Ok(guard) => guard, - Err(poisoned) => poisoned.into_inner(), - } - } -} - -#[derive(PluginName, Default)] +#[derive(PluginName)] pub struct Tell { - tells: Mutex, + tells: RwLock, } impl Tell { pub fn new(db: T) -> Tell { Tell { - tells: Mutex::new(db), + tells: RwLock::new(db), } } @@ -71,7 +61,8 @@ impl Tell { .map(|channel| client.list_users(&channel)) .map(|option| { option.and_then(|users| { - users.into_iter() + users + .into_iter() .find(|user| user.get_nickname().eq_ignore_ascii_case(&receiver)) }) }) @@ -91,7 +82,7 @@ impl Tell { }; debug!("Saving tell for {:?}", receiver); - try_lock!(self.tells).insert_tell(&tell)?; + self.tells.write().insert_tell(&tell)?; no_receiver = false; } @@ -107,7 +98,7 @@ impl Tell { } fn on_namelist(&self, client: &IrcClient, channel: &str) -> Result<(), FrippyError> { - let receivers = try_lock!(self.tells) + let receivers = self.tells.read() .get_receivers() .context(FrippyErrorKind::Tell)?; @@ -133,7 +124,7 @@ impl Tell { return Ok(()); } - let mut tells = try_lock!(self.tells); + let mut tells = self.tells.write(); let tell_messages = match tells.get_tells(&receiver.to_lowercase()) { Ok(t) => t, -- cgit v1.2.3-70-g09d2