spdlog-rs icon indicating copy to clipboard operation
spdlog-rs copied to clipboard

Change some APIs for downstream crate implementors

Open SpriteOvO opened this issue 7 months ago • 1 comments

Currently, I'm working on a separated crate spdlog-telegram which writes log records to a Telegram chat, and I found some API is hard (even impossible) to use from a downstream crate implementor in the ecosystem.

This PR improves some APIs to solve such problems (with API breaking changes, of course), aiming not just to make crate spdlog-telegram, but more downstream crates could be easily developed in the ecosystem.

I will update more details to explain each commit changes later.

SpriteOvO avatar Apr 08 '25 08:04 SpriteOvO

As a first thought, I was tried to omit the explicit ErrorHandler::new calling by accepting a generic type F where F has Into<fn> impl if user is passing a closure as a argument to {,set_}error_handler methods.

However, it doesn't work. It is surprised that there is no such an auto implementation:

fn foo(_: fn()) {}
fn bar<F: Into<fn()>>(_: F) {}

fn main() {
    foo(|| {}); // OK
    bar(|| {}); // Error
    bar::<fn()>(|| {}); // OK
}

I did ask about it on Zulip (link), it may be an opportunity to contribute to Rust stdlib.

SpriteOvO avatar Apr 12 '25 16:04 SpriteOvO

I have changed the ErrorHandler to store Arc<dyn Fn(Error) instead of fn(Error) so that we can avoid the inconvenience mentioned above.

Also, I've reverted the introduction of ErasableError and instead added a new error variant, Downstream(Box<dyn StdError>), specifically for downstream crate errors.

SpriteOvO avatar Sep 18 '25 10:09 SpriteOvO