Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
QuantumNinja
Mar 8, 2013

Trust me.
I pretend to be a ninja.
I'd write it more like:

code:
            Dest::Register(r) => self.v.get_mut(r)
                                       .and_then(|reg| { *reg = data as Register8 & 0xFF; Some(())} )
                                       .ok_or(Err(Chip8Error::OutOfBoundsAt(r))),
...
            Src::Register(r)  => self.v.get(r)
                                       .and_then(|reg| *reg as usize)
                                       .ok_or(Chip8Error::OutOfBoundsAt(r)),
That said, the Dest register thing seems a bit like a misuse of the Result type. Do you plan to catch and handle that Chip8Error? If not, use a panic!. If you do, a Result containing a unit on one side is an Option, and it would probably be nicer to just write update_reg(reg: Reg, index: usize) -> Option<Chip8Error> and call that.

QuantumNinja fucked around with this message at 06:13 on Jul 28, 2016

Adbot
ADBOT LOVES YOU

QuantumNinja
Mar 8, 2013

Trust me.
I pretend to be a ninja.

taqueso posted:

I don't want to panic inside my library for a minor error like this. I'm trying to pass the results all the way out to the API boundary so the calling app can see & handle the errors. I considered using Option here, but didn't use it for 2 reasons - I read somewhere that it was bad form to use None to indicate an error condition, and also so I don't have to convert the Option to a Result in the calling functions. This is pretty analogous to std's use of None when popping from a collection, so it is mostly for the second reason.

The library is pretty functional now, I'll try to post the code later today.

I agree that panic! is probably too much. But I wasn't advocating None as Error, I was advocating None as the unit result, where you only get something back if you have an error. For the sort of thing you seem to have here, though, I agree that following the Option conventions of std::Vec is probably a good best-practices guideline. :shrug:

Vanadium posted:

Result<(), T> is a legit pattern. :colbert:

You see it in std::fmt and the serialzied, and a few other places, but it's never carried very far in those APIs. taqueso's snippet sort of implies that it's going to carry a Result<(), T> through a few layers to "propagate the error", though, and that's going to induce a nasty ok_or chain all the way up the callstack.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply