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

Awkwardness with std::fmt::Display? #25

Open
BartMassey opened this issue Sep 7, 2019 · 2 comments
Open

Awkwardness with std::fmt::Display? #25

BartMassey opened this issue Sep 7, 2019 · 2 comments

Comments

@BartMassey
Copy link

BartMassey commented Sep 7, 2019

I'm trying to use tabwriter inside an impl of std::fmt::Display, and it seems really complex and inefficient. I'm currently doing it like this. Any suggestions on how to clean things up? Ideally I'd like to just write the table directly to the Formatter.

@BurntSushi
Copy link
Owner

Regrettably, I don't see a clean answer here with the current API. The fundamental problem is that a TabWriter is coupled to io::Write---which supports arbitrary bytes---where as the Display infrastructure is a "purer" world that only supports UTF-8.

I guess the ideal API here would permit one to create a TabWriter that wraps a std::fmt::Write instead of a std::io::Write. Then I think you'd just need to do let f = TabWriter::new(f); and then things would be pretty natural from there.

The problem is that TabWriter as it exists today is wedded to std::io::Write.

Probably the right way to do this is to create a TabWriterBuilder that has from_io and from_fmt constructors. They would both produce a TabWriter<W>, but the former would have W: io::Write where as the latter would have W: fmt::Write. The TabWriter would then implement io::Write and fmt::Write itself as appropriate.

Unfortunately, that feels like a "tabwriter 2" kind of thing to me. I personally doubt I'll do that unless I run into the same use case as you. :-)

@BartMassey
Copy link
Author

Thanks much for looking at this, and for the clear explanation. Mostly just wanted to make sure I wasn't missing something obvious.

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

No branches or pull requests

2 participants