Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/alloc/src/collections/vec_deque/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ macro_rules! __impl_slice_eq1 {
}
}
}

macro_rules! __impl_slice_eq2 {
($(#[$stability:meta])+, [$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
$(#[$stability])+
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
where
T: PartialEq<U>,
$($constraints)*
{
fn eq(&self, other: &$rhs) -> bool {
if self.len() != other.len() {
return false;
}
let (oa, ob) = other.as_slices();
let (sa, sb) = self[..].split_at(oa.len());
sa == oa && sb == ob
}
}
}
}
7 changes: 7 additions & 0 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3907,6 +3907,13 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A>, [U; N], }
__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &[U; N], }
__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &mut [U; N], }

__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [] Vec<T, A>, VecDeque<U, A>, }

@pthariensflame pthariensflame Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the two allocators both A? Is there a reason to restrict to only comparing a Vec and VecDeque using the same allocator?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point, and I think unfortunately this might warrant another crater run to fix. Although it's worth clarifying that only Vec stably supports allocators at the moment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's worth clarifying that only Vec stably supports allocators at the moment.

We did think of that but it doesn’t actually make a difference to this generalization. Vec having a choice of allocators means we may want to compare it with an arbitrary allocator to VecDeque<_, Global>.

__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [] &[T], VecDeque<U, A>, }
__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [] &mut [T], VecDeque<U, A>, }
__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [const N: usize] [T; N], VecDeque<U, A>, }
__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [const N: usize] &[T; N], VecDeque<U, A>, }
__impl_slice_eq2! { #[stable(feature = "slice_partial_eq_vec_deque", since = "CURRENT_RUSTC_VERSION")], [const N: usize] &mut [T; N], VecDeque<U, A>, }

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Expand Down
21 changes: 21 additions & 0 deletions library/alloctests/tests/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,27 @@ fn test_partial_eq_array() {
assert!(d == ['a', 'b']);
}

#[test]
fn test_partial_eq_vecdeque_reverse() {
let mut d = VecDeque::with_capacity(4);
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.pop_front();
d.push_back(4);

let v = vec![2, 3, 4];
let a = [2, 3, 4];
let mut b = [2, 3, 4];

assert!(v == d);
assert!(&v[..] == d);
assert!(&mut b[..] == d);
assert!(a == d);
assert!(&a == d);
assert!(&mut b == d);
}

#[test]
fn test_hash() {
let mut x = VecDeque::new();
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/consts/too_generic_eval_ice.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ LL | [5; Self::HOST_SIZE] == [6; 0]
|
= help: the trait `PartialEq<[{integer}; 0]>` is not implemented for `[{integer}; Self::HOST_SIZE]`
= help: the following other types implement trait `PartialEq<Rhs>`:
`&[T; N]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<Cow<'_, [U]>>`
`&[T]` implements `PartialEq<Vec<U, A>>`
`&[T]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<[U; N]>`
`&[u8; N]` implements `PartialEq<ByteStr>`
`&[u8; N]` implements `PartialEq<ByteString>`
`&[u8]` implements `PartialEq<ByteStr>`
`&[u8]` implements `PartialEq<ByteString>`
`&mut [T]` implements `PartialEq<Cow<'_, [U]>>`
and 13 others
and 18 others

error: aborting due to 4 previous errors

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/macros/assert-ne-no-invalid-help-issue-146204.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ LL | assert_ne!(buf, b"----");
|
= help: the trait `PartialEq<&[u8; 4]>` is not implemented for `[u8; 4]`
= help: the following other types implement trait `PartialEq<Rhs>`:
`&[T; N]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<Cow<'_, [U]>>`
`&[T]` implements `PartialEq<Vec<U, A>>`
`&[T]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<[U; N]>`
`&[u8; N]` implements `PartialEq<ByteStr>`
`&[u8; N]` implements `PartialEq<ByteString>`
`&[u8]` implements `PartialEq<ByteStr>`
`&[u8]` implements `PartialEq<ByteString>`
`&mut [T]` implements `PartialEq<Cow<'_, [U]>>`
and 13 others
and 18 others

error[E0277]: can't compare `[u8; 4]` with `&[u8; 4]`
--> $DIR/assert-ne-no-invalid-help-issue-146204.rs:19:5
Expand All @@ -24,15 +24,15 @@ LL | assert_eq!(buf, b"----");
|
= help: the trait `PartialEq<&[u8; 4]>` is not implemented for `[u8; 4]`
= help: the following other types implement trait `PartialEq<Rhs>`:
`&[T; N]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<Cow<'_, [U]>>`
`&[T]` implements `PartialEq<Vec<U, A>>`
`&[T]` implements `PartialEq<VecDeque<U, A>>`
`&[T]` implements `PartialEq<[U; N]>`
`&[u8; N]` implements `PartialEq<ByteStr>`
`&[u8; N]` implements `PartialEq<ByteString>`
`&[u8]` implements `PartialEq<ByteStr>`
`&[u8]` implements `PartialEq<ByteString>`
`&mut [T]` implements `PartialEq<Cow<'_, [U]>>`
and 13 others
and 18 others

error[E0277]: can't compare `[u8; 4]` with `&[u8; 4]`
--> $DIR/assert-ne-no-invalid-help-issue-146204.rs:5:30
Expand Down
Loading