Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 20, 2023
1 parent 56fb271 commit 5a92c6b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ pub fn no_memory_allocations() {
code_that_should_not_allocate_memory();
});
assert_eq!(allocations, 0);

// Or use this utility method in this case:
allocation_counter::assert_no_allocations(|| {
code_that_should_not_allocate_memory();
});

// Can also allow a certain number of allocations:
allocation_counter::assert_max_allocations(10 || {
code_that_should_not_allocate_much();
});

// Can also assert on a range, useful to adjust
// test expectations over time:
allocation_counter::assert_num_allocations(500..600 || {
code_that_should_not_allocate_much();
});

// It's possible to opt out of counting allocations
// for certain parts of the code flow:
allocation_counter::assert_no_allocations(|| {
code_that_should_not_allocate();
allocation_counter::avoid_counting(|| {
external_code_that_should_not_be_tested();
});
code_that_should_not_allocate();
});
}
```

Expand Down

0 comments on commit 5a92c6b

Please sign in to comment.