From 2761dce8b2b25f970cc3a6cd15e1d3e58648d641 Mon Sep 17 00:00:00 2001 From: Martin Ockajak Date: Fri, 19 Jul 2024 16:30:49 +0200 Subject: [PATCH] . --- src/extensions/collections/linked_list.rs | 12 ++++++++++-- src/extensions/collections/vec.rs | 8 ++++---- src/extensions/collections/vec_deque.rs | 8 +++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/extensions/collections/linked_list.rs b/src/extensions/collections/linked_list.rs index 3aaa501..ba31151 100644 --- a/src/extensions/collections/linked_list.rs +++ b/src/extensions/collections/linked_list.rs @@ -356,8 +356,12 @@ impl Sequence for LinkedList { } fn move_at(self, source_index: usize, target_index: usize) -> Self { if source_index == target_index { + let size = self.len(); + if source_index >= size { + panic!(r#"source index (is {source_index:?}) should be < len (is {size:?})"#) + } return self; - }; + } let mut iterator = self.into_iter(); let mut index = 0_usize; if source_index <= target_index { @@ -406,8 +410,12 @@ impl Sequence for LinkedList { fn swap_at(self, source_index: usize, target_index: usize) -> Self { if source_index == target_index { + let size = self.len(); + if source_index >= size { + panic!(r#"source index (is {source_index:?}) should be < len (is {size:?})"#) + } return self; - }; + } let (source, target) = if source_index <= target_index { (source_index, target_index) } else { (target_index, source_index) }; let mut iterator = self.into_iter(); diff --git a/src/extensions/collections/vec.rs b/src/extensions/collections/vec.rs index d484b1c..eaebbc1 100644 --- a/src/extensions/collections/vec.rs +++ b/src/extensions/collections/vec.rs @@ -393,14 +393,14 @@ impl Sequence for Vec { } fn move_at(mut self, source_index: usize, target_index: usize) -> Self { - if source_index != target_index { - let item = self.remove(source_index); - self.insert(target_index, item); - } else { + if source_index == target_index { let size = self.len(); if source_index >= size { panic!(r#"source index (is {source_index:?}) should be < len (is {size:?})"#) } + } else { + let item = self.remove(source_index); + self.insert(target_index, item); }; self } diff --git a/src/extensions/collections/vec_deque.rs b/src/extensions/collections/vec_deque.rs index 25ffabb..1601ee0 100644 --- a/src/extensions/collections/vec_deque.rs +++ b/src/extensions/collections/vec_deque.rs @@ -393,15 +393,13 @@ impl Sequence for VecDeque { } fn move_at(mut self, source_index: usize, target_index: usize) -> Self { - if source_index != target_index { - if let Some(item) = self.remove(source_index) { - self.insert(target_index, item); - } - } else { + if source_index == target_index { let size = self.len(); if source_index >= size { panic!(r#"source index (is {source_index:?}) should be < len (is {size:?})"#) } + } else if let Some(item) = self.remove(source_index) { + self.insert(target_index, item); }; self }