-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Allow generics in method return types and arguments.
Partial fix fo #18 This changeset allows generics to method return types and arguments. Generic impl traits are still not supported in return position (yet?).
- Loading branch information
1 parent
9f2956b
commit 83ea074
Showing
2 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![allow(clippy::disallowed_names)] | ||
|
||
pub trait MyTrait {} | ||
|
||
#[derive(Clone, PartialEq, Debug)] | ||
struct Entity {} | ||
impl MyTrait for Entity {} | ||
|
||
#[faux::create] | ||
pub struct Foo {} | ||
|
||
#[faux::create] | ||
pub struct Bar {} | ||
|
||
|
||
|
||
#[faux::methods] | ||
impl Foo { | ||
pub fn foo<E: MyTrait>(&self, _e: E) -> E { | ||
todo!() | ||
} | ||
pub fn bar<E: MyTrait, F: MyTrait>(&self, _e: E, _f: F) -> Result<E, F> { | ||
todo!() | ||
} | ||
pub fn baz<E>(&self, _e: E) -> E where E: MyTrait { | ||
todo!() | ||
} | ||
} | ||
|
||
|
||
#[faux::create] | ||
struct AsyncFoo {} | ||
#[faux::methods] | ||
impl AsyncFoo { | ||
pub async fn foo<E: MyTrait>(&self, _e: E) -> E { | ||
todo!() | ||
} | ||
pub async fn bar<E: MyTrait, F: MyTrait>(&self, _e: E, _f: F) -> Result<E, F> { | ||
todo!() | ||
} | ||
pub async fn baz<E>(&self, _e: E) -> E where E: MyTrait { | ||
todo!() | ||
} | ||
} | ||
|
||
|
||
#[test] | ||
fn generics() { | ||
let mut foo = Foo::faux(); | ||
faux::when!(foo.foo).then_return(Entity {}); | ||
assert_eq!(foo.foo(Entity {}), Entity {}); | ||
|
||
let mut bar = Foo::faux(); | ||
faux::when!(bar.bar).then_return(Ok::<_, Entity>(Entity {})); | ||
assert_eq!(bar.bar(Entity {}, Entity {}), Ok(Entity {})); | ||
|
||
let mut baz = Foo::faux(); | ||
faux::when!(baz.baz).then_return(Entity {}); | ||
assert_eq!(baz.baz(Entity {}), Entity {}); | ||
} | ||
|
||
#[test] | ||
fn generic_tests_async() { | ||
let mut foo: AsyncFoo = AsyncFoo::faux(); | ||
faux::when!(foo.foo).then_return(Entity {}); | ||
|
||
let mut bar = AsyncFoo::faux(); | ||
faux::when!(bar.bar).then_return(Ok::<_, Entity>(Entity {})); | ||
|
||
let mut baz = AsyncFoo::faux(); | ||
faux::when!(baz.baz).then_return(Entity {}); | ||
futures::executor::block_on(async { | ||
assert_eq!(foo.foo(Entity {}).await, Entity {}); | ||
assert_eq!(bar.bar(Entity {}, Entity {}).await, Ok(Entity {})); | ||
assert_eq!(baz.baz(Entity {}).await, Entity {}); | ||
}); | ||
} |