Impl `fmt::Write` for `io::Write`?

Is there a reason the standard library doesn't implement fmt::Write for io::Write? I was struggling to figure out how to treat Stdout as a fmt::Write until I saw this comment about using a wrapper struct.

If there's a good reason the standard library doesn't implement fmt::Write for io::Write, perhaps the standard library should at least provide a wrapper struct for this purpose?

It's backwards incompatible to add blanket definitions - someone might have already implemented both traits for their own types.

Why there was no initial blanket impl, and whether it was accidental or a deliberate separation of concerns / enabling of flexibility, I couldn't say offhand.

There is a problem with implementing both, because of how write! works. write! just duck types between io::Write and fmt::Write by calling the methods shared by both. If you implement both traits, then write! would be ambiguous when both are in scope.

1 Like

Okay, those answers both make sense. Is there any good reason not to provide a wrapper type in the standard library for converting an io::Write into a fmt::Write?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.