Trait signature_core::lib::marker::StructuralEq [−][src]
pub trait StructuralEq { }
structural_match
)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 => {} _ => {} } }
(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.
Implementations on Foreign Types
impl StructuralEq for InvalidKeyLength
impl StructuralEq for MacError
impl StructuralEq for Lsb0
impl<'a, O, T> StructuralEq for IterOnes<'a, O, T> where
T: BitStore,
O: BitOrder,
impl<R> StructuralEq for BitIdxError<R> where
R: BitRegister,
impl<T> StructuralEq for BitPtrError<T> where
T: BitStore,
impl<T> StructuralEq for BitSpanError<T> where
T: BitStore,
impl<R> StructuralEq for BitIdx<R> where
R: BitRegister,
impl<R> StructuralEq for BitEnd<R> where
R: BitRegister,
impl<'a, O, T> StructuralEq for IterZeros<'a, O, T> where
T: BitStore,
O: BitOrder,
impl<R> StructuralEq for BitPos<R> where
R: BitRegister,
impl<R> StructuralEq for BitMask<R> where
R: BitRegister,
impl StructuralEq for Msb0
impl<T> StructuralEq for MisalignError<T>
impl<R> StructuralEq for BitSel<R> where
R: BitRegister,
impl StructuralEq for Const
impl<Inner> StructuralEq for Frozen<Inner> where
Inner: Mutability,
impl StructuralEq for NullPtrError
impl StructuralEq for Mut
impl StructuralEq for BigEndian
impl StructuralEq for LittleEndian