Function macro_tools::exposed::phantom::add_to_item
source · pub fn add_to_item(input: &ItemStruct) -> ItemStruct
Expand description
Adds a PhantomData
field to a struct to manage generic parameter usage.
This function clones a given syn::ItemStruct
, calculates the appropriate PhantomData
usage
based on the struct’s generic parameters, and adds a corresponding PhantomData
field. This field
helps in handling ownership and lifetime indications for generic parameters, ensuring that they
are correctly accounted for in type checking, even if they are not directly used in the struct’s
fields.
§Parameters
input
: A reference to thesyn::ItemStruct
which describes the structure to which thePhantomData
field will be added.
§Returns
Returns a new syn::ItemStruct
with the PhantomData
field added to its list of fields.
§Examples
use syn::{ parse_quote, ItemStruct };
let input_struct : ItemStruct = parse_quote!
{
pub struct MyStruct< T, U >
{
data : T,
}
};
let modified_struct = macro_tools::phantom::add_to_item( &input_struct );
println!( "{:#?}", modified_struct );
// Output will include a _phantom field of type `PhantomData< ( T, U ) >`