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
//! Proc-macro implementation for `newtype`. You probably shouldn't use this
//! crate directly.

extern crate proc_macro;

use proc_macro::TokenStream;

use syn::{
    Data,
    Fields,
};

use quote::quote;

/// Treat a single-field tuple struct as a "newtype"
///
/// This will implement `From`, `Into`, `Deref`, and `DerefMut` for the inner
/// type.
#[proc_macro_derive(NewType)]
pub fn newtype(input: TokenStream) -> TokenStream {
    let input = syn::parse::<syn::DeriveInput>(input).expect("syn parse derive input");

    gen_impl(input).into()
}

fn gen_impl(input: syn::DeriveInput) -> proc_macro2::TokenStream {
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    let name = input.ident;

    let st = match input.data {
        Data::Struct(st) => st,
        _ => panic!("NewType can only be derived for single-field tuple structs"),
    };

    let fields = match st.fields {
        Fields::Unnamed(fields) => fields,
        _ => panic!("NewType can only be derived for single-field tuple structs"),
    };

    if fields.unnamed.len() != 1 {
        panic!("NewType can only be derived for single-field tuple structs")
    }

    let field_ty = fields.unnamed.into_iter().nth(0).unwrap().ty;

    let from = quote! {
        impl #impl_generics From<#field_ty> for #name #ty_generics #where_clause {
            fn from(other: #field_ty) -> #name #ty_generics {
                #name (other)
            }
        }
    };

    let deref = quote! {
        impl #impl_generics ::core::ops::Deref for #name #ty_generics #where_clause {
            type Target = #field_ty;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }
    };

    let deref_mut = quote! {
        impl #impl_generics ::core::ops::DerefMut for #name #ty_generics #where_clause {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }
    };

    let into_inner = quote! {
        impl #impl_generics #name #ty_generics #where_clause {
            /// Unwrap to the inner type
            pub fn into_inner(self) -> #field_ty {
                self.0
            }
        }
    };

    quote! {
        #from #deref #deref_mut #into_inner
    }
}