qadapt/README.md

96 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2018-12-02 22:42:29 -05:00
# qadapt
2018-09-23 12:41:11 -04:00
2018-11-15 20:16:49 -05:00
[![crates.io](https://img.shields.io/crates/v/qadapt.svg)](https://crates.io/crates/qadapt)
[![docs.rs](https://docs.rs/qadapt/badge.svg)](https://docs.rs/qadapt/)
2018-12-02 22:42:29 -05:00
[![codecov](https://codecov.io/gh/bspeice/qadapt/branch/master/graph/badge.svg)](https://codecov.io/gh/bspeice/qadapt)
2018-12-02 22:48:46 -05:00
[![travisci](https://travis-ci.org/bspeice/qadapt.svg?branch=master)](https://travis-ci.org/bspeice/qadapt)
[![appveyor](https://ci.appveyor.com/api/projects/status/km1p081tkjcptn1w/branch/master?svg=true)](https://ci.appveyor.com/project/bspeice/qadapt/branch/master)
2018-12-02 22:42:29 -05:00
---
2018-12-06 23:36:28 -05:00
## `debug_assert!` for your memory usage
2018-11-15 20:16:49 -05:00
2019-02-11 00:18:16 -05:00
**Please note**: This crate has been deprecated in favor of [alloc-counter](https://crates.io/crates/alloc_counter).
2018-12-06 23:02:44 -05:00
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.
2018-11-11 22:34:05 -05:00
2018-12-06 23:02:44 -05:00
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](https://doc.rust-lang.org/std/macro.debug_assert.html)
in Rust's standard library, it's safe to use without worrying about a unforeseen
circumstance causing your application to crash.
2018-11-11 22:34:05 -05:00
2018-12-06 23:02:44 -05:00
# 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:
2019-02-11 00:18:16 -05:00
```rust
2018-12-06 23:02:44 -05:00
use qadapt::QADAPT;
#[global_allocator]
static Q: QADAPT = QADAPT;
2019-02-11 00:18:16 -05:00
fn main() {
# // Because `debug_assertions` are on for doctests in release mode
# // we have to add an extra guard.
# if qadapt::is_active() {
assert!(qadapt::is_active());
# }
}
2018-12-06 23:02:44 -05:00
```
After that, there are two ways of telling QADAPT that it should trigger a panic:
1. Annotate functions with the `#[no_alloc]` proc macro:
2019-02-11 00:18:16 -05:00
```rust
2018-12-06 23:02:44 -05:00
use qadapt::no_alloc;
2019-02-11 00:18:16 -05:00
use qadapt::QADAPT;
use std::panic::catch_unwind;
#[global_allocator]
static Q: QADAPT = QADAPT;
2018-12-06 23:02:44 -05:00
2018-12-15 16:12:06 -05:00
// This function is fine, there are no allocations here
2018-12-06 23:02:44 -05:00
#[no_alloc]
fn do_math() -> u8 {
2018-12-06 23:36:28 -05:00
2 + 2
2018-12-06 23:02:44 -05:00
}
2018-12-15 16:12:06 -05:00
// This function will trigger a panic when called
#[no_alloc]
fn does_panic() -> Box<u32> {
Box::new(5)
}
fn main() {
do_math();
2019-02-11 00:18:16 -05:00
let err = catch_unwind(|| does_panic());
# if qadapt::is_active() {
assert!(err.is_err());
# }
2018-12-15 16:12:06 -05:00
}
2018-12-06 23:02:44 -05:00
```
2. Evaluate expressions with the `assert_no_alloc!` macro
2019-02-11 00:18:16 -05:00
```rust
2018-12-06 23:02:44 -05:00
use qadapt::assert_no_alloc;
2019-02-11 00:18:16 -05:00
use qadapt::QADAPT;
2018-12-06 23:02:44 -05:00
2019-02-11 00:18:16 -05:00
#[global_allocator]
static Q: QADAPT = QADAPT;
fn main() {
2018-12-06 23:36:28 -05:00
// 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);
2018-12-06 23:02:44 -05:00
}
2019-02-11 00:18:16 -05:00
```