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
//

extern crate proc_macro;

use crate::proc_macro::TokenStream;
use quote::quote;
use syn;

#[proc_macro_derive(Pluralize)]
pub fn derive_pluralize( input: TokenStream ) -> TokenStream {
    let ast = syn::parse( input ).unwrap( );

    impl_pluralize( &ast )
}

fn impl_pluralize( ast: &syn::DeriveInput ) -> TokenStream {
    let ty = &ast.ident;

    let gen = quote! {
        impl Pluralize< #ty > for #ty {
            #[inline(always)]
            fn pluralize<'a>( &'a self ) -> core::slice::Iter<'a, #ty> {
                unsafe{ core::mem::transmute::<&'a #ty, &'a [#ty;1]>(self)}.iter( )
            }
            #[inline(always)]
            fn pluralize_mut<'a>( &'a mut self ) -> core::slice::IterMut<'a, #ty> {
                unsafe{ core::mem::transmute::<&'a mut #ty, &'a mut [#ty;1]>(self)}.iter_mut( )
            }
        }
    };

    gen.into( )
}