Trait core::marker::StructuralEq
source · pub trait StructuralEq { }
structural_match
#31434)Expand description
Required trait for constants used in pattern matches.
Any type that derives Eq
automatically implements this trait, regardless
of whether its type parameters implement Eq
.
This is a hack to work around a limitation in our type system.
Background
We want to require that types of consts used in pattern matches
have the attribute #[derive(PartialEq, Eq)]
.
In a more ideal world, we could check that requirement by just checking that
the given type implements both the StructuralPartialEq
trait and
the Eq
trait. However, you can have ADTs that do derive(PartialEq, Eq)
,
and be a case that we want the compiler to accept, and yet the constant’s
type fails to implement Eq
.
Namely, a case like this:
#[derive(PartialEq, Eq)]
struct Wrap<X>(X);
fn higher_order(_: &()) { }
const CFN: Wrap<fn(&())> = Wrap(higher_order);
fn main() {
match CFN {
CFN => {}
_ => {}
}
}
Run(The problem in the above code is that Wrap<fn(&())>
does not implement
PartialEq
, nor Eq
, because for<'a> fn(&'a _)
does not implement those
traits.)
Therefore, we cannot rely on naive check for StructuralPartialEq
and
mere Eq
.
As a hack to work around this, we use two separate traits injected by each
of the two derives (#[derive(PartialEq)]
and #[derive(Eq)]
) and check
that both of them are present as part of structural-match checking.
Implementors§
impl StructuralEq for AsciiChar
impl StructuralEq for core::cmp::Ordering
impl StructuralEq for core::fmt::Alignment
impl StructuralEq for IpAddr
impl StructuralEq for Ipv6MulticastScope
impl StructuralEq for SocketAddr
impl StructuralEq for FpCategory
impl StructuralEq for IntErrorKind
impl StructuralEq for Which
impl StructuralEq for SearchStep
impl StructuralEq for core::sync::atomic::Ordering
impl StructuralEq for bool
impl StructuralEq for char
impl StructuralEq for i8
impl StructuralEq for i16
impl StructuralEq for i32
impl StructuralEq for i64
impl StructuralEq for i128
impl StructuralEq for isize
impl StructuralEq for str
impl StructuralEq for u8
impl StructuralEq for u16
impl StructuralEq for u32
impl StructuralEq for u64
impl StructuralEq for u128
impl StructuralEq for ()
impl StructuralEq for usize
impl StructuralEq for AllocError
impl StructuralEq for Layout
impl StructuralEq for LayoutError
impl StructuralEq for TypeId
impl StructuralEq for CpuidResult
impl StructuralEq for CharTryFromError
impl StructuralEq for DecodeUtf16Error
impl StructuralEq for ParseCharError
impl StructuralEq for TryFromCharError
impl StructuralEq for FromBytesUntilNulError
impl StructuralEq for FromBytesWithNulError
impl StructuralEq for Error
impl StructuralEq for Assume
impl StructuralEq for AddrParseError
impl StructuralEq for Ipv4Addr
impl StructuralEq for Ipv6Addr
impl StructuralEq for SocketAddrV4
impl StructuralEq for SocketAddrV6
impl StructuralEq for NonZeroI8
impl StructuralEq for NonZeroI16
impl StructuralEq for NonZeroI32
impl StructuralEq for NonZeroI64
impl StructuralEq for NonZeroI128
impl StructuralEq for NonZeroIsize
impl StructuralEq for NonZeroU8
impl StructuralEq for NonZeroU16
impl StructuralEq for NonZeroU32
impl StructuralEq for NonZeroU64
impl StructuralEq for NonZeroU128
impl StructuralEq for NonZeroUsize
impl StructuralEq for ParseIntError
impl StructuralEq for TryFromIntError
impl StructuralEq for RangeFull
impl StructuralEq for core::ptr::Alignment
impl StructuralEq for ParseBoolError
impl StructuralEq for Utf8Error
impl StructuralEq for Duration
impl StructuralEq for TryFromFloatSecsError
impl StructuralEq for PhantomPinned
impl<'a> StructuralEq for Location<'a>
impl<'a> StructuralEq for Utf8Chunk<'a>
impl<B, C> StructuralEq for ControlFlow<B, C>
impl<Idx> StructuralEq for Range<Idx>
impl<Idx> StructuralEq for RangeFrom<Idx>
impl<Idx> StructuralEq for RangeInclusive<Idx>
impl<Idx> StructuralEq for RangeTo<Idx>
impl<Idx> StructuralEq for RangeToInclusive<Idx>
impl<T> StructuralEq for Bound<T>
impl<T> StructuralEq for Option<T>
impl<T> StructuralEq for Poll<T>
impl<T> StructuralEq for [T]
impl<T> StructuralEq for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.