nrxus / faux

Struct mocking library for Rust
https://nrxus.github.io/faux/
MIT License
411 stars 14 forks source link

Multiple impls within a module #13

Closed nrxus closed 1 year ago

nrxus commented 4 years ago

#[faux::methods]creates a module with the name of the struct being mocked.

This means that if there are multiple impl blocks for the same type, two modules with the same name would be created in the same scope, hence collide, and fail to compile.

https://github.com/rust-lang/rust/issues/8995 would lift the requirement of making the dummy modules and fix this but I do not see an ETA in it being done soon.

Multiple impls within the same module is rare since we do not support generics nor trait impls but once we start supporting them this issue would become more important.

Multiple impls are allowed as long as there is no more than one inherent implementation and no more than one trait implementation per trait.

The user can get around this by wrapping impls in a separate module themselves, but forcing the user to write hacks to support this library would be very subpar UX.

A possible solution would be to mock an entire module but that is a bit far from being implemented,

nrxus commented 4 years ago

Generics are now supported thanks to https://github.com/nrxus/faux/commit/9f1106f3e24d26734858f40fbd221df62fdea1ff

nrxus commented 4 years ago

As part of solving #10 a decision was made to also include trait names in the name of the load bearing mod that causes the problem in this issue. This means that an user CAN have multiple impl blocks in a mod as long as no more than one is an inherent implementation impl MyStruct {}, and the rest are all for different traits: impl TraitA for MyStruct {} impl TraitB for MyStruct {} works.

nrxus commented 1 year ago

Fixed in: https://github.com/nrxus/faux/commit/f694453a097d9efde81943309813542877d1b2ee