Basically while I was trying to figure out the way to construct a graph node , I was little concerned regarding the size of my graph type starting from the top node !
Is there any convenient way to acquire the size of objects stored within the smart pointers type during runtime since , I can have input varying in any amount to the program ,
also I don't want to acquire size of each type individually using some loop / recursion
Inside impl Graph { ... }, Self is the same as Graph. So Self::total_size is the same as Graph::total_size, which is how you refer to the associated function total_size. You could rewrite that line as
Correction: if you are using Option<Box<Vec<Graph>>> you need to modify this code as follows:
let mut size = std::mem::size_of::<Self>();
+ size += std::mem::size_of::<Vec<Self>>();
if let Some(ref children) = self.children {
size += children.iter().map(Self::total_size).sum();
}
size
This is because the Vec is stored in a separate heap allocation that's not counted by std::mem::size_of::<Self>(). If you don't use Box—and again, there's no point using it here—then the original code is correct.