From 5a92c6b0307977a489e64e8503d073ce08d57a4c Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Wed, 20 Sep 2023 13:28:08 +0200 Subject: [PATCH] Update README.md --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 1322e2a..dff6568 100644 --- a/README.md +++ b/README.md @@ -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(); + }); } ```