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

0.9 changes prevent to use a std::fs::File directly with to_writer and to_writer_pretty #561

Closed
Ummon opened this issue Mar 18, 2025 · 2 comments · Fixed by #562
Closed

Comments

@Ummon
Copy link

Ummon commented Mar 18, 2025

As to_writer and to_writer_pretty need a std::fmt::Write as writer it's not possible to give them a std::fs::File directly.

It was possible in 0.8 to do that:

let file = File::create("file.ron").unwrap();
to_writer(file, &my_ron);

But it's not possible anymore.

Is there an easy way to write directly to a file? If yes it should be great to have an example in /examples/ as the reader example /examples/decode_file.rs.

@juntyr
Copy link
Member

juntyr commented Mar 18, 2025

Thank you for raising this issue with the updated API. The decision to move to fmt::Writer was taken because it provides stricter guarantees to users of RON, but I missed your case where you don’t need those guarantees.

At the moment there is no direct way to serialise to io::Write. Whatever we do next, there is the https://crates.io/crates/io-adapters crate that implements this conversion.

We could add additional methods that take io::Write (e.g. to_io_writer) and use an adapter internally. Would that work for you?

@juntyr
Copy link
Member

juntyr commented Mar 18, 2025

I’m thinking that one fun way to write the adapter internally would be to create a Display impl that fmt::Write’s to the fmt::Formatter and to then pass that Display impl into io::Write::write_fmt

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

Successfully merging a pull request may close this issue.

2 participants