pub trait HasMemoryCostStructural {
// Required method
fn indirect_memory_cost(&self, _: EnabledToken) -> usize;
}
Expand description
Types whose HasMemoryCost
is derived structurally
Usually implemented using
#[derive_deftly(HasMemoryCost)]
.
For Copy
types, it can also be implemented with
memory_cost_structural_copy!
.
When this trait is implemented, a blanket impl provides HasMemoryCost
.
§Structural memory cost
We call the memory cost “structural” when it is derived from the type’s structure.
The memory cost of a HasMemoryCostStructural
type is:
-
The number of bytes in its own size
size_of
; plus -
The (structural) memory cost of all the out-of-line data that it owns; that’s what’s returned by
indirect_memory_cost
For example, String
s out-of-line memory cost is just its capacity,
so its memory cost is the size of its three word layout plus its capacity.
This calculation is performed by the blanket impl of HasMemoryCost
.
§Shared data - non-'static
types, Arc
It is probably a mistake to implement this trait (or HasMemoryCost
)
for types with out-of-line data that they don’t exclusively own.
After all, the memory cost must be known and fixed,
and if there is shared data it’s not clear how it should be accounted.
Required Methods§
Sourcefn indirect_memory_cost(&self, _: EnabledToken) -> usize
fn indirect_memory_cost(&self, _: EnabledToken) -> usize
Memory cost of data stored out-of-line
The total memory cost is the cost of the layout of self
plus this.