1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::DeriveInput;

#[proc_macro_derive(PtrEq)]
pub fn derive_ptr_eq(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;
    let (impl_gn, ty_gn, where_cl) = input.generics.split_for_impl();

    let expanded = quote! {
        unsafe impl #impl_gn PtrEq for #name #ty_gn #where_cl {}

        // &T

        impl #impl_gn ::core::hash::Hash for &#name #ty_gn #where_cl {
            fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
                ((*self) as *const (#name #ty_gn)).hash(state)
            }
        }

        // &mut T

        impl #impl_gn ::core::hash::Hash for &mut #name #ty_gn #where_cl {
            fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
                ((*self) as *const (#name #ty_gn)).hash(state)
            }
        }

        // &T and &T

        impl #impl_gn ::core::cmp::PartialEq<&#name #ty_gn> for &#name #ty_gn #where_cl {
            #[inline]
            fn eq(&self, other: &&#name #ty_gn) -> bool {
                ::core::ptr::eq(*self, *other)
            }
        }

        impl #impl_gn ::core::cmp::Eq for &#name #ty_gn #where_cl {}

        impl #impl_gn ::core::cmp::PartialOrd<&#name #ty_gn> for &#name #ty_gn #where_cl {
            #[inline]
            fn partial_cmp(&self, other: &&#name #ty_gn) -> ::core::option::Option<::core::cmp::Ordering> {
                ((*self) as *const (#name #ty_gn)).partial_cmp(&((*other) as *const (#name #ty_gn)))
            }
        }

        impl #impl_gn ::core::cmp::Ord for &#name #ty_gn #where_cl {
            #[inline]
            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
                ((*self) as *const (#name #ty_gn)).cmp(&((*other) as *const (#name #ty_gn)))
            }
        }

        // &mut T and &mut T

        impl #impl_gn ::core::cmp::PartialEq<&mut #name #ty_gn> for &mut #name #ty_gn #where_cl {
            #[inline]
            fn eq(&self, other: &&mut #name #ty_gn) -> bool {
                ::core::ptr::eq(*self, *other)
            }
        }

        impl #impl_gn ::core::cmp::Eq for &mut #name #ty_gn #where_cl {}

        impl #impl_gn ::core::cmp::PartialOrd<&mut #name #ty_gn> for &mut #name #ty_gn #where_cl {
            #[inline]
            fn partial_cmp(&self, other: &&mut #name #ty_gn) -> ::core::option::Option<::core::cmp::Ordering> {
                ((*self) as *const (#name #ty_gn)).partial_cmp(&((*other) as *const (#name #ty_gn)))
            }
        }

        impl #impl_gn ::core::cmp::Ord for &mut #name #ty_gn #where_cl {
            #[inline]
            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
                ((*self) as *const (#name #ty_gn)).cmp(&((*other) as *const (#name #ty_gn)))
            }
        }

        // &T and &mut T

        impl #impl_gn ::core::cmp::PartialEq<&mut #name #ty_gn> for &#name #ty_gn #where_cl {
            #[inline]
            fn eq(&self, other: &&mut #name #ty_gn) -> bool {
                ::core::ptr::eq(*self, *other)
            }
        }

        impl #impl_gn ::core::cmp::PartialOrd<&mut #name #ty_gn> for &#name #ty_gn #where_cl {
            #[inline]
            fn partial_cmp(&self, other: &&mut #name #ty_gn) -> ::core::option::Option<::core::cmp::Ordering> {
                ((*self) as *const (#name #ty_gn)).partial_cmp(&((*other) as *const (#name #ty_gn)))
            }
        }

        // &mut T and &T

        impl #impl_gn ::core::cmp::PartialEq<&#name #ty_gn> for &mut #name #ty_gn #where_cl {
            #[inline]
            fn eq(&self, other: &&#name #ty_gn) -> bool {
                ::core::ptr::eq(*self, *other)
            }
        }

        impl #impl_gn ::core::cmp::PartialOrd<&#name #ty_gn> for &mut #name #ty_gn #where_cl {
            #[inline]
            fn partial_cmp(&self, other: &&#name #ty_gn) -> ::core::option::Option<::core::cmp::Ordering> {
                ((*self) as *const (#name #ty_gn)).partial_cmp(&((*other) as *const (#name #ty_gn)))
            }
        }
    };

    expanded.into()
}