More async in traits

This commit is contained in:
daladim 2021-03-21 19:19:49 +01:00
parent 6ce077ca12
commit ddaa32c9f5
5 changed files with 58 additions and 58 deletions

View file

@ -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<dyn Error>> {
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> {
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<ItemId> {
async fn get_item_ids(&mut self) -> HashSet<ItemId> {
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)
}
}

View file

@ -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<ItemId> {
async fn get_item_ids(&mut self) -> HashSet<ItemId> {
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<dyn Error>> {
async fn delete_item(&mut self, _item_id: &ItemId) -> Result<(), Box<dyn Error>> {
log::error!("Not implemented");
Ok(())
}

View file

@ -19,7 +19,7 @@ where
L: CalDavSource<T> + SyncSlave,
T: CompleteCalendar,
S: CalDavSource<U>,
U: PartialCalendar,
U: PartialCalendar + Sync + Send,
{
/// The remote server
server: S,
@ -35,7 +35,7 @@ where
L: CalDavSource<T> + SyncSlave,
T: CompleteCalendar,
S: CalDavSource<U>,
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<C: PartialCalendar>(items: &mut Vec<Item>, calendar: &mut C) {
async fn move_to_calendar<C: PartialCalendar>(items: &mut Vec<Item>, 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<C: PartialCalendar>(ids: &Vec<ItemId>, calendar: &mut C) {
async fn remove_from_calendar<C: PartialCalendar>(ids: &Vec<ItemId>, 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);
}
}

View file

@ -45,16 +45,16 @@ pub trait PartialCalendar {
-> HashMap<ItemId, &Item>;
/// Get the IDs of all current items in this calendar
fn get_item_ids(&mut self) -> HashSet<ItemId>;
async fn get_item_ids(&mut self) -> HashSet<ItemId>;
/// 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<dyn Error>>;
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
/// 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<ItemId>) -> HashSet<ItemId> {
let current_items = self.get_item_ids();
async fn find_deletions_from(&mut self, reference_set: HashSet<ItemId>) -> HashSet<ItemId> {
let current_items = self.get_item_ids().await;
reference_set.difference(&current_items).map(|id| id.clone()).collect()
}
}

View file

@ -85,19 +85,19 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
// Step 1
// Build the calendar as it was at the time of the sync
let mut calendar = CachedCalendar::new("a list".into(), cal_id.clone(), my_tasks::calendar::SupportedComponents::TODO);
calendar.add_item(task_a);
calendar.add_item(task_b);
calendar.add_item(task_c);
calendar.add_item(task_d);
calendar.add_item(task_e);
calendar.add_item(task_f);
calendar.add_item(task_g);
calendar.add_item(task_h);
calendar.add_item(task_i);
calendar.add_item(task_j);
calendar.add_item(task_k);
calendar.add_item(task_l);
calendar.add_item(task_m);
calendar.add_item(task_a).await;
calendar.add_item(task_b).await;
calendar.add_item(task_c).await;
calendar.add_item(task_d).await;
calendar.add_item(task_e).await;
calendar.add_item(task_f).await;
calendar.add_item(task_g).await;
calendar.add_item(task_h).await;
calendar.add_item(task_i).await;
calendar.add_item(task_j).await;
calendar.add_item(task_k).await;
calendar.add_item(task_l).await;
calendar.add_item(task_m).await;
server.add_calendar(Arc::new(Mutex::new(calendar.clone())));
local.add_calendar(Arc::new(Mutex::new(calendar.clone())));
@ -107,29 +107,29 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
let cal_server = server.get_calendar(cal_id.clone()).await.unwrap();
let mut cal_server = cal_server.lock().unwrap();
cal_server.delete_item(&task_b_id).unwrap();
cal_server.delete_item(&task_b_id).await.unwrap();
cal_server.get_item_by_id_mut(&task_e_id).unwrap().unwrap_task_mut()
cal_server.get_item_by_id_mut(&task_e_id).await.unwrap().unwrap_task_mut()
.set_name("E has been remotely renamed".into());
cal_server.get_item_by_id_mut(&task_f_id).unwrap().unwrap_task_mut()
cal_server.get_item_by_id_mut(&task_f_id).await.unwrap().unwrap_task_mut()
.set_name("F renamed in the server".into());
cal_server.get_item_by_id_mut(&task_g_id).unwrap().unwrap_task_mut()
cal_server.get_item_by_id_mut(&task_g_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_server.get_item_by_id_mut(&task_i_id).unwrap().unwrap_task_mut()
cal_server.get_item_by_id_mut(&task_i_id).await.unwrap().unwrap_task_mut()
.set_name("I renamed in the server".into());
cal_server.delete_item(&task_j_id).unwrap();
cal_server.delete_item(&task_j_id).await.unwrap();
cal_server.get_item_by_id_mut(&task_k_id).unwrap().unwrap_task_mut()
cal_server.get_item_by_id_mut(&task_k_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_server.delete_item(&task_l_id).unwrap();
cal_server.delete_item(&task_l_id).await.unwrap();
let task_n = Item::Task(Task::new("task N (new from server)".into(), Utc::now()));
cal_server.add_item(task_n);
cal_server.add_item(task_n).await;
// Step 3
@ -137,28 +137,28 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
let cal_local = local.get_calendar(cal_id).await.unwrap();
let mut cal_local = cal_local.lock().unwrap();
cal_local.delete_item(&task_c_id).unwrap();
cal_local.delete_item(&task_c_id).await.unwrap();
cal_local.get_item_by_id_mut(&task_d_id).unwrap().unwrap_task_mut()
cal_local.get_item_by_id_mut(&task_d_id).await.unwrap().unwrap_task_mut()
.set_name("D has been locally renamed".into());
cal_local.get_item_by_id_mut(&task_f_id).unwrap().unwrap_task_mut()
cal_local.get_item_by_id_mut(&task_f_id).await.unwrap().unwrap_task_mut()
.set_name("F renamed locally as well!".into());
cal_local.get_item_by_id_mut(&task_h_id).unwrap().unwrap_task_mut()
cal_local.get_item_by_id_mut(&task_h_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_local.get_item_by_id_mut(&task_i_id).unwrap().unwrap_task_mut()
cal_local.get_item_by_id_mut(&task_i_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_local.get_item_by_id_mut(&task_j_id).unwrap().unwrap_task_mut()
cal_local.get_item_by_id_mut(&task_j_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_local.delete_item(&task_k_id).unwrap();
cal_local.delete_item(&task_l_id).unwrap();
cal_local.delete_item(&task_k_id).await.unwrap();
cal_local.delete_item(&task_l_id).await.unwrap();
let task_o = Item::Task(Task::new("task O (new from local)".into(), Utc::now()));
cal_local.add_item(task_o);
cal_local.add_item(task_o).await;
Provider::new(server, local)
}