Remove rand usage in agb-macros

This commit is contained in:
Gwilym Kuiper 2022-03-05 20:51:58 +00:00
parent e77a1c26a3
commit 54d370a8b6

View file

@ -3,9 +3,11 @@ use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, ToTokens};
use rand::Rng;
use syn::{FnArg, Ident, ItemFn, Pat, ReturnType, Token, Type, Visibility};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[proc_macro_attribute]
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
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"
);
let fn_name = random_ident();
let fn_name = hashed_ident(&f);
let attrs = f.attrs;
let stmts = f.block.stmts;
@ -98,18 +100,13 @@ pub fn num(input: TokenStream) -> TokenStream {
quote!((#integer, #fractional)).into()
}
fn random_ident() -> Ident {
let mut rng = rand::thread_rng();
Ident::new(
&(0..16)
.map(|i| {
if i == 0 || rng.gen() {
(b'a' + rng.gen::<u8>() % 25) as char
} else {
(b'0' + rng.gen::<u8>() % 10) as char
fn hashed_ident<T: Hash>(f: &T) -> Ident {
let hash = calculate_hash(f);
Ident::new(&format!("_agb_main_func_{}", hash), Span::call_site())
}
})
.collect::<String>(),
Span::call_site(),
)
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}