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.)
因此,我们不能仅仅依靠 StructuralPartialEq
和 Eq
的幼稚检查。
要解决此问题,我们使用两个派生对象 (#[derive(PartialEq)]
和 #[derive(Eq)]
) 中的每个派生注入的两个单独的 traits,并检查它们是否都作为结构匹配检查的一部分出现。
Implementors§
impl StructuralEq for AsciiChar
impl StructuralEq for BacktraceStatus
impl StructuralEq for std::cmp::Ordering
impl StructuralEq for TryReserveErrorKind
impl StructuralEq for VarError
impl StructuralEq for std::fmt::Alignment
impl StructuralEq for ErrorKind
impl StructuralEq for SeekFrom
impl StructuralEq for IpAddr
impl StructuralEq for Ipv6MulticastScope
impl StructuralEq for Shutdown
impl StructuralEq for SocketAddr
impl StructuralEq for FpCategory
impl StructuralEq for IntErrorKind
impl StructuralEq for BacktraceStyle
impl StructuralEq for Which
impl StructuralEq for SearchStep
impl StructuralEq for std::sync::atomic::Ordering
impl StructuralEq for RecvTimeoutError
impl StructuralEq for TryRecvError
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 usize
impl StructuralEq for CpuidResult
impl StructuralEq for FromBytesUntilNulError
impl StructuralEq for AllocError
impl StructuralEq for Layout
impl StructuralEq for LayoutError
impl StructuralEq for TypeId
impl StructuralEq for CharTryFromError
impl StructuralEq for DecodeUtf16Error
impl StructuralEq for ParseCharError
impl StructuralEq for TryFromCharError
impl StructuralEq for TryReserveError
impl StructuralEq for CString
impl StructuralEq for FromBytesWithNulError
impl StructuralEq for FromVecWithNulError
impl StructuralEq for IntoStringError
impl StructuralEq for NulError
impl StructuralEq for Error
impl StructuralEq for FileType
impl StructuralEq for Permissions
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 ParseFloatError
impl StructuralEq for ParseIntError
impl StructuralEq for TryFromIntError
impl StructuralEq for RangeFull
impl StructuralEq for UCred
Available on Unix only.
impl StructuralEq for InvalidHandleError
Available on Windows only.
impl StructuralEq for NullHandleError
Available on Windows only.