From 5eb823896f01146df0d4ed6c296ef7dd445ccbbf Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 1 Apr 2023 15:27:09 +0200 Subject: fix: make expectation returning method unsafe --- examples/automock.rs | 18 +++++++++-------- examples/basic.rs | 14 +++++++------ examples/generic_fn_method.rs | 32 +++++++++++++++++------------- examples/generic_method.rs | 46 +++++++++++++++++++++++-------------------- examples/generic_trait.rs | 16 +++++++++------ macros/src/expectation.rs | 8 +++++++- 6 files changed, 78 insertions(+), 56 deletions(-) diff --git a/examples/automock.rs b/examples/automock.rs index a597678..a0d638a 100644 --- a/examples/automock.rs +++ b/examples/automock.rs @@ -26,14 +26,16 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo - .expect_bar() - .with(eq(1234), always()) - .returning(|_, num, text| { - println!("bar was called with {num} and '{text}'"); - - 9.36f32 - }); + unsafe { + mock_foo + .expect_bar() + .with(eq(1234), always()) + .returning(|_, num, text| { + println!("bar was called with {num} and '{text}'"); + + 9.36f32 + }); + } mock_foo.bar::(1234, "Hello"); } diff --git a/examples/basic.rs b/examples/basic.rs index 01688f5..189b45a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -19,12 +19,14 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo - .expect_bar() - .with(eq(1234), always()) - .returning(|_, num, text| { - println!("bar was called with {num} and '{text}'"); - }); + unsafe { + mock_foo + .expect_bar() + .with(eq(1234), always()) + .returning(|_, num, text| { + println!("bar was called with {num} and '{text}'"); + }); + } mock_foo.bar(1234, "Hello"); } diff --git a/examples/generic_fn_method.rs b/examples/generic_fn_method.rs index 7cf0d78..3330269 100644 --- a/examples/generic_fn_method.rs +++ b/examples/generic_fn_method.rs @@ -32,25 +32,29 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo - .expect_do_something() - .returning(|_me, cb: fn(_) -> _| { - println!("do_something was called"); + unsafe { + mock_foo + .expect_do_something() + .returning(|_me, cb: fn(_) -> _| { + println!("do_something was called"); - let cb_res = cb(42); + let cb_res = cb(42); - println!("Received '{cb_res}' from callback"); - }); + println!("Received '{cb_res}' from callback"); + }); + } - mock_foo - .expect_do_something_else() - .returning(|_me, cb: fn(_) -> PathBuf| { - println!("do_something_else was called"); + unsafe { + mock_foo + .expect_do_something_else() + .returning(|_me, cb: fn(_) -> PathBuf| { + println!("do_something_else was called"); - let cb_res = cb(255.255); + let cb_res = cb(255.255); - println!("Received '{}' from callback", cb_res.display()); - }); + println!("Received '{}' from callback", cb_res.display()); + }); + } mock_foo.do_something((|num| format!("Hello there {num}")) as fn(_) -> _); diff --git a/examples/generic_method.rs b/examples/generic_method.rs index c4e3214..c86864d 100644 --- a/examples/generic_method.rs +++ b/examples/generic_method.rs @@ -39,31 +39,35 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo - .expect_bar::() - .returning(|_me, num| { + unsafe { + mock_foo.expect_bar::().returning(|_me, num| { println!("bar was called with {num}"); "Hello".to_string() }) - .times(3); - - mock_foo.expect_bar::<&str>().returning(|_me, num| { - println!("bar was called with {num}"); - - 128u8 - }); - - mock_foo - .expect_abc::<&str, f64>() - .with( - function(|thing_a: &&str| thing_a.starts_with("Good morning")), - is_close(7.13081), - ) - .returning(|_me, _thing_a, _thing_b| { - println!("abc was called"); - }) - .times(1); + } + .times(3); + + unsafe { + mock_foo.expect_bar::<&str>().returning(|_me, num| { + println!("bar was called with {num}"); + + 128u8 + }); + } + + unsafe { + mock_foo + .expect_abc::<&str, f64>() + .with( + function(|thing_a: &&str| thing_a.starts_with("Good morning")), + is_close(7.13081), + ) + .returning(|_me, _thing_a, _thing_b| { + println!("abc was called"); + }) + } + .times(1); assert_eq!(mock_foo.bar::(123), "Hello".to_string()); assert_eq!(mock_foo.bar::(123), "Hello".to_string()); diff --git a/examples/generic_trait.rs b/examples/generic_trait.rs index d863d85..b821f43 100644 --- a/examples/generic_trait.rs +++ b/examples/generic_trait.rs @@ -20,13 +20,17 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo.expect_bar().returning(|_me, something: &f32| { - println!("bar was called with {something}"); - }); + unsafe { + mock_foo.expect_bar().returning(|_me, something: &f32| { + println!("bar was called with {something}"); + }); + } - mock_foo.expect_bar().returning(|_me, something: &f64| { - println!("bar was called with {something}"); - }); + unsafe { + mock_foo.expect_bar().returning(|_me, something: &f64| { + println!("bar was called with {something}"); + }); + } mock_foo.bar(&0.826f32); diff --git a/macros/src/expectation.rs b/macros/src/expectation.rs index 2e86db0..d724655 100644 --- a/macros/src/expectation.rs +++ b/macros/src/expectation.rs @@ -439,8 +439,14 @@ impl ToTokens for Expectation } } + /// Sets the function that will be called to provide the return value of + /// 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. #[allow(unused)] - pub fn returning( + pub unsafe fn returning( &mut self, func: #returning_fn ) -> &mut Self -- cgit v1.2.3-18-g5258