From 3efcaedbf1caff07c25de5783dd0ea276f507cb6 Mon Sep 17 00:00:00 2001 From: Matt Johnston <matt@ucc.asn.au> Date: Sat, 30 Dec 2023 23:17:43 +0800 Subject: [PATCH] Use ControlFlow to exit menu --- embassy/demos/picow/src/main.rs | 4 +++- embassy/demos/picow/src/picowmenu.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/embassy/demos/picow/src/main.rs b/embassy/demos/picow/src/main.rs index e83ba6a..de0c517 100644 --- a/embassy/demos/picow/src/main.rs +++ b/embassy/demos/picow/src/main.rs @@ -9,6 +9,8 @@ pub use log::{debug, error, info, log, trace, warn}; #[cfg(feature = "defmt")] pub use defmt::{debug, error, info, panic, trace, warn}; +use core::ops::ControlFlow; + use {defmt_rtt as _, panic_probe as _}; use embassy_executor::Spawner; @@ -236,7 +238,7 @@ where menu.input_byte(*c); menu.context.out.flush(chanw).await?; - if menu.context.progress(chanr, chanw).await? { + if let ControlFlow::Break(_) = menu.context.progress(chanr, chanw).await? { break 'io; } } diff --git a/embassy/demos/picow/src/picowmenu.rs b/embassy/demos/picow/src/picowmenu.rs index 203ab92..8c60616 100644 --- a/embassy/demos/picow/src/picowmenu.rs +++ b/embassy/demos/picow/src/picowmenu.rs @@ -11,7 +11,7 @@ pub use defmt::{debug, error, info, panic, trace, warn}; use core::fmt::{Write as _, Debug, Display}; use core::future::{poll_fn, Future}; -use core::ops::DerefMut; +use core::ops::{DerefMut, ControlFlow}; use core::sync::atomic::Ordering::{Relaxed, SeqCst}; use core::str::FromStr; @@ -92,12 +92,12 @@ impl MenuCtx { true } - // Returns `Ok(true)` to exit the menu + // Returns `Ok(Break)` to exit the menu pub(crate) async fn progress<R, W>( &mut self, mut chanr: &mut R, mut chanw: &mut W, - ) -> Result<bool> + ) -> Result<ControlFlow<()>> where R: AsyncRead<Error = sunset::Error>, W: AsyncWrite<Error = sunset::Error>, @@ -117,7 +117,7 @@ impl MenuCtx { } crate::serial(chanr, chanw, self.state.usb_pipe).await?; // TODO we could return to the menu on serial error? - return Ok(true); + return Ok(ControlFlow::Break(())); } } @@ -136,7 +136,7 @@ impl MenuCtx { } crate::serial(chanr, chanw, self.state.serial1_pipe).await?; // TODO we could return to the menu on serial error? - return Ok(true); + return Ok(ControlFlow::Break(())); } } @@ -153,7 +153,7 @@ impl MenuCtx { } if self.logout { - return Ok(true); + return Ok(ControlFlow::Break(())); } if self.reset { @@ -171,7 +171,7 @@ impl MenuCtx { // write messages from handling self.out.flush(&mut chanw).await?; - Ok(false) + Ok(ControlFlow::Continue(())) } } -- GitLab