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
#[macro_use]
extern crate quote;
extern crate syn;
extern crate proc_macro;
#[macro_use]
extern crate failure;
extern crate heck;
use failure::Error;
use syn::Ident;
use proc_macro::TokenStream;
use quote::quote;
use heck::ShoutySnakeCase;
#[proc_macro_derive(Singleton)]
pub fn singleton_derive(input: TokenStream) -> TokenStream {
let s = input.to_string();
let ast = syn::parse_derive_input(&s).unwrap();
let gen = impl_singleton(&ast);
gen.parse().unwrap()
}
fn impl_singleton(ast: &syn::DeriveInput) -> quote::Tokens {
let name = &ast.ident;
let static_var_name = format!("{}_SINGLETON", name.as_ref().to_shouty_snake_case());
let static_var_ident = Ident::from(static_var_name);
quote! {
static #static_var_ident: singletonum::OnceCell<#name> = singletonum::OnceCell::INIT;
impl singletonum::Singleton for #name {
fn get_instance(init: &Self::Init) -> &'static Self {
#static_var_ident.get_or_init(|| Self::init(init))
}
}
}
}