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
extern crate proc_macro;
use proc_macro::TokenStream;
use syn;
use quote::quote;

#[proc_macro_derive(SimpleOrd)]
pub fn simple_ord(input: TokenStream) -> TokenStream {
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;

    TokenStream::from(quote! {
        impl ::std::cmp::PartialOrd for #name {
            fn partial_cmp(&self, other: &Self) -> ::std::option::Option<::std::cmp::Ordering> {
                ::std::option::Option::Some(self.cmp(other))
            }
        }

        impl ::std::cmp::PartialEq for #name {
            fn eq(&self, other: &Self) -> bool {
                self.cmp(other) == ::std::cmp::Ordering::Equal
            }
        }

        impl ::std::cmp::Eq for #name {}
    })
}

#[proc_macro_derive(SimplerOrd)]
pub fn simpler_ord(input: TokenStream) -> TokenStream {
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;

    TokenStream::from(quote! {
        impl ::std::cmp::PartialEq for #name {
            fn eq(&self, other: &Self) -> bool {
                self.partial_cmp(other) == ::std::option::Option::Some(::std::cmp::Ordering::Equal)
            }
        }
    })
}