Changed the PartialEq impl of Deque, replacing the if comparison chain with match + cmp, as requested by clippy.

This commit is contained in:
William Hicklin 2024-12-10 14:02:10 +00:00
parent 6e47051553
commit fc75aed0ff

View File

@ -34,6 +34,7 @@
//! ``` //! ```
use core::borrow::{Borrow, BorrowMut}; use core::borrow::{Borrow, BorrowMut};
use core::cmp::Ordering;
use core::fmt; use core::fmt;
use core::iter::FusedIterator; use core::iter::FusedIterator;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
@ -957,33 +958,35 @@ impl<T: PartialEq, const N: usize> PartialEq for Deque<T, N> {
} }
let (sa, sb) = self.as_slices(); let (sa, sb) = self.as_slices();
let (oa, ob) = other.as_slices(); let (oa, ob) = other.as_slices();
if sa.len() == oa.len() { match sa.len().cmp(&oa.len()) {
sa == oa && sb == ob Ordering::Equal => sa == oa && sb == ob,
} else if sa.len() < oa.len() { Ordering::Less => {
// Always divisible in three sections, for example: // Always divisible in three sections, for example:
// self: [a b c|d e f] // self: [a b c|d e f]
// other: [0 1 2 3|4 5] // other: [0 1 2 3|4 5]
// front = 3, mid = 1, // front = 3, mid = 1,
// [a b c] == [0 1 2] && [d] == [3] && [e f] == [4 5] // [a b c] == [0 1 2] && [d] == [3] && [e f] == [4 5]
let front = sa.len(); let front = sa.len();
let mid = oa.len() - front; let mid = oa.len() - front;
let (oa_front, oa_mid) = oa.split_at(front); let (oa_front, oa_mid) = oa.split_at(front);
let (sb_mid, sb_back) = sb.split_at(mid); let (sb_mid, sb_back) = sb.split_at(mid);
debug_assert_eq!(sa.len(), oa_front.len()); debug_assert_eq!(sa.len(), oa_front.len());
debug_assert_eq!(sb_mid.len(), oa_mid.len()); debug_assert_eq!(sb_mid.len(), oa_mid.len());
debug_assert_eq!(sb_back.len(), ob.len()); debug_assert_eq!(sb_back.len(), ob.len());
sa == oa_front && sb_mid == oa_mid && sb_back == ob sa == oa_front && sb_mid == oa_mid && sb_back == ob
} else { }
let front = oa.len(); Ordering::Greater => {
let mid = sa.len() - front; let front = oa.len();
let mid = sa.len() - front;
let (sa_front, sa_mid) = sa.split_at(front); let (sa_front, sa_mid) = sa.split_at(front);
let (ob_mid, ob_back) = ob.split_at(mid); let (ob_mid, ob_back) = ob.split_at(mid);
debug_assert_eq!(sa_front.len(), oa.len()); debug_assert_eq!(sa_front.len(), oa.len());
debug_assert_eq!(sa_mid.len(), ob_mid.len()); debug_assert_eq!(sa_mid.len(), ob_mid.len());
debug_assert_eq!(sb.len(), ob_back.len()); debug_assert_eq!(sb.len(), ob_back.len());
sa_front == oa && sa_mid == ob_mid && sb == ob_back sa_front == oa && sa_mid == ob_mid && sb == ob_back
}
} }
} }
} }