1
0
mirror of https://github.com/bspeice/qadapt synced 2024-12-21 20:18:08 -05:00

Add an async demonstration

This commit is contained in:
Bradlee Speice 2018-12-08 18:05:46 -05:00
parent 26ebd50fc0
commit 9e5b2b0f7a
4 changed files with 60 additions and 24 deletions

View File

@ -24,3 +24,6 @@ spin = { git = "https://github.com/bspeice/spin-rs.git" }
thread-id = "3.3"
qadapt-macro = { version = "0.7.1", path = "./qadapt-macro" }
[dev-dependencies]
futures = "0.1"

View File

@ -69,14 +69,14 @@ thread_local! {
static PROTECTION_LEVEL: RwLock<usize> = RwLock::new(0);
}
/// The QADAPT allocator itself
///
/// The QADAPT allocator itself
///
/// To make use of the allocator, include this code block in your program
/// binaries/tests:
///
///
/// ```rust,ignore
/// use qadapt::QADAPT;
///
///
/// #[global_allocator]
/// static Q: QADAPT = QADAPT;
/// ```
@ -84,23 +84,23 @@ pub struct QADAPT;
/// Let QADAPT know that we are now entering a protected region and that
/// panics should be triggered if allocations/drops happen while we are running.
///
///
/// **Example**:
///
///
/// ```rust,no_run
/// use qadapt::enter_protected;
/// use qadapt::exit_protected;
///
///
/// fn main() {
/// // Force an allocation by using a Box
/// let x = Box::new(2);
///
///
/// enter_protected();
/// // We're now in a memory-protected region - allocations and drops
/// // here will trigger thread panic
/// let y = *x * 4;
/// exit_protected();
///
///
/// // It's now safe to allocate/drop again
/// let z = Box::new(y);
/// }
@ -125,23 +125,23 @@ pub fn enter_protected() {
/// Let QADAPT know that we are exiting a protected region. Will panic
/// if we attempt to [`exit_protected`] more times than we [`enter_protected`].
///
///
/// **Example**:
///
///
/// ```rust,no_run
/// use qadapt::enter_protected;
/// use qadapt::exit_protected;
///
///
/// fn main() {
/// // Force an allocation by using a Box
/// let x = Box::new(2);
///
///
/// enter_protected();
/// // We're now in a memory-protected region - allocations and drops
/// // here will trigger thread panic
/// let y = *x * 4;
/// exit_protected();
///
///
/// // It's now safe to allocate/drop again
/// let z = Box::new(y);
/// }
@ -168,12 +168,12 @@ pub fn exit_protected() {
/// Get the result of an expression, guaranteeing that no memory accesses occur
/// during its evaluation.
///
///
/// **Example**:
///
///
/// ```rust,no_run
/// use qadapt::assert_no_alloc;
///
///
/// fn main() {
/// assert_no_alloc!(2 + 2);
/// }
@ -183,14 +183,14 @@ pub fn exit_protected() {
/// 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.
///
///
/// ```rust,no_run
/// use qadapt::assert_no_alloc;
///
///
/// fn early_return() -> usize {
/// assert_no_alloc!(return 8);
/// }
///
///
/// fn main() {
/// let x = early_return();
///
@ -213,14 +213,14 @@ static IS_ACTIVE: RwLock<bool> = RwLock::new(false);
static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value());
/// Get the current "protection level" in QADAPT: calls to enter_protected() - exit_protected()
///
///
/// **Example**:
///
///
/// ```rust,no_run
/// use qadapt::enter_protected;
/// use qadapt::exit_protected;
/// use qadapt::protection_level;
///
///
/// fn main() {
/// enter_protected();
/// // We're now in an allocation-protected code region

View File

@ -39,4 +39,4 @@ fn list_push() {
} else {
panic!("Intentional")
}
}
}

33
tests/async.rs Normal file
View File

@ -0,0 +1,33 @@
use futures::future::ok;
use futures::prelude::*;
use qadapt::assert_no_alloc;
use qadapt::no_alloc;
use qadapt::QADAPT;
#[global_allocator]
static Q: QADAPT = QADAPT;
#[no_alloc]
fn async_box() -> impl Future<Item = Box<u8>, Error = ()> {
ok(12).and_then(|e| Ok(Box::new(e)))
}
#[test]
fn raw_call() {
async_box();
}
#[test]
fn guarded_call() {
assert_no_alloc!(async_box());
}
#[test]
#[should_panic]
fn guarded_poll() {
if cfg!(debug_assertions) {
assert_no_alloc!(async_box().poll().unwrap());
} else {
panic!("Intentional")
}
}