From a13e1dad5284ac4171ace150b2016987a1abeb9b Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 8 Jan 2024 10:17:46 +0100 Subject: [PATCH] fix: get rid of controlflow --- src/item.rs | 30 +++++++++++------------------- src/lib.rs | 20 +------------------- src/mock_flash.rs | 7 ++----- src/queue.rs | 10 ++-------- 4 files changed, 16 insertions(+), 51 deletions(-) diff --git a/src/item.rs b/src/item.rs index 1f3bb52..6f7baad 100644 --- a/src/item.rs +++ b/src/item.rs @@ -22,7 +22,6 @@ //! use core::num::NonZeroU32; -use core::ops::ControlFlow; use core::ops::Range; use embedded_storage_async::nor_flash::{MultiwriteNorFlash, NorFlash}; @@ -289,7 +288,7 @@ pub async fn find_next_free_item_spot( data_length: u32, ) -> Result, Error> { let (_, free_item_address) = ItemHeaderIter::new(start_address, end_address) - .traverse(flash, |_, _| ControlFlow::Continue(())) + .traverse(flash, |_, _| true) .await?; if let Some(available) = ItemHeader::available_data_bytes::(end_address - free_item_address) { @@ -423,10 +422,7 @@ pub async fn is_page_empty( Ok( ItemHeaderIter::new(page_data_start_address, page_data_end_address) - .traverse(flash, |header, _| match header.crc { - Some(_) => ControlFlow::Break(()), - None => ControlFlow::Continue(()), - }) + .traverse(flash, |header, _| header.crc.is_none()) .await? .0 .is_none(), @@ -498,32 +494,28 @@ impl ItemHeaderIter { &mut self, flash: &mut S, ) -> Result<(Option, u32), Error> { - self.traverse(flash, |_, _| ControlFlow::Break(())).await + self.traverse(flash, |_, _| false).await } - /// Traverse headers until the callback returns `Break`. If the callback returns `Continue`, + /// Traverse headers until the callback returns false. If the callback returns true, /// the element is skipped and traversal continues. /// /// If the end of the headers is reached, a `None` item header is returned. pub async fn traverse( &mut self, flash: &mut S, - callback: impl Fn(&ItemHeader, u32) -> ControlFlow<(), ()>, + callback: impl Fn(&ItemHeader, u32) -> bool, ) -> Result<(Option, u32), Error> { loop { match ItemHeader::read_new(flash, self.current_address, self.end_address).await { Ok(Some(header)) => { let next_address = header.next_item_address::(self.current_address); - match callback(&header, self.current_address) { - ControlFlow::Continue(_) => { - self.current_address = next_address; - continue; - } - ControlFlow::Break(_) => { - let current_address = self.current_address; - self.current_address = next_address; - return Ok((Some(header), current_address)); - } + if callback(&header, self.current_address) { + self.current_address = next_address; + } else { + let current_address = self.current_address; + self.current_address = next_address; + return Ok((Some(header), current_address)); } } Ok(None) => { diff --git a/src/lib.rs b/src/lib.rs index 1e663b7..fcc6670 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ use core::{ fmt::Debug, - ops::{ControlFlow, Deref, DerefMut, Range}, + ops::{Deref, DerefMut, Range}, }; use embedded_storage_async::nor_flash::NorFlash; @@ -377,24 +377,6 @@ impl NorFlashExt for S { }; } -/// Some plumbing for things not yet stable in std/core -trait ResultToControlflow { - fn to_controlflow(self) -> ControlFlow; -} - -impl ResultToControlflow for Result -where - // T: Into, - E: Into, -{ - fn to_controlflow(self) -> ControlFlow { - match self { - Ok(c) => ControlFlow::Continue(c), - Err(b) => ControlFlow::Break(b.into()), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/mock_flash.rs b/src/mock_flash.rs index b6ede36..d5f575a 100644 --- a/src/mock_flash.rs +++ b/src/mock_flash.rs @@ -139,7 +139,7 @@ impl let mut it = crate::item::ItemHeaderIter::new(page_data_start, page_data_end); while let (Some(header), item_address) = - block_on(it.traverse(self, |_, _| core::ops::ControlFlow::Break(()))).unwrap() + block_on(it.traverse(self, |_, _| false)).unwrap() { let next_item_address = header.next_item_address::(item_address); let maybe_item = @@ -163,8 +163,6 @@ impl /// - If true, the item is present and fine. /// - If false, the item is corrupt or erased. pub fn get_item_presence(&mut self, target_item_address: u32) -> Option { - use core::ops::ControlFlow; - use crate::NorFlashExt; use futures::executor::block_on; @@ -186,8 +184,7 @@ impl let mut found_item = None; let mut it = crate::item::ItemHeaderIter::new(page_data_start, page_data_end); - while let (Some(header), item_address) = - block_on(it.traverse(self, |_, _| ControlFlow::Break(()))).unwrap() + while let (Some(header), item_address) = block_on(it.traverse(self, |_, _| false)).unwrap() { let next_item_address = header.next_item_address::(item_address); diff --git a/src/queue.rs b/src/queue.rs index 1c0e583..e158c1c 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -375,13 +375,7 @@ impl<'d, S: NorFlash> QueueIterator<'d, S> { // Search for the first item with data let mut it = ItemHeaderIter::new(current_address, page_data_end_address); if let (Some(found_item_header), found_item_address) = it - .traverse(self.flash, |header, _| { - if header.crc.is_some() { - ControlFlow::Break(()) - } else { - ControlFlow::Continue(()) - } - }) + .traverse(self.flash, |header, _| header.crc.is_none()) .await? { let maybe_item = found_item_header @@ -477,7 +471,7 @@ pub async fn find_max_fit( calculate_page_end_address::(flash_range.clone(), current_page) - S::WORD_SIZE as u32; let next_item_address = ItemHeaderIter::new(page_data_start_address, page_data_end_address) - .traverse(flash, |_, _| ControlFlow::Continue(())) + .traverse(flash, |_, _| true) .await? .1;