|
@firstdrafthell | |||||
|
Does someone better than me at @rustlang (probably everyone) know how I can make this test utility code a little smarter? Really don't like the duplication and my gut feeling says that there must be some better way of doing this... pic.twitter.com/vkwoOfu0OP
|
||||||
|
||||||
|
Patrik Svensson
@firstdrafthell
|
31. sij |
|
Crisis averted! Managed to write a simple macro after some help and inspiration from @dzamlo that does what I want it to. Thanks for all the suggestions and help! pic.twitter.com/vFe3aluM0a
|
||
|
|
||
|
Loïc Damien
@Dzamlo
|
31. sij |
|
One option to move the redundant code to a generic trait implemented on the Enum: play.rust-lang.org/?version=stabl…
But this doesn't help much. Another option is to use a macro: play.rust-lang.org/?version=stabl…
I'm not sure this a better way.
|
||
|
|
||
|
Patrik Svensson
@firstdrafthell
|
31. sij |
|
Thanks for your help! I haven't written any macros before so your example was of great help (and inspiration). After some thought I managed to get something working: pic.twitter.com/5DDsh0bsyQ
|
||
|
|
||
|
Taalaran
@taalaran
|
31. sij |
|
Maybe a marker trait for these struct plus an generic function ?
|
||
|
|
||
|
Patrik Svensson
@firstdrafthell
|
31. sij |
|
Problem is that "CollectorConfiguration" is an enum. Not sure how I can make this generic
|
||
|
|
||
|
Peter Ekerot
@ekerot
|
31. sij |
|
I have never ever used rust but looking at it I wonder if you could not switch it completely backwards and iterate over the configurations instead of the enum?
|
||
|
|
||
|
Patrik Svensson
@firstdrafthell
|
31. sij |
|
That’s bot possible. I need to match on the enums to access the configs
|
||
|
|
||
|
໓ฯlคຖ ໓p¢
@DPC_22
|
31. sij |
|
You could make it generic over the type of config? Though for a test it might be overkill
|
||
|
|
||
|
Thom Chiovoloni
@thomcsc
|
31. sij |
|
i don't think there's a way. i've wished there were a good solution for going from `pattern that binds a value` => `option containing that value` (for any enum/pattern). I can't think of what that would look like though, or I'd consider RFCing it...
|
||
|
|
||
|
Thom Chiovoloni
@thomcsc
|
31. sij |
|
Ah, that said, if you write function like `CollectorConfiguration::as_azure(&self) -> Option<&AzureDevopsConfiguration>` then these could be shaped like `self.iter().find_map(|c| c.as_azure()).expect("unable to find ...")`. Might be better.
|
||
|
|
||