From ddaa32c9f5f2e99cc6f18d6c8b4b77188e119d2c Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 21 Mar 2021 19:19:49 +0100 Subject: [PATCH] More async in traits --- src/calendar/cached_calendar.rs | 8 ++--- src/calendar/remote_calendar.rs | 8 ++--- src/provider.rs | 26 +++++++------- src/traits.rs | 12 +++---- tests/sync.rs | 62 ++++++++++++++++----------------- 5 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index e93d219..3a7fd54 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -57,11 +57,11 @@ impl PartialCalendar for CachedCalendar { self.supported_components } - fn add_item(&mut self, item: Item) { + async fn add_item(&mut self, item: Item) { self.items.insert(item.id().clone(), item); } - fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box> { + async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box> { if let None = self.items.remove(item_id) { return Err("This key does not exist.".into()); } @@ -97,11 +97,11 @@ impl PartialCalendar for CachedCalendar { map } - fn get_item_ids(&mut self) -> HashSet { + async fn get_item_ids(&mut self) -> HashSet { self.items.keys().cloned().collect() } - fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> { + async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item> { self.items.get_mut(id) } } diff --git a/src/calendar/remote_calendar.rs b/src/calendar/remote_calendar.rs index 4574603..0fb6cde 100644 --- a/src/calendar/remote_calendar.rs +++ b/src/calendar/remote_calendar.rs @@ -44,24 +44,24 @@ impl PartialCalendar for RemoteCalendar { } /// Get the IDs of all current items in this calendar - fn get_item_ids(&mut self) -> HashSet { + async fn get_item_ids(&mut self) -> HashSet { log::error!("Not implemented"); HashSet::new() } /// Returns a particular item - fn get_item_by_id_mut(&mut self, _id: &ItemId) -> Option<&mut Item> { + async fn get_item_by_id_mut<'a>(&'a mut self, _id: &ItemId) -> Option<&'a mut Item> { log::error!("Not implemented"); None } /// Add an item into this calendar - fn add_item(&mut self, _item: Item) { + async fn add_item(&mut self, _item: Item) { log::error!("Not implemented"); } /// Remove an item from this calendar - fn delete_item(&mut self, _item_id: &ItemId) -> Result<(), Box> { + async fn delete_item(&mut self, _item_id: &ItemId) -> Result<(), Box> { log::error!("Not implemented"); Ok(()) } diff --git a/src/provider.rs b/src/provider.rs index 58f2db8..3fcbdcc 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -19,7 +19,7 @@ where L: CalDavSource + SyncSlave, T: CompleteCalendar, S: CalDavSource, - U: PartialCalendar, + U: PartialCalendar + Sync + Send, { /// The remote server server: S, @@ -35,7 +35,7 @@ where L: CalDavSource + SyncSlave, T: CompleteCalendar, S: CalDavSource, - U: PartialCalendar, + U: PartialCalendar + Sync + Send, { /// Create a provider. /// @@ -83,10 +83,10 @@ where Some(date) => cal_local.get_items_deleted_since(date).await, }; if last_sync.is_some() { - let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids()); + let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids().await).await; for server_del_id in server_deletions { // Even in case of conflicts, "the server always wins", so it is safe to remove tasks from the local cache as soon as now - if let Err(err) = cal_local.delete_item(&server_del_id) { + if let Err(err) = cal_local.delete_item(&server_del_id).await { log::error!("Unable to remove local item {}: {}", server_del_id, err); } @@ -118,7 +118,7 @@ where // ...upload local deletions,... for local_del_id in local_dels { - if let Err(err) = cal_server.delete_item(&local_del_id) { + if let Err(err) = cal_server.delete_item(&local_del_id).await { log::error!("Unable to remove remote item {}: {}", local_del_id, err); } } @@ -126,15 +126,15 @@ where // ...and upload local changes for (local_mod_id, local_mod) in local_mods { // Conflicts are no longer in local_mods - if let Err(err) = cal_server.delete_item(&local_mod_id) { + if let Err(err) = cal_server.delete_item(&local_mod_id).await { log::error!("Unable to remove remote item (before an update) {}: {}", local_mod_id, err); } // TODO: should I add a .update_item()? - cal_server.add_item(local_mod.clone()); + cal_server.add_item(local_mod.clone()).await; } - remove_from_calendar(&conflicting_tasks, &mut (*cal_local)); - move_to_calendar(&mut tasks_to_add, &mut (*cal_local)); + remove_from_calendar(&conflicting_tasks, &mut (*cal_local)).await; + move_to_calendar(&mut tasks_to_add, &mut (*cal_local)).await; } self.local.update_last_sync(None); @@ -144,18 +144,18 @@ where } -fn move_to_calendar(items: &mut Vec, calendar: &mut C) { +async fn move_to_calendar(items: &mut Vec, calendar: &mut C) { while items.len() > 0 { let item = items.remove(0); log::warn!("Moving {} to calendar", item.name()); - calendar.add_item(item); + calendar.add_item(item).await; } } -fn remove_from_calendar(ids: &Vec, calendar: &mut C) { +async fn remove_from_calendar(ids: &Vec, calendar: &mut C) { for id in ids { log::info!(" Removing {:?} from calendar", id); - if let Err(err) = calendar.delete_item(id) { + if let Err(err) = calendar.delete_item(id).await { log::warn!("Unable to delete item {:?} from calendar: {}", id, err); } } diff --git a/src/traits.rs b/src/traits.rs index c0071ed..674f0b8 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -45,16 +45,16 @@ pub trait PartialCalendar { -> HashMap; /// Get the IDs of all current items in this calendar - fn get_item_ids(&mut self) -> HashSet; + async fn get_item_ids(&mut self) -> HashSet; /// Returns a particular item - fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item>; + async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item>; /// Add an item into this calendar - fn add_item(&mut self, item: Item); + async fn add_item(&mut self, item: Item); /// Remove an item from this calendar - fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box>; + async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box>; /// Returns whether this calDAV calendar supports to-do items @@ -68,8 +68,8 @@ pub trait PartialCalendar { } /// Finds the IDs of the items that are missing compared to a reference set - fn find_deletions_from(&mut self, reference_set: HashSet) -> HashSet { - let current_items = self.get_item_ids(); + async fn find_deletions_from(&mut self, reference_set: HashSet) -> HashSet { + let current_items = self.get_item_ids().await; reference_set.difference(¤t_items).map(|id| id.clone()).collect() } } diff --git a/tests/sync.rs b/tests/sync.rs index a1254a5..ba0ea02 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -85,19 +85,19 @@ async fn populate_test_provider() -> Provider Provider Provider