From b782a96ba9f2e801f912493b3d682e9ced22b554 Mon Sep 17 00:00:00 2001 From: Martin Ockajak Date: Sun, 14 Jul 2024 03:09:35 +0200 Subject: [PATCH] . --- src/extensions/sequence.rs | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/extensions/sequence.rs b/src/extensions/sequence.rs index 437862e..292358e 100644 --- a/src/extensions/sequence.rs +++ b/src/extensions/sequence.rs @@ -870,33 +870,34 @@ pub trait Sequence { Self: IntoIterator + FromIterator, { let mut iterator = self.into_iter(); - let mut stored = LinkedList::::new(); let mut position = 0_usize; - unfold(|| match (position >= source_index, position >= target_index) { - (true, true) => stored.pop_front().or_else(|| iterator.next()), - (true, false) => { + if source_index <= target_index { + let mut moved = None; + unfold(|| { if position == source_index { if let Some(value) = iterator.next() { - stored.push_back(value) + moved = Some(value); } } + let result = if position == target_index { moved.take() } else { iterator.next() }; position += 1; - iterator.next() - } - (false, true) => { - let mut store = true; - while store && position < source_index { - iterator.next().map(|x| stored.push_back(x)).unwrap_or_else(|| store = false); - position += 1; - } - iterator.next().or_else(|| stored.pop_front()) - } - (false, false) => { + result + }) + .collect() + } else { + let mut stored = LinkedList::from_iter(iterator.by_ref().take(source_index)).into_iter(); + let mut moved = iterator.next(); + unfold(|| { + let result = if position == target_index { + moved.take().or_else(|| stored.next().or_else(|| iterator.next())) + } else { + stored.next().or_else(|| iterator.next()) + }; position += 1; - iterator.next() - } - }) - .collect() + result + }) + .collect() + } } /// Creates a new sequence by padding this sequence to a minimum length of `size`