Skip to content

Idea: Duplicate code for std::io::Write and std::fmt::Write #401

@kaleidawave

Description

@kaleidawave

Currently generally emitting diagnostics is based on the internal GeneralWrite. This is core::fmt::Write when the std feature is missing and std::io::Write when feature std is enabled.

They have some similarities such as write_str and being passed to the first argument of writeln!. They have several differences: what types implement each trait, std::io::Write has a flush method and they have different error types in the write_str Result.

Before 0.13.0 this reuse of writeln! and the swapping of traits caused issues. For example String could be written to with --no-default-features but broke when a feature was added (#389). Since #400 this has been better handled through the addition of different functions for different writers. For example the emit_to_string function.

However, just there are still issues if the user does not pick a designated function.

A implementer of WriteStyle that wants to be usable with and without the std feature has to be aware of the differences. While possible (see example), it is not great.

I have considered a unified SuperWrite trait but have not been able to figure anything that works well with write! macros etc.

Another possible solution to remove this GeneralWrite tricks is

  • Add a feature like fmt-write
  • Contain all code in mod renderer_io
  • Create a macro that #[cfg(feature = "fmt-write")] include_mod_rewrite_io_to_fmt(mod renderer_io)...

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions