Skip to main content

main/
main.rs

1use nice_assert_no_alloc::*;
2
3#[cfg(debug_assertions)]
4#[global_allocator]
5static A: AllocDisabler = AllocDisabler;
6
7fn main() {
8    println!("Alloc is allowed. Let's allocate some memory...");
9    let mut vec_can_push = Vec::new();
10    vec_can_push.push(42);
11
12    println!();
13
14    let fib5 = assert_no_alloc(|| {
15        println!("Alloc is forbidden. Let's calculate something without memory allocations...");
16
17        fn fib(n: u32) -> u32 {
18            if n <= 1 {
19                1
20            } else {
21                fib(n - 1) + fib(n - 2)
22            }
23        }
24
25        fib(5)
26    });
27    println!("\tSuccess, the 5th fibonacci number is {}", fib5);
28    println!();
29
30    assert_no_alloc(|| {
31        println!("Alloc is forbidden. Let's allocate some memory...");
32        let mut vec_cannot_push = Vec::new();
33        vec_cannot_push.push(42); // panics
34    });
35
36    println!("This will not be executed if the above allocation has aborted.");
37}