Feature Request: Add a (conditional) trait bound for Any to allow downcasting Log implementations #399

Closed
opened 2020-06-04 05:49:19 -05:00 by LunarLambda · 2 comments
LunarLambda commented 2020-06-04 05:49:19 -05:00 (Migrated from github.com)

I need logging in the early stage of my program, before it can produce a proper configuration.

This means I wanted to add a set_config function to my logging implementation (which is its own crate), that would replace the configuration stored behind a Mutex<RefCell<Config>> in my logger.

This would require downcasting the &'static dyn Log obtained from the logger function, which is not possible without adding a trait bound for Any.

Any is implemented for all types that ascribe to 'static, which is all types that log currently accepts, that is &'static dyn Log and Box<dyn Log>.

Therefore, this looks like a completely backwards compatible change (especially if feature-gated, either behind std or its own any feature that requires std), and should be future proof too, as I don't think there's any way for log to support non-'static types.

The only major issue would be that there is no easy way to have a conditional trait bound, short of either duplicating the trait definition with cfg_if, or using a custom macro.

I need logging in the early stage of my program, before it can produce a proper configuration. This means I wanted to add a `set_config` function to my logging implementation (which is its own crate), that would replace the configuration stored behind a `Mutex<RefCell<Config>>` in my logger. This would require downcasting the `&'static dyn Log` obtained from the `logger` function, which is not possible without adding a trait bound for `Any`. `Any` is implemented for [all types that ascribe to `'static`][any], which is all types that `log` currently accepts, that is `&'static dyn Log` and `Box<dyn Log>`. Therefore, this looks like a completely backwards compatible change (especially if feature-gated, either behind `std` or its own `any` feature that requires `std`), and should be future proof too, as I don't think there's any way for log to support non-'static types. The only major issue would be that there is no easy way to have a conditional trait bound, short of either duplicating the trait definition with `cfg_if`, or using a custom macro. [any]: https://doc.rust-lang.org/stable/std/any/index.html
sfackler commented 2020-06-04 06:11:59 -05:00 (Migrated from github.com)

Downcasting the installed logger does not seem required for this. You could alternatively e.g. provide a handle type that can update the configuration.

Downcasting the installed logger does not seem required for this. You could alternatively e.g. provide a handle type that can update the configuration.
LunarLambda commented 2020-06-04 08:39:38 -05:00 (Migrated from github.com)

Ah! That would indeed work! Thank you.

Ah! That would indeed work! Thank you.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-rust-lang/log#399
No description provided.