ouroboros_macro/generate/
struc.rsuse crate::{
info_structures::StructInfo,
utils::{self, replace_this_with_lifetime},
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::Error;
pub fn create_actual_struct_def(info: &StructInfo) -> Result<TokenStream, Error> {
let visibility = utils::submodule_contents_visibility(&info.vis);
let mut fields = Vec::new();
for (ty, ident) in info.generic_consumers() {
fields.push(quote! { #ident: ::core::marker::PhantomData<#ty> });
}
let generic_params = info.generic_params();
let generic_args = info.generic_arguments();
let generic_where = &info.generics.where_clause;
let ident = &info.ident;
let internal_ident = &info.internal_ident;
Ok(quote! {
#[repr(transparent)]
#visibility struct #ident <#generic_params> #generic_where {
actual_data: ::core::mem::MaybeUninit<#internal_ident<#(#generic_args),*>>,
}
})
}
pub fn create_internal_struct_def(info: &StructInfo) -> Result<TokenStream, Error> {
let ident = &info.internal_ident;
let generics = &info.generics;
let field_defs: Vec<_> = info
.fields
.iter()
.rev()
.map(|field| {
let name = &field.name;
let ty = field.stored_type();
quote! {
#[doc(hidden)]
#name: #ty
}
})
.collect();
let mut where_clause = quote! {};
if let Some(clause) = &generics.where_clause {
where_clause = quote! { #clause };
}
let def = quote! {
struct #ident #generics #where_clause {
#(#field_defs),*
}
};
let fake_lifetime = info.fake_lifetime();
let def = replace_this_with_lifetime(quote! { #def }, fake_lifetime.clone());
Ok(def)
}