diff options
author | HampusM <hampus@hampusmat.com> | 2023-03-18 21:26:54 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-03-18 21:26:54 +0100 |
commit | 2d964b39da09ad82eccf09abdea73967bbff76f2 (patch) | |
tree | d5b43196d2402e62559e999adb65ef99f584eaf7 /examples | |
parent | 43e0bdb4cc598f199eacb63f755f30dc2108146b (diff) |
feat: add support for generic traits
Diffstat (limited to 'examples')
-rw-r--r-- | examples/simple.rs | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/examples/simple.rs b/examples/simple.rs index cb6feb9..c5494da 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -7,7 +7,7 @@ trait SomeFoobar fn do_something(&self) -> bool; } -trait Foo +trait Foo<Xyz> { fn bar<Baz>(&self, num: u128) -> Baz; @@ -16,12 +16,14 @@ trait Foo fn baz<Foobar>(&self, name: &str, foobar: Foobar) where Foobar: SomeFoobar + Debug; + + fn hello(&self, xyz: Xyz); } mock! { MockFoo {} - impl Foo for MockFoo { + impl<Xyz> Foo<Xyz> for MockFoo { fn bar<Baz>(&self, num: u128) -> Baz; fn biz<'a, Fiz: Debug, Bar>(&'a self, fiz: Fiz) -> &'a Bar; @@ -29,6 +31,8 @@ mock! { fn baz<Foobar>(&self, name: &str, foobar: Foobar) where Foobar: SomeFoobar + Debug; + + fn hello(&self, xyz: Xyz); } } @@ -36,19 +40,38 @@ fn main() { let mut mock_foo = MockFoo::new(); - mock_foo.expect_bar().returning(|_me, num| { + mock_foo.expect_bar::<_, Vec<u8>>().returning(|_me, num| { println!("bar was called with {num}"); "Hello".to_string() }); - mock_foo.expect_bar::<u128>().returning(|_me, num| { + mock_foo + .expect_bar::<u128, Vec<u8>>() + .returning(|_me, num| { + println!("bar was called with {num}"); + + 136322 + }); + + mock_foo.expect_bar::<_, String>().returning(|_me, num| { println!("bar was called with {num}"); - 136322 + "Greetings".to_string() }); - assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string()); + assert_eq!( + <MockFoo as Foo<Vec<u8>>>::bar::<String>(&mock_foo, 123), + "Hello".to_string() + ); + + assert_eq!( + <MockFoo as Foo<Vec<u8>>>::bar::<u128>(&mock_foo, 456), + 136322 + ); - assert_eq!(mock_foo.bar::<u128>(456), 136322); + assert_eq!( + <MockFoo as Foo<String>>::bar::<String>(&mock_foo, 789), + "Greetings".to_string() + ); } |