diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2021-09-13 23:21:23 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2021-09-13 23:21:23 -0300 |
commit | f0ccda7d7402c532bce560fe743c07d1444b80ce (patch) | |
tree | 58f10b8b277a9f393a5b9e7b113d4a58ac6726ee /src/data/managers.rs | |
parent | 22e7a62e9529cc4a59941e8342f69d0f6ded60f9 (diff) |
Ditch Duration in DataSourceBase rustgan
Diffstat (limited to 'src/data/managers.rs')
-rw-r--r-- | src/data/managers.rs | 103 |
1 files changed, 25 insertions, 78 deletions
diff --git a/src/data/managers.rs b/src/data/managers.rs index 9a82a8e..7b34f80 100644 --- a/src/data/managers.rs +++ b/src/data/managers.rs @@ -23,7 +23,6 @@ use std::collections::BTreeSet; use std::error; use std::fmt; use std::sync::Arc; -use std::time::Duration; use impl_trait::impl_trait; use qcell::{QCell, QCellOwner}; @@ -35,6 +34,7 @@ use super::effective::EffectiveDataSource; use super::effective::EffectiveKind; use super::kinds::{InstanceTitle, InstanceBaseUrl, RepoListUrl, ProjectFork}; use super::sources::DefaultsDataSource; +use super::Update; /// A wrapper around multiple [`DataSource`]s of [`ProjectFork`]s. /// @@ -44,7 +44,6 @@ use super::sources::DefaultsDataSource; #[derive(Default)] pub struct RepoListManager { repos: Vec<Box<dyn DataSource<EffectiveKind<ProjectFork>> + Send + Sync>>, - durations: Vec<Option<Duration>>, valid: usize, } @@ -63,7 +62,6 @@ pub struct RepoListManager { pub struct ConfigManager { owner: QCellOwner, resources: Vec<Resource>, - durations: Vec<Option<Duration>>, // add_source can be called after update. valid: usize, } @@ -153,14 +151,9 @@ impl_trait! { } impl trait DataSourceBase { - /// Updates the contained `DataSource`s, and returns the shortest - /// duration for the next update. + /// Updates the contained `DataSource`s, and returns any relevant + /// update stats. /// - /// # Errors - /// - /// Returns an error if any `DataSource` returns an error. Always - /// updates all `DataSource`s. - /// /// # Examples /// /// ``` @@ -170,39 +163,19 @@ impl_trait! { /// /// let mut repos = RepoListManager::default(); /// repos.add_source(DefaultsDataSource); - /// let (duration, result) = repos.update(); - /// # assert!(duration.is_none()); - /// # assert!(result.is_ok()); + /// let update = repos.update(); + /// # assert!(update.errors.is_empty()); /// ``` - fn update(&mut self) -> ( - Option<Duration>, - Result<(), Box<dyn error::Error + Send + Sync + 'static>>, - ) { - let mut results = Vec::with_capacity(self.repos.len()); - let mut ok = true; - self.durations.resize(self.repos.len(), None); - Iterator::zip( - self.repos.iter_mut(), - self.durations.iter_mut(), - ).for_each(|e| { - if !matches!(*e.1, Some(d) if d.is_zero()) { - results.push(Ok(())); - return; - } - let (duration, result) = e.0.update(); - *e.1 = duration; - ok &= result.is_ok(); - results.push(result); + fn update(&mut self) -> Update { + let mut errors = Vec::new(); + self.repos.iter_mut().for_each(|e| { + let ret = e.update(); + errors.extend(ret.errors); }); - let try_min = self.durations.iter().flatten().min().copied(); - let min = try_min.unwrap_or(Duration::ZERO); - for duration in self.durations.iter_mut().flatten() { - *duration -= min; + self.valid = self.repos.len(); + Update { + errors: errors, } - self.valid = results.len(); - (try_min, ok.then(|| ()).ok_or_else(|| { - Box::new(MultiResult { results }) as _ - })) } /// Returns whether this data source contains any (valid) data. @@ -304,14 +277,9 @@ impl_trait! { } impl trait DataSourceBase { - /// Updates the contained `DataSource`s, and returns the shortest - /// duration for the next update. + /// Updates the contained `DataSource`s, and returns any relevant + /// update stats. /// - /// # Errors - /// - /// Returns an error if any `DataSource` returns an error. Always - /// updates all `DataSource`s. - /// /// # Examples /// /// ``` @@ -320,41 +288,20 @@ impl_trait! { /// /// let mut cm = ConfigManager::default(); /// cm.add_defaults(); - /// let (duration, result) = cm.update(); - /// # assert!(duration.is_none()); - /// # assert!(result.is_ok()); + /// let update = cm.update(); + /// # assert!(update.errors.is_empty()); /// ``` - fn update(&mut self) -> ( - Option<Duration>, - Result<(), Box<dyn error::Error + Send + Sync + 'static>>, - ) { + fn update(&mut self) -> Update { let owner = &mut self.owner; - let mut results = Vec::with_capacity(self.resources.len()); - let mut ok = true; - self.durations.resize(self.resources.len(), None); - Iterator::zip( - self.resources.iter(), - self.durations.iter_mut(), - ).for_each(|e| { - if !matches!(*e.1, Some(d) if d.is_zero()) { - results.push(Ok(())); - return; - } - let ret = owner.rw(&*e.0.base).update(); - let (duration, result) = ret; - *e.1 = duration; - ok &= result.is_ok(); - results.push(result); + let mut errors = Vec::new(); + self.resources.iter().for_each(|e| { + let ret = owner.rw(&*e.base).update(); + errors.extend(ret.errors); }); - let try_min = self.durations.iter().flatten().min().copied(); - let min = try_min.unwrap_or(Duration::ZERO); - for duration in self.durations.iter_mut().flatten() { - *duration -= min; + self.valid = self.resources.len(); + Update { + errors: errors, } - self.valid = results.len(); - (try_min, ok.then(|| ()).ok_or_else(|| { - Box::new(MultiResult { results }) as _ - })) } /// Returns whether this data source contains any (valid) data. |