which, I hope for obvious reasons, I don't really like
Is there a reasonable syntax for this? Or some kind of chainable set of methods?
My solution will be avoid the closure completely by using a chain of filters and maps, but it would still be nice to know if there is a better option for the above closure for the future ...
I'm not aware of a general way to improve on this sort of filter_map — in particular, there is no language feature which directly turns a pattern match success or failure into Option — but the direction I would look at is whether there are any methods you can add to TocEntry that would make this work better and also better-documented.
For example, what does matching that the entry is a FirstTrackInfo actually do? Does it mean "this is the first track rather than some other track"? If so, then how about adding an is_first() method? Then you could write:
Hmm ... I guess my original statement should have been "for reasons which I can't quite put my finger on, but feel should be obvious". It took me a moment to really think about the answer to this:
Yeah, the formatting is discordant, but that get a touch better when outside a closure and feels more like a warning smell than a problem to me
The mental switch from function chaining (iterator adaptors) to blocks is more discordant. Again, that is context-related, and I have a function-chained alternative for this which I've used
I think the main issue is that "if then some(thing_from_the_if_clause) else none" just feels like a verbose version of something that has a very obvious default else.
Thinking overnight, what I was looking for was a version of this (on nightly) which captures from within the matches! and avoids the and_then.
Thanks for the tip. I managed to further simplify the final version to
/// Parse the entry detailing the track number of the first audio track, if present
fn find_first_audio_info(self) -> Option<TrackNo> {
self.filter(|entry| matches!(entry, TocEntry::FirstTrackInfo { .. }))
.filter(|entry| entry.control().is_audio())
.find_map(TocEntry::track)
}
For a bit of background
It's in a crate dedicated to handling audio CDs.
The very start of a CD is dedicated to a Table of Contents, with data blocks which are effectively a bag-of-bytes with "well defined fields" that are then re-used for various other things depending on the context and the values of other fields (of course!, because real space constraint, 1980's design, ... Technically it's not a wire-protocol but effectively it works like one)
So in this case, for one response format, the usual data format is as per the first row below, but there is also an entry as defined in the second row based on a sentinal "track number"
AdrCtl
TNO
Point
Min
Sec
Frame
ZERO
PMin
Psec
PFrame
bitflags incl Data/Audio
0
TrackNumber
0
0
0
0
Start Mins
Start Secs
Start Frame
bitflags incl Data/Audio
0
0xA0
0
0
0
0
First Track No
0
0
That felt like something worth abstracting away into an enum, hence the FirstTrackInfo variant, if I'm going to the trouble of creating a nice library.
There are a few of them on crates.io, search for "extract macro". There was a proposal to add one to std (I personally would find it useful) but it wasn’t accepted.
Using these types, you can (try) convert a more general object to a specific one. Then add some From impls for SomeErrorType and you can use the question-mark-operator, flat_map and so on very easy. This gets rid of many if/else constructs.
Yeah. The latest changes I made have provided named types for all variants rather than using anonymous structs for the less-common ones.
It took me a moment to consider why I'd preferred getter methods to (Try)From and then noticed that API Guidelines - C-GETTER explains this perfectly, assuming you consider C-CONV-TRAITS equivalent to C-CONV.
The getters work great in matches!().then_some(this).map(THIS::getter) just as From would.