-
-
Notifications
You must be signed in to change notification settings - Fork 133
glib-macros: Add thread_local_object macro #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Take a look at the license at the top of the repository in the LICENSE file. | ||
|
|
||
| use proc_macro2::TokenStream; | ||
| use quote::{quote, quote_spanned, ToTokens}; | ||
| use syn::{ | ||
| parse::{Parse, ParseStream}, | ||
| spanned::Spanned, | ||
| token, Expr, Ident, TypePath, | ||
| }; | ||
|
|
||
| pub(crate) struct ThreadLocalObjectTokens { | ||
| name: Ident, | ||
| ty: TypePath, | ||
| init_expr: Option<Expr>, | ||
| } | ||
|
|
||
| impl Parse for ThreadLocalObjectTokens { | ||
| fn parse(input: ParseStream) -> syn::Result<Self> { | ||
| let name: Ident = input.parse()?; | ||
| let _comma: token::Comma = input.parse()?; | ||
| let ty: TypePath = input.parse()?; | ||
|
|
||
| let init_expr = if input.is_empty() { | ||
| None | ||
| } else { | ||
| let _comma: token::Comma = input.parse()?; | ||
| Some(input.parse()?) | ||
| }; | ||
|
|
||
| Ok(Self { | ||
| name, | ||
| ty, | ||
| init_expr, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ToTokens for ThreadLocalObjectTokens { | ||
| fn to_tokens(&self, tokens: &mut TokenStream) { | ||
| let Self { | ||
| name, | ||
| ty, | ||
| init_expr, | ||
| } = self; | ||
|
|
||
| let init_stream = if let Some(init_expr) = init_expr { | ||
| init_expr.to_token_stream() | ||
| } else { | ||
| quote_spanned! { | ||
| ty.span() => #ty::default() | ||
| } | ||
| }; | ||
|
|
||
| let mod_name = Ident::new( | ||
| &format!("__thread_local_object_private_{name}"), | ||
| name.span(), | ||
| ); | ||
|
|
||
| tokens.extend(quote! { | ||
| mod #mod_name { | ||
| use super::*; | ||
| ::std::thread_local!(static THREAD_LOCAL_OBJ: #ty = #init_stream); | ||
|
|
||
| pub fn #name() -> #ty { | ||
| THREAD_LOCAL_OBJ.with(|w| w.clone()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use something from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that actually works 😅. Because
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay, less code :) |
||
| } | ||
| } | ||
|
|
||
| pub use #mod_name::#name; | ||
| }); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also just be a declarative macro in the glib crate or not? :) Having it as a proc-macro just complicates things