hiirc
hiirc copied to clipboard
Use traits instead of structs in listener
Hi! Thanks for the library.
I'm trying to use it on a project, but I'm having a really hard time writing unit tests for my listener implementation because I need an Irc struct, a Channel struct, etc. that are almost impossible to create for unit testing.
Is it possible for the Listener to receive traits instead of structs, so I can create my own mock implementations?
Thanks again!
Hey. This is an interesting idea. Here are my thoughts about this.
Rust does not support polymorphism, only generics. This means that the Listener
trait must take all of its parameters as generics.
Also, Irc, Channel and ChannelUser
have methods that would return those traits. Currently, the story of returning traits from a method isn't too great. We would have to Box
the traits, and that would generate a lot of allocations. There is a RFC proposing a way to return traits that would make our life way easier. It has not been accepted yet.
A good side effect of this approach is that we could hide away the ugly Arc
s everywhere.
I'm open to the idea. I think the library needs to re-designing. I'll wait to hear your thoughts.
For my unit test purpose, I do not need all methods from the current struct
to be present in the trait
s... only identification ones (Irc
can be empty, Channel
have name
and ChannelUser
nickname
).
I don't think boxing the traits is needed for the method call, but I'm not sure.
About the overall design, I found the API quite intuitive 👍.
Boxing is required when you want to return something as a trait, because their size is unknown. Which methods do you want to test?
No listener method returns anything... right?
For mock, I don't need any method on Irc
(it can be an empty trait), Channel
only needs to have name
and ChannelUser
only needs nickname
. I think that's the minimum number of methods that would allow me basic testing.
I've tried a couple designs and none worked. This is a place where polymorphism would make it much easier. I couldn't get it to work with generics. If you have ideas, please let me know.