Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/overvenus/heapsz
A crate for calculating the heap usage of a data structure.
https://github.com/overvenus/heapsz
derive memory-management rust
Last synced: 3 months ago
JSON representation
A crate for calculating the heap usage of a data structure.
- Host: GitHub
- URL: https://github.com/overvenus/heapsz
- Owner: overvenus
- License: mit
- Created: 2024-05-06T07:29:12.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-14T09:43:40.000Z (8 months ago)
- Last Synced: 2024-09-16T12:31:43.352Z (4 months ago)
- Topics: derive, memory-management, rust
- Language: Rust
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# heapsz
A crate for calculating the heap usage of a data structure.
It's simple. Only one required method in the `HeapSize` trait, which can be
generated by `#[derive(HeapSize)]`.It's fast. It estimates an approximate heap size in O(1) time.
## Usage
### Examples
**Calculate `heap_size()` selectively**
Example Expanded example
```rust,ignore
#[derive(HeapSize)]
struct Foo {
a: usize,
b: Option,
#[heap_size]
c: Box<[usize; 5]>,
#[heap_size]
d: Vec,
}
``````rust,ignore
impl HeapSize for Foo {
fn heap_size(&self) -> usize {
self.c.heap_size() + self.d.heap_size()
}
}
```Calculate
heap_size()
of all fieldsExample Expanded example
```rust,ignore
#[derive(HeapSize)]
#[heap_size]
struct Foo {
a: usize,
b: Option,
c: Box<[usize; 5]>,
d: Vec,
}
``````rust,ignore
impl HeapSize for Foo {
fn heap_size(&self) -> usize {
self.a.heap_size()
+ self.b.heap_size()
+ self.c.heap_size()
+ self.d.heap_size()
}
}
```Skip irrelative fields
Example Expanded example
```rust,ignore
#[derive(HeapSize)]
#[heap_size]
struct Foo {
a: usize,
b: Option,
#[heap_size(skip)]
c: Arc<[usize; 5]>,
d: Vec,
}
``````rust,ignore
impl HeapSize for Foo {
fn heap_size(&self) -> usize {
self.a.heap_size()
+ self.b.heap_size()
+ self.d.heap_size()
}
}
```Implement HeapSize for third-party structs
Example Expanded example
```rust,ignore
mod bytes_heap_size {
pub heap_size(
b: &Bytes
) -> usize {
b.len()
}
}#[derive(HeapSize)]
#[heap_size]
struct Foo {
a: usize,
b: Option,
c: Box<[usize; 5]>,
d: Bytes,
}
``````rust,ignore
impl HeapSize for Foo {
fn heap_size(&self) -> usize {
self.a.heap_size()
+ self.b.heap_size()
+ bytes_heap_size::heap_size(&self.d)
}
}
```## `#[derive(HeapSize)]`
### Field attributes
Apply to one field in a struct or in an enum variant.
* `#[heap_size]`
By default, `#[derive(HeapSize)]` generates an empty implementation which
always return 0. `#[heap_size]` needs to be added to fields that have a heap
allocation.This is because of 1) most struct does not contain heap allocation at all,
and 2) they often do not implement the `HeapSize` trait.* `#[heap_size(skip)]`
Skip this field: do not calculate its heap size.
Requires a struct has a container attribute `#[heap_size]`.
* `#[heap_size(with = "module")]`
`#[derive(HeapSize)]` will use `$module::heap_size` as the function to obtain
this field’s heap size.### Container attributes
Apply to a struct or enum declaration.
* `#[heap_size]`
By default, `#[derive(HeapSize)]` generates an empty implementation that
always return 0. By adding `#[heap_size]`, it sums up `heap_size()` of all
fields in a struct or an enum.### Variant attributes
Apply to a variant of an enum.
* `#[heap_size]`
Generate
By default, `#[derive(HeapSize)]` generates an empty implementation which
always return 0. By adding `#[heap_size]`, it sums up `heap_size()` of fields
in a variant.* `#[heap_size(skip)]`
Skip this variant: do not calculate its heap size.
Requires an enum has a `#[heap_size]` container attribute.
## License
This project is licensed under the [MIT license](https://github.com/overvenus/heapsz/blob/main/LICENSE).