mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-10 00:51:34 +11:00
Remove rand usage in agb-macros
This commit is contained in:
parent
e77a1c26a3
commit
54d370a8b6
|
@ -3,9 +3,11 @@ use proc_macro::TokenStream;
|
||||||
|
|
||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
use rand::Rng;
|
|
||||||
use syn::{FnArg, Ident, ItemFn, Pat, ReturnType, Token, Type, Visibility};
|
use syn::{FnArg, Ident, ItemFn, Pat, ReturnType, Token, Type, Visibility};
|
||||||
|
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
|
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
let f: ItemFn = syn::parse(input).expect("#[agb::entry] must be applied to a function");
|
let f: ItemFn = syn::parse(input).expect("#[agb::entry] must be applied to a function");
|
||||||
|
@ -57,7 +59,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
"Must pass no args to #[agb::entry] macro"
|
"Must pass no args to #[agb::entry] macro"
|
||||||
);
|
);
|
||||||
|
|
||||||
let fn_name = random_ident();
|
let fn_name = hashed_ident(&f);
|
||||||
|
|
||||||
let attrs = f.attrs;
|
let attrs = f.attrs;
|
||||||
let stmts = f.block.stmts;
|
let stmts = f.block.stmts;
|
||||||
|
@ -98,18 +100,13 @@ pub fn num(input: TokenStream) -> TokenStream {
|
||||||
quote!((#integer, #fractional)).into()
|
quote!((#integer, #fractional)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_ident() -> Ident {
|
fn hashed_ident<T: Hash>(f: &T) -> Ident {
|
||||||
let mut rng = rand::thread_rng();
|
let hash = calculate_hash(f);
|
||||||
Ident::new(
|
Ident::new(&format!("_agb_main_func_{}", hash), Span::call_site())
|
||||||
&(0..16)
|
}
|
||||||
.map(|i| {
|
|
||||||
if i == 0 || rng.gen() {
|
fn calculate_hash<T: Hash>(t: &T) -> u64 {
|
||||||
(b'a' + rng.gen::<u8>() % 25) as char
|
let mut s = DefaultHasher::new();
|
||||||
} else {
|
t.hash(&mut s);
|
||||||
(b'0' + rng.gen::<u8>() % 10) as char
|
s.finish()
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<String>(),
|
|
||||||
Span::call_site(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue