Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Log for Box<Log> #414

Merged
merged 1 commit into from
Sep 21, 2020
Merged

Conversation

lionel-faber
Copy link
Contributor

Hi there! I was trying to wrap a Box<dyn Log> returned from flexi logger into async_log but I was unable to do so since the Log trait was not implemented for Box<dyn Log>.

One option was to create a wrapper struct:

struct LoggerWrapper(Box<dyn Log>);

impl Log for LoggerWrapper {
    fn enabled(&self, metadata: &Metadata) -> bool {
        self.0.enabled(metadata)
    }

    fn log(&self, record: &Record) {
        self.0.log(record)
    }

    fn flush(&self) {
        self.0.flush();
    }
}

It was suggested to me that it might be beneficial to have the implementation in the log crate so I've added the implementation in this pull request.

Would you accept this change? I will be happy to make any changes or modifications if necessary.

src/lib.rs Outdated
@@ -1167,6 +1167,20 @@ impl Log for NopLogger {
fn flush(&self) {}
}

#[cfg(feature = "std")]
impl Log for std::boxed::Box<dyn Log> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made more general to impl<T> Log for Box<T> where T: ?Sized + Log { ... }

@sfackler sfackler merged commit 7396e38 into rust-lang:master Sep 21, 2020
@lionel-faber lionel-faber deleted the boxed-impl branch September 22, 2020 07:25
EFanZh pushed a commit to EFanZh/log that referenced this pull request Jul 23, 2023
(cargo-release) version 0.2.1

Co-authored-by: github-actions <[email protected]>
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.

2 participants