[][src]Macro qadapt::assert_no_alloc

macro_rules! assert_no_alloc {
    ($e:expr) => { ... };
}
Deprecated since 1.0.3:

Please use the alloc_counter crate instead.

Get the result of an expression, guaranteeing that no memory accesses occur during its evaluation.

Example:

use qadapt::assert_no_alloc;
use qadapt::QADAPT;

#[global_allocator]
static Q: QADAPT = QADAPT;

fn main() {
    assert_no_alloc!(2 + 2);
}

Warning: Unexpected behavior will occur when using the return keyword. Because QADAPT doesn't have an opportunity to clean up, there may be a panic in code that was not intended to be allocation-free. The compiler will warn you that there is an unreachable statement if this happens.

use qadapt::assert_no_alloc;
use qadapt::QADAPT;
use std::panic::catch_unwind;

#[global_allocator]
static Q: QADAPT = QADAPT;

fn early_return() -> usize {
    assert_no_alloc!(return 8);
}

fn main() {
    let x = early_return();
     
    // Even though only the `early_return` function contains
    // QADAPT allocation guards, this triggers a panic:
    // `Box::new` forces an allocation, and QADAPT still thinks
    // we're in a protected region because of the return in  `early_return()`
    let res = catch_unwind(|| Box::new(x));
    assert!(res.is_err());
}