1use crate::{
2 info_structures::StructInfo,
3 utils::{self, replace_this_with_lifetime},
4};
5use proc_macro2::TokenStream;
6use quote::quote;
7use syn::Error;
89/// Creates the struct that will actually store the data.
10pub fn create_actual_struct_def(info: &StructInfo) -> Result<TokenStream, Error> {
11let visibility = utils::submodule_contents_visibility(&info.vis);
12let mut fields = Vec::new();
13for (ty, ident) in info.generic_consumers() {
14 fields.push(quote! { #ident: ::core::marker::PhantomData<#ty> });
15 }
16let generic_params = info.generic_params();
17let generic_args = info.generic_arguments();
18let generic_where = &info.generics.where_clause;
19let ident = &info.ident;
20let internal_ident = &info.internal_ident;
21Ok(quote! {
22#[repr(transparent)]
23#visibility struct #ident <#generic_params> #generic_where {
24 actual_data: ::core::mem::MaybeUninit<#internal_ident<#(#generic_args),*>>,
25 }
26 })
27}
2829/// Creates a struct with fields like the original struct. Instances of the
30/// "actual" struct are reinterpreted as instances of the "internal" struct
31/// whenever data needs to be accessed. (This gets around the problem that
32/// references passed to functions must be valid through the entire function,
33/// but references *created* inside a function can be considered invalid
34/// whenever, even during the duration of the function.)
35pub fn create_internal_struct_def(info: &StructInfo) -> Result<TokenStream, Error> {
36let ident = &info.internal_ident;
37let generics = &info.generics;
3839let field_defs: Vec<_> = info
40 .fields
41 .iter()
42// Reverse the order of all fields. We ensure that items in the struct are only dependent
43 // on references to items above them. Rust drops items in a struct in forward declaration order.
44 // This would cause parents being dropped before children, necessitating the reversal.
45.rev()
46 .map(|field| {
47let name = &field.name;
48let ty = field.stored_type();
49quote! {
50#[doc(hidden)]
51#name: #ty
52 }
53 })
54 .collect();
5556// Create the new struct definition.
57let mut where_clause = quote! {};
58if let Some(clause) = &generics.where_clause {
59 where_clause = quote! { #clause };
60 }
61let def = quote! {
62struct #ident #generics #where_clause {
63 #(#field_defs),*
64 }
65 };
6667// Finally, replace the fake 'this lifetime with the one we found.
68let fake_lifetime = info.fake_lifetime();
69let def = replace_this_with_lifetime(quote! { #def }, fake_lifetime.clone());
7071Ok(def)
72}