diff options
author | HampusM <hampus@hampusmat.com> | 2023-04-01 15:00:06 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-04-01 15:03:29 +0200 |
commit | ba865b581fbc1d0589b402b028f2bce70332891b (patch) | |
tree | 752e7be1150b8a5412b4351803edc6970916d9fa /examples | |
parent | 1628732d6514670fe2108e5063e9d5ba7166ad94 (diff) |
feat: allow for usage of associated types of generics
Diffstat (limited to 'examples')
-rw-r--r-- | examples/generic_method.rs | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/examples/generic_method.rs b/examples/generic_method.rs index 7283810..c4e3214 100644 --- a/examples/generic_method.rs +++ b/examples/generic_method.rs @@ -1,12 +1,15 @@ -use std::fmt::Display; - use predicates::float::is_close; use ridicule::mock; use ridicule::predicate::function; +trait Haha +{ + type Hello; +} + trait Foo { - fn bar<Baz: Display>(&self, num: u128) -> Baz; + fn bar<Baz: Haha>(&self, num: u128) -> Baz::Hello; fn abc<ThingA, ThingB>(&mut self, thing_a: ThingA, thing_b: ThingB); } @@ -16,18 +19,28 @@ mock! { impl Foo for MockFoo { - fn bar<Baz: Display>(&self, num: u128) -> Baz; + fn bar<Baz: Haha>(&self, num: u128) -> Baz::Hello; fn abc<ThingA, ThingB>(&mut self, thing_a: ThingA, thing_b: ThingB); } } +impl Haha for u16 +{ + type Hello = String; +} + +impl Haha for &str +{ + type Hello = u8; +} + fn main() { let mut mock_foo = MockFoo::new(); mock_foo - .expect_bar() + .expect_bar::<u16>() .returning(|_me, num| { println!("bar was called with {num}"); @@ -35,7 +48,7 @@ fn main() }) .times(3); - mock_foo.expect_bar().returning(|_me, num| { + mock_foo.expect_bar::<&str>().returning(|_me, num| { println!("bar was called with {num}"); 128u8 @@ -52,14 +65,14 @@ fn main() }) .times(1); - assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string()); - assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string()); - assert_eq!(mock_foo.bar::<String>(123), "Hello".to_string()); + assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string()); + assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string()); + assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string()); // Would panic // mock_foo.bar::<String>(123); - assert_eq!(mock_foo.bar::<u8>(456), 128); + assert_eq!(mock_foo.bar::<&str>(456), 128); mock_foo.abc( concat!( |