Trait std::marker::StructuralEq

source ·
pub trait StructuralEq { }
🔬This is a nightly-only experimental API. (structural_match #31434)
Expand description

模式匹配中使用的常量的必需 trait。

派生 Eq 的任何类型都会自动实现此 trait,无论其类型参数是否实现 Eq

这是一种解决我们类型系统中的限制的技巧。

Background

我们要要求模式匹配中使用的 const 类型具有属性 #[derive(PartialEq, Eq)]

在更理想的世界中,我们可以通过仅检查给定类型是否同时实现 StructuralPartialEq trait 和 Eq trait 来检查该要求。 但是,您可能拥有 doderive(PartialEq, Eq) 的 ADT,这是我们希望编译器接受的情况,但是 const 的类型无法实现 Eq

也就是说,像这样的情况:

#[derive(PartialEq, Eq)]
struct Wrap<X>(X);

fn higher_order(_: &()) { }

const CFN: Wrap<fn(&())> = Wrap(higher_order);

fn main() {
    match CFN {
        CFN => {}
        _ => {}
    }
}
Run

(以上代码中的问题是 Wrap<fn(&())> 既不实现 PartialEq 也不实现 Eq,因为 for <'a> fn(&'a _) does not implement those traits.)

因此,我们不能仅仅依靠 StructuralPartialEqEq 的幼稚检查。

要解决此问题,我们使用两个派生对象 (#[derive(PartialEq)]#[derive(Eq)]) 中的每个派生注入的两个单独的 traits,并检查它们是否都作为结构匹配检查的一部分出现。

Implementors§

source§

impl StructuralEq for AsciiChar

1.65.0 · source§

impl StructuralEq for BacktraceStatus

1.0.0 · source§

impl StructuralEq for std::cmp::Ordering

source§

impl StructuralEq for TryReserveErrorKind

1.0.0 · source§

impl StructuralEq for VarError

1.28.0 · source§

impl StructuralEq for std::fmt::Alignment

1.0.0 · source§

impl StructuralEq for ErrorKind

1.0.0 · source§

impl StructuralEq for SeekFrom

1.7.0 · source§

impl StructuralEq for IpAddr

source§

impl StructuralEq for Ipv6MulticastScope

1.0.0 · source§

impl StructuralEq for Shutdown

1.0.0 · source§

impl StructuralEq for SocketAddr

1.0.0 · source§

impl StructuralEq for FpCategory

1.55.0 · source§

impl StructuralEq for IntErrorKind

source§

impl StructuralEq for BacktraceStyle

source§

impl StructuralEq for Which

source§

impl StructuralEq for SearchStep

1.0.0 · source§

impl StructuralEq for std::sync::atomic::Ordering

1.12.0 · source§

impl StructuralEq for RecvTimeoutError

1.0.0 · source§

impl StructuralEq for TryRecvError

source§

impl StructuralEq for bool

source§

impl StructuralEq for char

source§

impl StructuralEq for i8

source§

impl StructuralEq for i16

source§

impl StructuralEq for i32

source§

impl StructuralEq for i64

source§

impl StructuralEq for i128

source§

impl StructuralEq for isize

source§

impl StructuralEq for str

source§

impl StructuralEq for u8

source§

impl StructuralEq for u16

source§

impl StructuralEq for u32

source§

impl StructuralEq for u64

source§

impl StructuralEq for u128

source§

impl StructuralEq for usize

1.27.0 · source§

impl StructuralEq for CpuidResult

1.69.0 · source§

impl StructuralEq for FromBytesUntilNulError

source§

impl StructuralEq for AllocError

1.28.0 · source§

impl StructuralEq for Layout

1.50.0 · source§

impl StructuralEq for LayoutError

1.0.0 · source§

impl StructuralEq for TypeId

1.34.0 · source§

impl StructuralEq for CharTryFromError

1.9.0 · source§

impl StructuralEq for DecodeUtf16Error

1.20.0 · source§

impl StructuralEq for ParseCharError

1.59.0 · source§

impl StructuralEq for TryFromCharError

1.57.0 · source§

impl StructuralEq for TryReserveError

1.64.0 · source§

impl StructuralEq for CString

1.64.0 · source§

impl StructuralEq for FromBytesWithNulError

1.64.0 · source§

impl StructuralEq for FromVecWithNulError

1.64.0 · source§

impl StructuralEq for IntoStringError

1.64.0 · source§

impl StructuralEq for NulError

1.0.0 · source§

impl StructuralEq for Error

1.1.0 · source§

impl StructuralEq for FileType

1.0.0 · source§

impl StructuralEq for Permissions

source§

impl StructuralEq for Assume

1.0.0 · source§

impl StructuralEq for AddrParseError

1.0.0 · source§

impl StructuralEq for Ipv4Addr

1.0.0 · source§

impl StructuralEq for Ipv6Addr

1.0.0 · source§

impl StructuralEq for SocketAddrV4

1.0.0 · source§

impl StructuralEq for SocketAddrV6

1.34.0 · source§

impl StructuralEq for NonZeroI8

1.34.0 · source§

impl StructuralEq for NonZeroI16

1.34.0 · source§

impl StructuralEq for NonZeroI32

1.34.0 · source§

impl StructuralEq for NonZeroI64

1.34.0 · source§

impl StructuralEq for NonZeroI128

1.34.0 · source§

impl StructuralEq for NonZeroIsize

1.28.0 · source§

impl StructuralEq for NonZeroU8

1.28.0 · source§

impl StructuralEq for NonZeroU16

1.28.0 · source§

impl StructuralEq for NonZeroU32

1.28.0 · source§

impl StructuralEq for NonZeroU64

1.28.0 · source§

impl StructuralEq for NonZeroU128

1.28.0 · source§

impl StructuralEq for NonZeroUsize

1.0.0 · source§

impl StructuralEq for ParseFloatError

1.0.0 · source§

impl StructuralEq for ParseIntError

1.34.0 · source§

impl StructuralEq for TryFromIntError

1.0.0 · source§

impl StructuralEq for RangeFull

source§

impl StructuralEq for UCred

Available on Unix only.
1.63.0 · source§

impl StructuralEq for InvalidHandleError

Available on Windows only.
1.63.0 · source§

impl StructuralEq for NullHandleError

Available on Windows only.
1.7.0 · source§

impl StructuralEq for StripPrefixError

1.0.0 · source§

impl StructuralEq for ExitStatus

source§

impl StructuralEq for ExitStatusError

1.0.0 · source§

impl StructuralEq for Output

source§

impl StructuralEq for std::ptr::Alignment

1.0.0 · source§

impl StructuralEq for ParseBoolError

1.0.0 · source§

impl StructuralEq for Utf8Error

1.0.0 · source§

impl StructuralEq for FromUtf8Error

1.0.0 · source§

impl StructuralEq for String

1.0.0 · source§

impl StructuralEq for RecvError

1.5.0 · source§

impl StructuralEq for WaitTimeoutResult

1.26.0 · source§

impl StructuralEq for AccessError

1.19.0 · source§

impl StructuralEq for ThreadId

1.3.0 · source§

impl StructuralEq for Duration

1.8.0 · source§

impl StructuralEq for Instant

1.8.0 · source§

impl StructuralEq for SystemTime

1.66.0 · source§

impl StructuralEq for TryFromFloatSecsError

1.33.0 · source§

impl StructuralEq for PhantomPinned

1.0.0 · source§

impl<'a> StructuralEq for Component<'a>

1.0.0 · source§

impl<'a> StructuralEq for Prefix<'a>

1.10.0 · source§

impl<'a> StructuralEq for Location<'a>

1.0.0 · source§

impl<'a> StructuralEq for PrefixComponent<'a>

source§

impl<'a> StructuralEq for Utf8Chunk<'a>

1.55.0 · source§

impl<B, C> StructuralEq for ControlFlow<B, C>

1.0.0 · source§

impl<Idx> StructuralEq for Range<Idx>

1.0.0 · source§

impl<Idx> StructuralEq for RangeFrom<Idx>

1.26.0 · source§

impl<Idx> StructuralEq for RangeInclusive<Idx>

1.0.0 · source§

impl<Idx> StructuralEq for RangeTo<Idx>

1.26.0 · source§

impl<Idx> StructuralEq for RangeToInclusive<Idx>

1.17.0 · source§

impl<T> StructuralEq for Bound<T>

1.0.0 · source§

impl<T> StructuralEq for Option<T>

1.0.0 · source§

impl<T> StructuralEq for TrySendError<T>

1.36.0 · source§

impl<T> StructuralEq for Poll<T>

source§

impl<T> StructuralEq for &Twhere T: ?Sized,

source§

impl<T> StructuralEq for [T]

1.19.0 · source§

impl<T> StructuralEq for Reverse<T>

1.0.0 · source§

impl<T> StructuralEq for Cursor<T>

1.20.0 · source§

impl<T> StructuralEq for ManuallyDrop<T>where T: ?Sized,

source§

impl<T> StructuralEq for Saturating<T>

1.0.0 · source§

impl<T> StructuralEq for Wrapping<T>

1.0.0 · source§

impl<T> StructuralEq for SendError<T>

source§

impl<T> StructuralEq for PhantomData<T>where T: ?Sized,

1.0.0 · source§

impl<T, E> StructuralEq for Result<T, E>

source§

impl<T, const N: usize> StructuralEq for [T; N]

source§

impl<Y, R> StructuralEq for GeneratorState<Y, R>