clitic / kdam

A console progress bar library for Rust. (inspired by tqdm & rich.progress)
https://docs.rs/kdam
Apache License 2.0
196 stars 6 forks source link

Why does update() return a Result in the latest update? #11

Closed ghost closed 1 year ago

ghost commented 1 year ago

Hello,

Thank you for your hard work on "kdam". It is my favourite progress crate. As proof that it is my favourite, I count 186 calls on update(1) across my various projects. But in the latest update, update() returns a Result, which it didn't before. (To make my confusion even worse, Result here is an alias of std::io::Result rather than std::result::Result. But that's another subject...) Because, by principle or by religion, I never call unwrap() and always handle Results manually through conditional trees, now I have to handle 186 possible errors. I wonder if there is a big reason for this change, and what would an Err Result even mean. Your docs say:

"Returns whether an update was triggered or not depending on constraints."

but this only explains an Ok Result, not an Err Result.

clitic commented 1 year ago

Thanks for using kdam and counting your 186 update calls. I forgot to write docs about, why some methods return std::io::Result type. Here is an explanation for that.

By default kdam uses std::io::Stderr handle for writing and printing things in terminal. Stderr handle implements std::io::Write trait and it's write method returns Err variant when writing to the handle fails. In previous versions this result type was not returned and unwrap was called instead.

In simple terms, Err variant (very rare) should only be returned when writing to handle fails.

I have done this to make kdam more panic proof. You can also see that println! macro also panics if writing to handle fails.

You can use Result<(), Box<dyn Error>> (maybe anyhow crate) on your functions and use ? handle error cases. Or you can call unwrap, it should not panic in most of the cases.

ghost commented 1 year ago

Thank you for your thorough answer. It makes perfect sense now. And thank you again for your unique and unmatched crate. Doesn’t Apoorv in Sanskrit mean unique and unmatched? Haha, anyway, back to my projects…