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.
Ownership based accounting
This library follows the idea that only bytes owned by a certain object should be accounted for, and not bytes owned by different objects which are only borrowed. This means in particular that objects referenced by pointers are ignored.
Example
use GetSize;
use *;
On the other hand references implemented as shared ownership are treated as owned values. It is your responsibility to ensure that the bytes occupied by them are not counted twice in your application!
Example
use Arc;
use GetSize;
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.
Example
use GetSize;
// Does not implement GetSize, so we ignore it.
As already mentioned you can also derive GetSize
for enums.
Example
use GetSize;
You can also derive GetSize
on structs and enums with generics. In that case the generated trait implementation will require all generic types to also implement GetSize
.
This behavior may be unfavourable if one or more generic types are ignored duo to the corresponding struct field being ignored. In that case you can also use the ignore
attribute at the struct level to specifiy the generic parameters which shall not be required to implement GetSize
themselves.
Example
use GetSize;
use *;
// Does not implement GetSize, so we ignore it.
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.