From 105690e5e7789bb3721390d1ae9600b5bb280b7b Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 30 Mar 2022 23:54:36 +0100 Subject: [PATCH] Drop IteratorExt extension trait We used this once. Not a successful experiment I think. Signed-off-by: Ian Jackson --- apitest/atmain.rs | 21 +++++++++++--------- src/utils.rs | 50 ----------------------------------------------- 2 files changed, 12 insertions(+), 59 deletions(-) diff --git a/apitest/atmain.rs b/apitest/atmain.rs index 647499c6..7b73a08b 100644 --- a/apitest/atmain.rs +++ b/apitest/atmain.rs @@ -235,13 +235,15 @@ pub struct PieceInfo { impl Session { #[throws(Explode)] fn pieces(&self) -> Pieces { - let pieces = self.dom - .element("#pieces_marker") - .unwrap().next_siblings() - .map_loop(|puse: ego_tree::NodeRef| { + let pieces = { + let mut pieces: Pieces = default(); + for puse in self.dom + .element("#pieces_marker") + .unwrap().next_siblings() + { let puse = puse.value(); - let puse = puse.as_element().ok_or(Loop::Continue)?; - let attr = puse.attr("data-info").ok_or(Loop::Break)?; + if_let!{ Some(puse) = puse.as_element(); else continue; }; + if_let!{ Some(attr) = puse.attr("data-info"); else break; }; let pos = Pos::from_iter(["x","y"].iter().map(|attr|{ puse .attr(attr).unwrap() @@ -250,9 +252,10 @@ impl Session { let id = puse.id.as_ref().unwrap(); let id = id.strip_prefix("use").unwrap().to_string(); let info = serde_json::from_str(attr).unwrap(); - Loop::ok(PieceInfo { id, pos, info }) - }) - .collect(); + pieces.push(PieceInfo { id, pos, info }); + } + pieces + }; let nick = self.nick; dbgc!(nick, &pieces); pieces diff --git a/src/utils.rs b/src/utils.rs index 3dbfae0c..d6030d0f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -266,62 +266,12 @@ macro_rules! deref_to_field_mut { } } -#[derive(Debug)] -pub enum Loop { - Continue, - Break, - Error(E), -} -impl From for Loop { - fn from(e: E) -> Loop { Loop::Error(e) } -} -impl Loop { - pub fn ok(t: T) -> Result> { Ok(t) } -} - -pub trait IteratorExt: Iterator - where F: FnMut(Self::Item) -> Result>, -{ - type Return: Iterator; - fn map_loop(self, f: F) -> Self::Return where E: EmptyType; - - type TryReturn: Iterator>; - fn try_map_loop(self, f: F) -> Self::TryReturn; -} - pub trait EmptyType { fn diverge(self) -> T; } impl EmptyType for Infallible { fn diverge(self) -> T { match self { } } } -impl IteratorExt for T where - T: Iterator, - F: FnMut(Self::Item) -> Result>, -{ - type Return = impl Iterator; - fn map_loop(self, f: F) -> Self::Return where E: EmptyType { - self - .map(f) - .filter(|i| !matches!(i, Err(Loop::Continue))) - .take_while(|i| !matches!(i, Err(Loop::Break))) - .map(|i| i.ok().unwrap()) - } - - type TryReturn = impl Iterator>; - fn try_map_loop(self, f: F) -> Self::TryReturn { - self - .map(f) - .filter(|i| matches!(i, Err(Loop::Continue))) - .take_while(|i| matches!(i, Err(Loop::Break))) - .map(|i| match i { - Ok(y) => Ok(y), - Err(Loop::Error(e)) => Err(e), - _ => panic!(), - }) - } -} - #[macro_export] // <- otherwise bogus warning `unused_macros` macro_rules! matches_doesnot_yn2bool { (=) => (true); -- 2.30.2