Twitter | Pretraživanje | |
Patrik Svensson
Does someone better than me at (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...
Reply Retweet Označi sa "sviđa mi se" More
Patrik Svensson 31. sij
Odgovor korisniku/ci @rustlang @Dzamlo
Crisis averted! Managed to write a simple macro after some help and inspiration from that does what I want it to. Thanks for all the suggestions and help!
Reply Retweet Označi sa "sviđa mi se"
Loïc Damien 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
One option to move the redundant code to a generic trait implemented on the Enum: But this doesn't help much. Another option is to use a macro: I'm not sure this a better way.
Reply Retweet Označi sa "sviđa mi se"
Patrik Svensson 31. sij
Odgovor korisniku/ci @Dzamlo @rustlang
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:
Reply Retweet Označi sa "sviđa mi se"
Taalaran 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
Maybe a marker trait for these struct plus an generic function ?
Reply Retweet Označi sa "sviđa mi se"
Patrik Svensson 31. sij
Odgovor korisniku/ci @taalaran @rustlang
Problem is that "CollectorConfiguration" is an enum. Not sure how I can make this generic
Reply Retweet Označi sa "sviđa mi se"
Peter Ekerot 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
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?
Reply Retweet Označi sa "sviđa mi se"
Patrik Svensson 31. sij
Odgovor korisniku/ci @ekerot @rustlang
That’s bot possible. I need to match on the enums to access the configs
Reply Retweet Označi sa "sviđa mi se"
໓ฯlคຖ ໓p¢ 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
You could make it generic over the type of config? Though for a test it might be overkill
Reply Retweet Označi sa "sviđa mi se"
Thom Chiovoloni 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
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...
Reply Retweet Označi sa "sviđa mi se"
Thom Chiovoloni 31. sij
Odgovor korisniku/ci @firstdrafthell @rustlang
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| _azure()).expect("unable to find ...")`. Might be better.
Reply Retweet Označi sa "sviđa mi se"