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 }