diff options
author | HampusM <hampus@hampusmat.com> | 2023-04-15 22:44:03 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-04-15 22:44:03 +0200 |
commit | 22e8ebf22d5608c36dfe3b29957415e688d91b42 (patch) | |
tree | 5bf1ee484e71bff076a2629f4328ecc5588d7550 /macros/src/expectation.rs | |
parent | 7e1bbbc738362896e8956306b3dc02b1de406ac1 (diff) |
Diffstat (limited to 'macros/src/expectation.rs')
-rw-r--r-- | macros/src/expectation.rs | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/macros/src/expectation.rs b/macros/src/expectation.rs index d724655..c5cd758 100644 --- a/macros/src/expectation.rs +++ b/macros/src/expectation.rs @@ -443,8 +443,73 @@ impl ToTokens for Expectation /// this expectation. /// /// # Safety - /// The caller must ensure that no argument or return type is outlived. They must - /// be treated as if they are bound to 'static. + /// The caller must ensure that no argument or return type is used in a + /// way that exceeds its actual lifetime. + /// + /// This example is **OK**: + /// ``` + /// use ridicule::automock; + /// + /// #[automock] + /// trait Foo + /// { + /// fn do_stuff<Something>(&self, something: Something); + /// } + /// + /// # fn main() { + /// let mut mock_foo = MockFoo::new(); + /// + /// unsafe { + /// mock_foo.expect_do_stuff::<u32>().returning(|_, something| { + /// println!("Doing stuff with {something}"); + /// }); + /// } + /// + /// mock_foo.do_stuff(1234u32); + /// # } + /// ``` + /// + /// This example is **NOT OK**: + /// ```no_run + /// use std::slice::Chunks; + /// use std::sync::Mutex; + /// + /// use ridicule::automock; + /// + /// static cool_chunks: Mutex<Option<Chunks<i64>>> = Mutex::new(None); + /// + /// #[automock] + /// trait Foo + /// { + /// fn do_stuff<Something>(&self, something: Something); + /// } + /// + /// # fn main() { + /// let mut mock_foo = MockFoo::new(); + /// + /// unsafe { + /// mock_foo.expect_do_stuff::<Chunks<i64>>().returning(|_, chunks| { + /// // NEVER EVER DO THIS + /// *cool_chunks.lock().unwrap() = Some(chunks); + /// }); + /// } + /// + /// { + /// let items: Vec<i64> = Vec::from([9876, 567, 13579, -234, 890, -765]); + /// + /// mock_foo.do_stuff(items.chunks(2)); + /// + /// // items go out of scope here + /// } + /// + /// let cool_chunks_lock = cool_chunks.lock().unwrap(); + /// + /// // Undefined behavior here + /// let mut cool_chunks_cloned = cool_chunks_lock.clone().unwrap(); + /// + /// let garbage = cool_chunks_cloned.next(); + /// # } + /// ``` #[allow(unused)] pub unsafe fn returning( &mut self, |