@@ -512,7 +512,7 @@ use crate::{
512
512
} ;
513
513
514
514
/// The `Option` type. See [the module level documentation](self) for more.
515
- #[ derive( Copy , PartialEq , PartialOrd , Eq , Ord , Debug , Hash ) ]
515
+ #[ derive( Copy , PartialOrd , Eq , Ord , Debug , Hash ) ]
516
516
#[ rustc_diagnostic_item = "Option" ]
517
517
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
518
518
pub enum Option < T > {
@@ -2035,6 +2035,72 @@ impl<'a, T> const From<&'a mut Option<T>> for Option<&'a mut T> {
2035
2035
}
2036
2036
}
2037
2037
2038
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2039
+ impl < T > crate :: marker:: StructuralPartialEq for Option < T > { }
2040
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2041
+ impl < T : PartialEq > PartialEq for Option < T > {
2042
+ #[ inline]
2043
+ fn eq ( & self , other : & Self ) -> bool {
2044
+ SpecOptionPartialEq :: eq ( self , other)
2045
+ }
2046
+ }
2047
+
2048
+ #[ unstable( feature = "spec_option_partial_eq" , issue = "none" ) ]
2049
+ #[ doc( hidden) ]
2050
+ pub trait SpecOptionPartialEq : Sized {
2051
+ fn eq ( l : & Option < Self > , other : & Option < Self > ) -> bool ;
2052
+ }
2053
+
2054
+ #[ unstable( feature = "spec_option_partial_eq" , issue = "none" ) ]
2055
+ impl < T : PartialEq > SpecOptionPartialEq for T {
2056
+ #[ inline]
2057
+ default fn eq ( l : & Option < T > , r : & Option < T > ) -> bool {
2058
+ match ( l, r) {
2059
+ ( Some ( l) , Some ( r) ) => * l == * r,
2060
+ ( None , None ) => true ,
2061
+ _ => false ,
2062
+ }
2063
+ }
2064
+ }
2065
+
2066
+ macro_rules! non_zero_option {
2067
+ ( $( #[ $stability: meta] $NZ: ty; ) + ) => {
2068
+ $(
2069
+ #[ $stability]
2070
+ impl SpecOptionPartialEq for $NZ {
2071
+ #[ inline]
2072
+ fn eq( l: & Option <Self >, r: & Option <Self >) -> bool {
2073
+ l. map( Self :: get) . unwrap_or( 0 ) == r. map( Self :: get) . unwrap_or( 0 )
2074
+ }
2075
+ }
2076
+ ) +
2077
+ } ;
2078
+ }
2079
+
2080
+ non_zero_option ! {
2081
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroU8 ;
2082
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroU16 ;
2083
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroU32 ;
2084
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroU64 ;
2085
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroU128 ;
2086
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ] crate :: num:: NonZeroUsize ;
2087
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroI8 ;
2088
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroI16 ;
2089
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroI32 ;
2090
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroI64 ;
2091
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroI128 ;
2092
+ #[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ] crate :: num:: NonZeroIsize ;
2093
+ }
2094
+
2095
+ #[ stable( feature = "nonnull" , since = "1.25.0" ) ]
2096
+ impl < T > SpecOptionPartialEq for crate :: ptr:: NonNull < T > {
2097
+ #[ inline]
2098
+ fn eq ( l : & Option < Self > , r : & Option < Self > ) -> bool {
2099
+ l. map ( Self :: as_ptr) . unwrap_or_else ( || crate :: ptr:: null_mut ( ) )
2100
+ == r. map ( Self :: as_ptr) . unwrap_or_else ( || crate :: ptr:: null_mut ( ) )
2101
+ }
2102
+ }
2103
+
2038
2104
/////////////////////////////////////////////////////////////////////////////
2039
2105
// The Option Iterators
2040
2106
/////////////////////////////////////////////////////////////////////////////
0 commit comments