Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Rust compiler doesn't consider Box<FileRotate<_>> as valid a implementation for Box<(dyn std::io::Write + Send + 'static)?

I am attempting to combine a rotating log with fern, by chaining a boxed FileRotate instance (from file-rotate crate) with a fern Dispatch instance, but can’t seem to satisfy the compiler. Here is the code snippet:

let log= Box::new(
    FileRotate::new(
        "log/output.log",
        CountSuffix::new(2),
        ContentLimit::Lines(10),
        Compression::None));

fern::Dispatch::new()
    .level(LevelFilter::Debug)
    .chain(log)
    .apply()?;

The compiler disagrees, with the following error:

error[E0277]: the trait bound `fern::Output: From<Box<FileRotate<CountSuffix>>>` is not satisfied
   --> src/main.rs:101:16
    |
101 |         .chain(log)
    |          ----- ^^^ the trait `From<Box<FileRotate<CountSuffix>>>` is not implemented for `fern::Output`
    |          |
    |          required by a bound introduced by this call
    |
    = help: the following implementations were found:
              <fern::Output as From<&'static (dyn log::Log + 'static)>>
              <fern::Output as From<Box<(dyn log::Log + 'static)>>>
              <fern::Output as From<Box<(dyn std::io::Write + Send + 'static)>>>
              <fern::Output as From<Dispatch>>
            and 6 others
    = note: required because of the requirements on the impl of `Into<fern::Output>` for `Box<FileRotate<CountSuffix>>`
note: required by a bound in `Dispatch::chain`
   --> /Users/l203663/.cargo/registry/src/github.com-1ecc6299db9ec823/fern-0.6.0/src/builders.rs:195:21
    |
195 |     pub fn chain<T: Into<Output>>(mut self, logger: T) -> Self {
    |                     ^^^^^^^^^^^^ required by this bound in `Dispatch::chain`

However, the FileRotate struct implements both Write and Send, as documented here and here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The closest implementation proposed by the compiler is IMO:

From<Box<(dyn std::io::Write + Send + 'static)>
  • Is the issue caused by the 'static lifetime? In which case, how could I solve this?
  • If not, what could possibly cause the issue?

Crates in use:

log = "0.4.14"
fern = "0.6.0"
file-rotate = "0.5.3" 

>Solution :

I am not certain that this is all of the problem (not having used fern) but since the input parameter to chain() is generic, it won’t automatically coerce your Box<FileRotate> to a Box<dyn Write + Send>, so you have to do that explicitly:

.chain(log as Box<dyn Write + Send>)

You do not need to specify 'static because that is the default for all Box<dyn ...> (but not &dyn ...).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading