From b3b652aa53a59df33a8fa103d99781b035662e59 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Wed, 9 Jun 2021 13:51:04 +0200 Subject: [PATCH] Stop using try_trait It is too much instable. --- libraries/persistent_store/src/lib.rs | 1 - libraries/persistent_store/src/store.rs | 54 ++++++++++++------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/libraries/persistent_store/src/lib.rs b/libraries/persistent_store/src/lib.rs index 0ff3224..5ef8670 100644 --- a/libraries/persistent_store/src/lib.rs +++ b/libraries/persistent_store/src/lib.rs @@ -335,7 +335,6 @@ //! is checked not to crash. #![cfg_attr(not(feature = "std"), no_std)] -#![feature(try_trait)] #[macro_use] extern crate alloc; diff --git a/libraries/persistent_store/src/store.rs b/libraries/persistent_store/src/store.rs index c143c89..0d617dd 100644 --- a/libraries/persistent_store/src/store.rs +++ b/libraries/persistent_store/src/store.rs @@ -30,7 +30,6 @@ use alloc::vec::Vec; use core::borrow::Borrow; use core::cmp::{max, min, Ordering}; use core::convert::TryFrom; -use core::option::NoneError; #[cfg(feature = "std")] use std::collections::HashSet; @@ -78,17 +77,16 @@ impl From for StoreError { } } -impl From for StoreError { - fn from(error: NoneError) -> StoreError { - match error { - NoneError => StoreError::InvalidStorage, - } - } -} - /// Result of store operations. pub type StoreResult = Result; +/// Converts an Option into a StoreResult. +/// +/// The None case is considered invalid and returns [`StoreError::InvalidStorage`]. +fn or_invalid(x: Option) -> StoreResult { + x.ok_or(StoreError::InvalidStorage) +} + /// Progression ratio for store metrics. /// /// This is used for the [`Store::capacity`] and [`Store::lifetime`] metrics. Those metrics are @@ -242,8 +240,8 @@ impl Store { /// Iterates over the entries. pub fn iter<'a>(&'a self) -> StoreResult> { - let head = self.head?; - Ok(Box::new(self.entries.as_ref()?.iter().map( + let head = or_invalid(self.head)?; + Ok(Box::new(or_invalid(self.entries.as_ref())?.iter().map( move |&offset| { let pos = head + offset as Nat; match self.parse_entry(&mut pos.clone())? { @@ -532,7 +530,7 @@ impl Store { /// Recover a possible interrupted operation which is not a compaction. fn recover_operation(&mut self) -> StoreResult<()> { self.entries = Some(Vec::new()); - let mut pos = self.head?; + let mut pos = or_invalid(self.head)?; let mut prev_pos = pos; let end = pos + self.format.virt_size(); while pos < end { @@ -672,7 +670,7 @@ impl Store { /// /// In particular, the handle has not been compacted. fn check_handle(&self, handle: &StoreHandle) -> StoreResult<()> { - if handle.pos < self.head? { + if handle.pos < or_invalid(self.head)? { Err(StoreError::InvalidArgument) } else { Ok(()) @@ -702,7 +700,7 @@ impl Store { /// Compacts one page. fn compact(&mut self) -> StoreResult<()> { - let head = self.head?; + let head = or_invalid(self.head)?; if head.cycle(&self.format) >= self.format.max_page_erases() { return Err(StoreError::NoLifetime); } @@ -717,7 +715,7 @@ impl Store { /// Continues a compaction after its compact page info has been written. fn compact_copy(&mut self) -> StoreResult<()> { - let mut head = self.head?; + let mut head = or_invalid(self.head)?; let page = head.page(&self.format); let end = head.next_page(&self.format); let mut tail = match self.parse_compact(page)? { @@ -773,9 +771,9 @@ impl Store { }; let head = self.format.page_head(init, page); if let Some(entries) = &mut self.entries { - let head_offset = u16::try_from(head - self.head?).ok()?; + let head_offset = or_invalid(u16::try_from(head - or_invalid(self.head)?).ok())?; for entry in entries { - *entry = entry.checked_sub(head_offset)?; + *entry = or_invalid(entry.checked_sub(head_offset))?; } } self.head = Some(head); @@ -791,7 +789,7 @@ impl Store { fn transaction_apply(&mut self, sorted_keys: &[Nat], marker: Position) -> StoreResult<()> { self.delete_keys(&sorted_keys, marker)?; self.set_padding(marker)?; - let end = self.head? + self.format.virt_size(); + let end = or_invalid(self.head)? + self.format.virt_size(); let mut pos = marker + 1; while pos < end { let entry_pos = pos; @@ -826,8 +824,8 @@ impl Store { /// Deletes entries matching a predicate up to a certain position. fn delete_if(&mut self, end: Position, delete: impl Fn(Nat) -> bool) -> StoreResult<()> { - let head = self.head?; - let mut entries = self.entries.take()?; + let head = or_invalid(self.head)?; + let mut entries = or_invalid(self.entries.take())?; let mut i = 0; while i < entries.len() { let pos = head + entries[i] as Nat; @@ -924,20 +922,20 @@ impl Store { } } // There is always at least one initialized page. - Ok(best?) + or_invalid(best) } /// Returns the number of words that can be written without compaction. fn immediate_capacity(&self) -> StoreResult { let tail = self.tail()?; - let end = self.head? + self.format.virt_size(); + let end = or_invalid(self.head)? + self.format.virt_size(); Ok(end.get().saturating_sub(tail.get())) } /// Returns the position of the first word in the store. #[cfg(feature = "std")] pub(crate) fn head(&self) -> StoreResult { - Ok(self.head?) + or_invalid(self.head) } /// Returns one past the position of the last word in the store. @@ -957,8 +955,8 @@ impl Store { None => return Ok(()), Some(x) => x, }; - let head = self.head?; - let offset = u16::try_from(pos - head).ok()?; + let head = or_invalid(self.head)?; + let offset = or_invalid(u16::try_from(pos - head).ok())?; debug_assert!(!entries.contains(&offset)); entries.push(offset); Ok(()) @@ -969,9 +967,9 @@ impl Store { None => return Ok(()), Some(x) => x, }; - let head = self.head?; - let offset = u16::try_from(pos - head).ok()?; - let i = entries.iter().position(|x| *x == offset)?; + let head = or_invalid(self.head)?; + let offset = or_invalid(u16::try_from(pos - head).ok())?; + let i = or_invalid(entries.iter().position(|x| *x == offset))?; entries.swap_remove(i); Ok(()) }