1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-21 13:28:11 -05:00
Go to file
Bradlee Speice 08dac29b82 Remove no_run from doctests
Uses `qadapt::is_active` to work around rust-lang/cargo#6570.
Currently assumes that *some* allocation will have happened
by the time `fn main()` is called, which is sub-par.
2019-01-20 14:40:57 -05:00
examples Renaming and a new macro 2018-12-06 23:02:44 -05:00
qadapt-macro Go back to the spin crate as a dependency 2019-01-09 00:26:07 -05:00
src Remove no_run from doctests 2019-01-20 14:40:57 -05:00
tests Add an is_active function, and remove panic on not using 2019-01-20 13:19:02 -05:00
.gitignore Add an is_active function, and remove panic on not using 2019-01-20 13:19:02 -05:00
.travis.yml Remove qadapt-spin from the publish build 2019-01-09 00:35:04 -05:00
appveyor.yml Add testing in stable/beta branches 2018-12-06 23:16:35 -05:00
Cargo.toml Go back to the spin crate as a dependency 2019-01-09 00:26:07 -05:00
CHANGELOG.md Update the changelog for 1.0.1 2019-01-01 14:23:14 -05:00
CONTRIBUTING.md Use new CI template 2018-12-02 22:42:29 -05:00
CONTRIBUTORS.md Use new CI template 2018-12-02 22:42:29 -05:00
LICENSE Use new CI template 2018-12-02 22:42:29 -05:00
Makefile Prepare for 1.0 2018-12-15 16:12:06 -05:00
README.md Prepare for 1.0 2018-12-15 16:12:06 -05:00
rustfmt.toml Use new CI template 2018-12-02 22:42:29 -05:00

qadapt

crates.io docs.rs codecov travisci appveyor


debug_assert! for your memory usage

This allocator is a helper for writing high-performance code that is memory-sensitive; a thread panic will be triggered if a function annotated with #[no_alloc], or code inside an assert_no_alloc! macro interacts with the allocator in any way. Wanton allocations and unforeseen drops no more - this library lets you focus on writing code without worrying if Rust properly managed to inline the variable into the stack.

Now, an allocator blowing up in production is a scary thought; that's why QADAPT is designed to strip its own code out whenever you're running with a release build. Just like the debug_assert! macro in Rust's standard library, it's safe to use without worrying about a unforeseen circumstance causing your application to crash.

Usage

Actually making use of QADAPT is straight-forward. To set up the allocator, place the following snippet in either your program binaries (main.rs) or tests:

use qadapt::QADAPT;

#[global_allocator]
static Q: QADAPT = QADAPT;

After that, there are two ways of telling QADAPT that it should trigger a panic:

  1. Annotate functions with the #[no_alloc] proc macro:
use qadapt::no_alloc;

// This function is fine, there are no allocations here
#[no_alloc]
fn do_math() -> u8 {
    2 + 2
}

// This function will trigger a panic when called
#[no_alloc]
fn does_panic() -> Box<u32> {
    Box::new(5)
}

fn main() {
    do_math();
    does_panic();
}
  1. Evaluate expressions with the assert_no_alloc! macro
use qadapt::assert_no_alloc;

fn do_work() {
    // This code is allowed to trigger an allocation
    let b = Box::new(8);
    
    // This code would panic if an allocation occurred inside it
    let x = assert_no_alloc!(*b + 2);
    assert_eq!(x, 10);
}