mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-25 10:58:11 +08:00
Structure record stack rewinds as scope guards and make reserve unsafe
This commit is contained in:
@@ -5,7 +5,7 @@ extern crate proc_macro;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, GenericParam, Lifetime, LifetimeParam, TypeParamBound, parse_macro_input};
|
||||
use syn::{DeriveInput, GenericParam, Lifetime, TypeParamBound, parse_macro_input};
|
||||
|
||||
/// Derives an implementation for the [`DynAny`] trait.
|
||||
///
|
||||
@@ -40,37 +40,45 @@ pub fn system_desc_derive(input: TokenStream) -> TokenStream {
|
||||
let struct_name = &ast.ident;
|
||||
let generics = &ast.generics;
|
||||
|
||||
let static_params = replace_lifetimes(generics, "'static");
|
||||
let dyn_params = replace_lifetimes(generics, "'dyn_any");
|
||||
let static_params = generic_arguments(generics, "'static");
|
||||
let dyn_params = generic_arguments(generics, "'dyn_any");
|
||||
|
||||
let old_params = &generics.params.iter().collect::<Vec<_>>();
|
||||
let impl_params = generics.params.iter().map(|param| match param {
|
||||
GenericParam::Type(t) => {
|
||||
let mut t = t.clone();
|
||||
t.bounds.push(TypeParamBound::Lifetime(Lifetime::new("'static", Span::call_site())));
|
||||
quote! {#t}
|
||||
}
|
||||
param => quote! {#param},
|
||||
});
|
||||
quote! {
|
||||
unsafe impl<'dyn_any, #(#old_params,)*> dyn_any::StaticType for #struct_name <#(#dyn_params,)*> {
|
||||
unsafe impl<'dyn_any, #(#impl_params,)*> dyn_any::StaticType for #struct_name <#(#dyn_params,)*> {
|
||||
type Static = #struct_name <#(#static_params,)*>;
|
||||
}
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn replace_lifetimes(generics: &syn::Generics, replacement: &str) -> Vec<proc_macro2::TokenStream> {
|
||||
/// The struct's generic parameters as argument tokens: bare idents for type
|
||||
/// and const parameters (bounds are illegal in argument position), the
|
||||
/// replacement for lifetimes.
|
||||
fn generic_arguments(generics: &syn::Generics, replacement: &str) -> Vec<proc_macro2::TokenStream> {
|
||||
generics
|
||||
.params
|
||||
.iter()
|
||||
.map(|param| {
|
||||
let param = match param {
|
||||
GenericParam::Lifetime(_) => GenericParam::Lifetime(LifetimeParam::new(Lifetime::new(replacement, Span::call_site()))),
|
||||
GenericParam::Type(t) => {
|
||||
let mut t = t.clone();
|
||||
t.bounds.iter_mut().for_each(|bond| {
|
||||
if let TypeParamBound::Lifetime(t) = bond {
|
||||
*t = Lifetime::new(replacement, Span::call_site())
|
||||
}
|
||||
});
|
||||
GenericParam::Type(t.clone())
|
||||
}
|
||||
c => c.clone(),
|
||||
};
|
||||
quote! {#param}
|
||||
.map(|param| match param {
|
||||
GenericParam::Lifetime(_) => {
|
||||
let lifetime = Lifetime::new(replacement, Span::call_site());
|
||||
quote! {#lifetime}
|
||||
}
|
||||
GenericParam::Type(t) => {
|
||||
let ident = &t.ident;
|
||||
quote! {#ident}
|
||||
}
|
||||
GenericParam::Const(c) => {
|
||||
let ident = &c.ident;
|
||||
quote! {#ident}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user