Skip to content

Mark a few libcore types as structurally equal if their contents are #70759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
17 changes: 17 additions & 0 deletions src/libcore/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::convert::{Infallible, TryFrom};
use crate::fmt;
use crate::hash::{self, Hash};
use crate::marker::Unsize;
use crate::marker::{StructuralEq, StructuralPartialEq};
use crate::slice::{Iter, IterMut};

mod iter;
Expand Down Expand Up @@ -228,6 +229,14 @@ where
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<A, const N: usize> StructuralPartialEq for [A; N]
where
A: StructuralPartialEq,
[A; N]: LengthAtMost32,
{
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
where
Expand Down Expand Up @@ -345,6 +354,14 @@ where
// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }

#[unstable(feature = "structural_match", issue = "31434")]
impl<A, const N: usize> StructuralEq for [A; N]
where
A: StructuralEq,
[A; N]: LengthAtMost32,
{
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}

Expand Down
15 changes: 15 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
mod impls {
use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::hint::unreachable_unchecked;
use crate::marker::{StructuralEq, StructuralPartialEq};

macro_rules! partial_eq_impl {
($($t:ty)*) => ($(
Expand Down Expand Up @@ -1209,6 +1210,9 @@ mod impls {

// & pointers

#[unstable(feature = "structural_match", issue = "31434")]
impl<A: ?Sized> StructuralPartialEq for &A where A: StructuralPartialEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
where
Expand Down Expand Up @@ -1259,11 +1263,18 @@ mod impls {
Ord::cmp(*self, *other)
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<A: ?Sized> StructuralEq for &A where A: StructuralEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &A where A: Eq {}

// &mut pointers

#[unstable(feature = "structural_match", issue = "31434")]
impl<A: ?Sized> StructuralPartialEq for &mut A where A: StructuralPartialEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
where
Expand Down Expand Up @@ -1314,6 +1325,10 @@ mod impls {
Ord::cmp(*self, *other)
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<A: ?Sized> StructuralEq for &mut A where A: StructuralEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &mut A where A: Eq {}

Expand Down
7 changes: 7 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::intrinsics::{assume, exact_div, is_aligned_and_not_null, unchecked_su
use crate::isize;
use crate::iter::*;
use crate::marker::{self, Copy, Send, Sized, Sync};
use crate::marker::{StructuralEq, StructuralPartialEq};
use crate::mem;
use crate::ops::{self, FnMut, Range};
use crate::option::Option;
Expand Down Expand Up @@ -5752,6 +5753,9 @@ extern "C" {
fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<A> StructuralPartialEq for [A] where A: StructuralPartialEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A, B> PartialEq<[B]> for [A]
where
Expand All @@ -5766,6 +5770,9 @@ where
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<A> StructuralEq for [A] where A: StructuralEq {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Eq> Eq for [T] {}

Expand Down
7 changes: 7 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ Section: Trait implementations

mod traits {
use crate::cmp::Ordering;
use crate::marker::{StructuralEq, StructuralPartialEq};
use crate::ops;
use crate::slice::{self, SliceIndex};

Expand All @@ -1738,6 +1739,9 @@ mod traits {
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl StructuralEq for str {}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for str {
#[inline]
Expand All @@ -1750,6 +1754,9 @@ mod traits {
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl StructuralPartialEq for str {}

#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for str {}

Expand Down
9 changes: 9 additions & 0 deletions src/libcore/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::cmp::Ordering::*;
use crate::cmp::*;
use crate::marker::{StructuralEq, StructuralPartialEq};

// macro for implementing n-ary tuple functions and operations
macro_rules! tuple_impls {
Expand All @@ -23,6 +24,14 @@ macro_rules! tuple_impls {
}
}

#[unstable(feature = "structural_match", issue = "31434")]
impl<$($T:StructuralPartialEq),+> StructuralPartialEq for ($($T,)+)
where last_type!($($T,)+): ?Sized {}

#[unstable(feature = "structural_match", issue = "31434")]
impl<$($T:StructuralEq),+> StructuralEq for ($($T,)+)
where last_type!($($T,)+): ?Sized {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}

Expand Down