Skip to content

Commit

Permalink
feat: drop redundant lifetime constraints from WatchableData::should_…
Browse files Browse the repository at this point in the history
…notify, add default implementation for WatchableData::update
  • Loading branch information
Iipin committed Jan 14, 2024
1 parent 03a5652 commit f5abab5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
11 changes: 7 additions & 4 deletions chombot-common/src/data_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ use tokio::time::sleep;

const DATA_UPDATE_INTERVAL: Duration = Duration::from_secs(60 * 10);

pub trait WatchableData {
pub trait WatchableData: Sized {
type Diff;

#[must_use]
fn should_notify<'a>(&'a self, new: &'a Self) -> Option<Self::Diff>;
fn update(&mut self, new: Self);
fn should_notify(&self, new: &Self) -> Option<Self::Diff>;

fn update(&mut self, new: Self) {
*self = new;
}

Check warning on line 20 in chombot-common/src/data_watcher.rs

View check run for this annotation

Codecov / codecov/patch

chombot-common/src/data_watcher.rs#L18-L20

Added lines #L18 - L20 were not covered by tests
}

impl<T: WatchableData> WatchableData for Option<T> {
type Diff = T::Diff;

#[must_use]
fn should_notify<'a>(&'a self, new: &'a Self) -> Option<T::Diff> {
fn should_notify(&self, new: &Self) -> Option<T::Diff> {

Check warning on line 27 in chombot-common/src/data_watcher.rs

View check run for this annotation

Codecov / codecov/patch

chombot-common/src/data_watcher.rs#L27

Added line #L27 was not covered by tests
match (&self, &new) {
(Some(o), Some(n)) => o.should_notify(n),
_ => None,
Expand Down
6 changes: 1 addition & 5 deletions chombot-common/src/tournaments_watcher/ema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ impl Tournaments {
impl WatchableData for Tournaments {
type Diff = TournamentStatuses;

fn should_notify<'a>(&'a self, new: &'a Self) -> Option<Self::Diff> {
fn should_notify(&self, new: &Self) -> Option<Self::Diff> {

Check warning on line 49 in chombot-common/src/tournaments_watcher/ema.rs

View check run for this annotation

Codecov / codecov/patch

chombot-common/src/tournaments_watcher/ema.rs#L49

Added line #L49 was not covered by tests
if self == new {
None
} else {
Some(tournaments_diff(self, new))
}
}

fn update(&mut self, new: Self) {
*self = new;
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
6 changes: 1 addition & 5 deletions chombot-kcc/src/ranking_watcher/usma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ impl Ranking {
impl WatchableData for Ranking {
type Diff = Vec<RankingEntry>;

fn should_notify<'a>(&'a self, new: &'a Self) -> Option<Vec<RankingEntry>> {
fn should_notify(&self, new: &Self) -> Option<Vec<RankingEntry>> {

Check warning on line 51 in chombot-kcc/src/ranking_watcher/usma.rs

View check run for this annotation

Codecov / codecov/patch

chombot-kcc/src/ranking_watcher/usma.rs#L51

Added line #L51 was not covered by tests
let new_changed = new.get_changed();
if new_changed.is_empty() || new_changed == self.get_changed() {
None
} else {
Some(new_changed.into_iter().cloned().collect())
}
}

fn update(&mut self, new: Self) {
*self = new;
}
}

fn first_element_child<'a>(e: &'a ElementRef) -> Option<&'a Element> {
Expand Down

0 comments on commit f5abab5

Please sign in to comment.