From 9e5b2b0f7a6f9b5267ceb0ed226b1ff383712392 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 8 Dec 2018 18:05:46 -0500 Subject: [PATCH] Add an async demonstration --- Cargo.toml | 3 +++ src/lib.rs | 46 +++++++++++++++++++++---------------------- tests/assert_macro.rs | 2 +- tests/async.rs | 33 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 tests/async.rs diff --git a/Cargo.toml b/Cargo.toml index 143e764..eb29853 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 8d3e7b0..6cb831a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,14 +69,14 @@ thread_local! { static PROTECTION_LEVEL: RwLock = 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 = RwLock::new(false); static INTERNAL_ALLOCATION: RwLock = 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 diff --git a/tests/assert_macro.rs b/tests/assert_macro.rs index 8befd29..788288e 100644 --- a/tests/assert_macro.rs +++ b/tests/assert_macro.rs @@ -39,4 +39,4 @@ fn list_push() { } else { panic!("Intentional") } -} \ No newline at end of file +} diff --git a/tests/async.rs b/tests/async.rs new file mode 100644 index 0000000..0a599a4 --- /dev/null +++ b/tests/async.rs @@ -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, 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") + } +}