Rust tip: Avoid accidentally reinventing dynamic dispatch in your code. If you have code that looks like this:
match d {
A(a) => a.x(),
B(b) => b.x(),
C(c) => c.x(),
}
This is pretty much the same as having a trait X and then using the &X trait object. #rustlang /1
-
Show this thread
-
This code is essentially the same as dynamic dispatch because the enum really only serves to figure out which type's method to call. That's what trait objects already do for you. /2
1 reply 0 retweets 0 likesShow this thread -
Do be a bit careful of this advice though because if you switch to Box<X> then you will no longer be operating only on the stack and that might matter depending on what you are doing. /3
2 replies 0 retweets 0 likesShow this thread -
Replying to @Sunjay03
I think I literally just saw that pattern in crossbeam-channel
1 reply 0 retweets 1 like -
Replying to @mmastrac
Could be worth asking about! Like I said, there might be a reason for it, but it might also be an oversight. It's super easy to do this by accident (I almost did that today!)
1 reply 0 retweets 2 likes
The reason why crossbeam-channel uses pattern matching is performance. Dynamic dispatch is measurably slower. And the main downside of pattern matching is that it increases compilation times and bloats code. Tradeoffs 
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.