get-size
Determine the size in bytes an object occupies inside RAM.
The GetSize
trait can be used to determine the size of an object inside the stack as well as in the heap. The size_of
function provided by the standard library can already be used to determine the size of an object in the stack, but many application (e.g. for caching) do also need to know the number of bytes occupied inside the heap, for which this library provides an appropriate trait.
How to implement
The GetSize
trait is already implemented for most objects defined by the standard library, like Vec
, HashMap
, String
as well as all the primitive values, like u8
, i32
etc.
Unless you have a complex datastructure which requires a manual implementation, you can easily derive GetSize
for your own structs and enums. The derived implementation will implement get_heap_size
by simply calling get_heap_size
on all values contained inside the struct or enum variant and return the sum of them.
You will need to activate the derive
feature first, which is disabled by default. Add the following to your cargo.toml
:
= { = "^0.1", = ["derive"] }
Then you can easily derive GetSize
. If you want the derive macro to ignore a certain struct field you can add the ignore
attribute to it. This might be usefull if some values do not implement the GetSize
trait and do not have data on the heap, or if the data on the heap has already been accounted for somewhere else.
use GetSize;
// Does not implement GetSize, so we ignore it.
As already mentioned you can also derive GetSize
for enums:
use GetSize;
Note that the derive macro does not support unions. You have to manually implement it for them.
License
This library is licensed under the MIT license.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this library by you, shall be licensed as MIT, without any additional terms or conditions.