Skip to content

Conversation

SpriteOvO
Copy link
Owner

@SpriteOvO SpriteOvO commented Apr 8, 2025

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.


EDIT: Mainly for error stuff.

@SpriteOvO SpriteOvO added this to the v0.5.0 milestone Apr 8, 2025
@SpriteOvO SpriteOvO force-pushed the improve-for-downstream branch from 1d5cf9a to 962f521 Compare April 12, 2025 16:01
@SpriteOvO
Copy link
Owner Author

SpriteOvO commented Apr 12, 2025

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 SpriteOvO force-pushed the main-dev branch 4 times, most recently from 2067176 to 69da3ed Compare September 9, 2025 04:39
@SpriteOvO SpriteOvO force-pushed the improve-for-downstream branch from 7e2f43d to 2145cc6 Compare September 18, 2025 10:54
@SpriteOvO SpriteOvO changed the title Change some APIs for downstream crate implementors Improve Error API for downstream crate implementors Sep 18, 2025
@SpriteOvO
Copy link
Owner Author

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 SpriteOvO changed the title Improve Error API for downstream crate implementors Improve error API for downstream crate implementors Sep 18, 2025
@SpriteOvO SpriteOvO marked this pull request as ready for review September 18, 2025 10:59
@SpriteOvO SpriteOvO force-pushed the improve-for-downstream branch from 2145cc6 to ed1ac88 Compare September 18, 2025 11:03
@SpriteOvO SpriteOvO force-pushed the improve-for-downstream branch from ed1ac88 to 8770d9b Compare September 18, 2025 16:45
@SpriteOvO SpriteOvO merged commit 59c1fb1 into main-dev Sep 18, 2025
84 checks passed
@SpriteOvO SpriteOvO deleted the improve-for-downstream branch September 18, 2025 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant