diff options
author | HampusM <hampus@hampusmat.com> | 2023-03-19 13:45:07 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-03-19 13:45:07 +0100 |
commit | 657673f4a25a2a7299d3751d54d9597635bc529d (patch) | |
tree | 0a1677d1b53958f19f392e5fc11f21d7f659485c /examples/generic_fn_method.rs | |
parent | d03c747cfef277fda5823e08784c94c30c7f3964 (diff) |
docs: add examples
Diffstat (limited to 'examples/generic_fn_method.rs')
-rw-r--r-- | examples/generic_fn_method.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/generic_fn_method.rs b/examples/generic_fn_method.rs new file mode 100644 index 0000000..7cf0d78 --- /dev/null +++ b/examples/generic_fn_method.rs @@ -0,0 +1,58 @@ +use std::path::PathBuf; + +use ridicule::mock; + +trait Foo +{ + fn do_something<Callback>(&self, cb: Callback) + where + Callback: Fn(u64) -> String; + + fn do_something_else<Callback, Value>(&self, cb: Callback) + where + Callback: Fn(f32) -> Value; +} + +mock! { + MockFoo {} + + impl Foo for MockFoo + { + fn do_something<Callback>(&self, cb: Callback) + where + Callback: Fn(u64) -> String; + + fn do_something_else<Callback, Value>(&self, cb: Callback) + where + Callback: Fn(f32) -> Value; + } +} + +fn main() +{ + let mut mock_foo = MockFoo::new(); + + mock_foo + .expect_do_something() + .returning(|_me, cb: fn(_) -> _| { + println!("do_something was called"); + + let cb_res = cb(42); + + println!("Received '{cb_res}' from callback"); + }); + + mock_foo + .expect_do_something_else() + .returning(|_me, cb: fn(_) -> PathBuf| { + println!("do_something_else was called"); + + let cb_res = cb(255.255); + + println!("Received '{}' from callback", cb_res.display()); + }); + + mock_foo.do_something((|num| format!("Hello there {num}")) as fn(_) -> _); + + mock_foo.do_something_else((|_| PathBuf::from(r"a/b/c.txt")) as fn(_) -> _); +} |