Auto merge of #103556 - clubby789:specialize-option-partial-eq, r=scottmcm
Manually implement PartialEq for Option<T> and specialize non-nullable types This PR manually implements `PartialEq` and `StructuralPartialEq` for `Option`, which seems to produce slightly better codegen than the automatically derived implementation. It also allows specializing on the `core::num::NonZero*` and `core::ptr::NonNull` types, taking advantage of the niche optimization by transmuting the `Option<T>` to `T` to be compared directly, which can be done in just two instructions. A comparison of the original, new and specialized code generation is available [here](https://godbolt.org/z/dE4jxdYsa).
This commit is contained in:
commit
8841bee954
4 changed files with 128 additions and 2 deletions
|
@ -48,7 +48,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
|
|||
/// `u32::MAX`. You can also customize things like the `Debug` impl,
|
||||
/// what traits are derived, and so forth via the macro.
|
||||
#[proc_macro]
|
||||
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
|
||||
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step, spec_option_partial_eq)]
|
||||
pub fn newtype_index(input: TokenStream) -> TokenStream {
|
||||
newtype::newtype(input)
|
||||
}
|
||||
|
|
|
@ -192,6 +192,30 @@ impl Parse for Newtype {
|
|||
}
|
||||
}
|
||||
};
|
||||
let spec_partial_eq_impl = if let Lit::Int(max) = &max {
|
||||
if let Ok(max_val) = max.base10_parse::<u32>() {
|
||||
quote! {
|
||||
impl core::option::SpecOptionPartialEq for #name {
|
||||
#[inline]
|
||||
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
|
||||
if #max_val < u32::MAX {
|
||||
l.map(|i| i.private).unwrap_or(#max_val+1) == r.map(|i| i.private).unwrap_or(#max_val+1)
|
||||
} else {
|
||||
match (l, r) {
|
||||
(Some(l), Some(r)) => r == l,
|
||||
(None, None) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
Ok(Self(quote! {
|
||||
#(#attrs)*
|
||||
|
@ -293,6 +317,8 @@ impl Parse for Newtype {
|
|||
|
||||
#step
|
||||
|
||||
#spec_partial_eq_impl
|
||||
|
||||
impl From<#name> for u32 {
|
||||
#[inline]
|
||||
fn from(v: #name) -> u32 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue