Add an async demonstration

pull/3/head
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" thread-id = "3.3"
qadapt-macro = { version = "0.7.1", path = "./qadapt-macro" } 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); 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 /// To make use of the allocator, include this code block in your program
/// binaries/tests: /// binaries/tests:
/// ///
/// ```rust,ignore /// ```rust,ignore
/// use qadapt::QADAPT; /// use qadapt::QADAPT;
/// ///
/// #[global_allocator] /// #[global_allocator]
/// static Q: QADAPT = QADAPT; /// static Q: QADAPT = QADAPT;
/// ``` /// ```
@ -84,23 +84,23 @@ pub struct QADAPT;
/// Let QADAPT know that we are now entering a protected region and that /// 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. /// panics should be triggered if allocations/drops happen while we are running.
/// ///
/// **Example**: /// **Example**:
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use qadapt::enter_protected; /// use qadapt::enter_protected;
/// use qadapt::exit_protected; /// use qadapt::exit_protected;
/// ///
/// fn main() { /// fn main() {
/// // Force an allocation by using a Box /// // Force an allocation by using a Box
/// let x = Box::new(2); /// let x = Box::new(2);
/// ///
/// enter_protected(); /// enter_protected();
/// // We're now in a memory-protected region - allocations and drops /// // We're now in a memory-protected region - allocations and drops
/// // here will trigger thread panic /// // here will trigger thread panic
/// let y = *x * 4; /// let y = *x * 4;
/// exit_protected(); /// exit_protected();
/// ///
/// // It's now safe to allocate/drop again /// // It's now safe to allocate/drop again
/// let z = Box::new(y); /// 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 /// Let QADAPT know that we are exiting a protected region. Will panic
/// if we attempt to [`exit_protected`] more times than we [`enter_protected`]. /// if we attempt to [`exit_protected`] more times than we [`enter_protected`].
/// ///
/// **Example**: /// **Example**:
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use qadapt::enter_protected; /// use qadapt::enter_protected;
/// use qadapt::exit_protected; /// use qadapt::exit_protected;
/// ///
/// fn main() { /// fn main() {
/// // Force an allocation by using a Box /// // Force an allocation by using a Box
/// let x = Box::new(2); /// let x = Box::new(2);
/// ///
/// enter_protected(); /// enter_protected();
/// // We're now in a memory-protected region - allocations and drops /// // We're now in a memory-protected region - allocations and drops
/// // here will trigger thread panic /// // here will trigger thread panic
/// let y = *x * 4; /// let y = *x * 4;
/// exit_protected(); /// exit_protected();
/// ///
/// // It's now safe to allocate/drop again /// // It's now safe to allocate/drop again
/// let z = Box::new(y); /// 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 /// Get the result of an expression, guaranteeing that no memory accesses occur
/// during its evaluation. /// during its evaluation.
/// ///
/// **Example**: /// **Example**:
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use qadapt::assert_no_alloc; /// use qadapt::assert_no_alloc;
/// ///
/// fn main() { /// fn main() {
/// assert_no_alloc!(2 + 2); /// 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 /// 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 /// in code that was not intended to be allocation-free. The compiler will warn you
/// that there is an unreachable statement if this happens. /// that there is an unreachable statement if this happens.
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use qadapt::assert_no_alloc; /// use qadapt::assert_no_alloc;
/// ///
/// fn early_return() -> usize { /// fn early_return() -> usize {
/// assert_no_alloc!(return 8); /// assert_no_alloc!(return 8);
/// } /// }
/// ///
/// fn main() { /// fn main() {
/// let x = early_return(); /// 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()); static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value());
/// Get the current "protection level" in QADAPT: calls to enter_protected() - exit_protected() /// Get the current "protection level" in QADAPT: calls to enter_protected() - exit_protected()
/// ///
/// **Example**: /// **Example**:
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use qadapt::enter_protected; /// use qadapt::enter_protected;
/// use qadapt::exit_protected; /// use qadapt::exit_protected;
/// use qadapt::protection_level; /// use qadapt::protection_level;
/// ///
/// fn main() { /// fn main() {
/// enter_protected(); /// enter_protected();
/// // We're now in an allocation-protected code region /// // We're now in an allocation-protected code region

View File

@ -39,4 +39,4 @@ fn list_push() {
} else { } else {
panic!("Intentional") 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")
}
}