diff --git a/src/model.rs b/src/model.rs index 69e9e2e..8beaa87 100644 --- a/src/model.rs +++ b/src/model.rs @@ -102,9 +102,12 @@ impl LangString { } } - pub fn for_each(&self, f: impl FnMut(&BossName) -> () + Copy) { - self.ja.iter().for_each(f); - self.en.iter().for_each(f); + pub fn for_each(&self, mut f: impl FnMut(&BossName)) { + for opt in &[self.ja.as_ref(), self.en.as_ref()] { + if let Some(value) = opt { + f(value) + } + } } pub fn merge(&self, other: &LangString) -> Self { diff --git a/src/raid_handler.rs b/src/raid_handler.rs index af8a445..6140661 100644 --- a/src/raid_handler.rs +++ b/src/raid_handler.rs @@ -107,7 +107,7 @@ impl BossEntry { &self.history } - fn for_each_key(&self, f: impl FnMut(&BossName) -> () + Copy) { + fn for_each_key(&self, f: impl FnMut(&BossName)) { self.boss.name.for_each(f) } } @@ -142,28 +142,31 @@ struct BossMap { impl BossMap { fn new(mut bosses: Vec, history_size: usize, broadcast_capacity: usize) -> Self { - let this = Self { - map: DashMap::new(), - vec: ArcSwap::from_pointee(Vec::new()), - waiting: DashMap::new(), - history_size, - broadcast_capacity, - }; - bosses.sort_by_key(|boss| boss.name.canonical().cloned()); bosses.dedup_by(|a, b| a.name == b.name); + let mut init = Vec::new(); + for boss in bosses { let (tx, _) = broadcast::channel(broadcast_capacity); - let entry = BossEntry { + let entry = Arc::new(BossEntry { boss, history: RwLock::new(CircularQueue::with_capacity(history_size)), broadcast: tx, - }; + }); - this.insert(&Arc::new(entry)); + entry.for_each_key(|key| init.push((key.clone(), entry.clone()))); } + let this = Self { + map: DashMap::from_iter(init), + vec: ArcSwap::from_pointee(Vec::new()), + waiting: DashMap::new(), + history_size, + broadcast_capacity, + }; + + this.update_vec(); this } @@ -287,8 +290,7 @@ impl RaidHandlerInner { } pub fn update_image_hash(&self, boss_name: &BossName, image_hash: ImageHash) { - let guard_opt = self.bosses.get(boss_name); - let guard = match guard_opt { + let guard = match self.bosses.get(boss_name) { Some(g) => g, None => return, };