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

Writing to fmt::Formatter #1137

Open
asokol123 opened this issue May 26, 2024 · 0 comments
Open

Writing to fmt::Formatter #1137

asokol123 opened this issue May 26, 2024 · 0 comments

Comments

@asokol123
Copy link

asokol123 commented May 26, 2024

Is it possible to serialize a value directly to fmt::Formatter?
Usecase:

impl<T: Debug + serde::Serialize> Debug for Formattable<T> {
    fn fmt(&self, mut f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.format_type {
            FormatType::Json => serde_json::to_writer(f, &self.data).map_err(|e| std::fmt::Error),
            FormatType::Plain => write!(f, "{:?}", self.data),
        }
    }
}

This doesn't work because to_writer requires std::io::Write, which we can't get from std::fmt::Formatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant