Skip to content

Commit

Permalink
Update LangString's for_each to not require Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
walfie committed May 24, 2020
1 parent 3515ac2 commit 2972c2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 16 additions & 14 deletions src/raid_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -142,28 +142,31 @@ struct BossMap {

impl BossMap {
fn new(mut bosses: Vec<Boss>, 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
}

Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 2972c2b

Please sign in to comment.