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

Formatting without fmt::Debug #76

Merged
merged 4 commits into from
Dec 10, 2024

Conversation

kornelski
Copy link
Contributor

Fixes #75

@kornelski kornelski changed the title More efficient string formatting Formatting without fmt::Debug Dec 8, 2024
@lukaslueg
Copy link
Owner

lukaslueg commented Dec 8, 2024

Thanks for the PR. I guess this is in preparation of rust-lang/rust#129709?!

I'll revert CI back to @stable after this, and add an extra nightly-phase to test this unstable feature specifically (built seems to be used in embedded quite often, and those people do care about binary sizes, so fmt-debug should be supported by built early on).

This PR removes the use of PathBuf's Debug-representation, using Display instead, which the (stable) docs explicitly mention as being lossy; the docs also advertise using Debug. Are we sure .escape_default() on the string-representation is sufficient to do what PathBuf's Debug used to do?

@kornelski
Copy link
Contributor Author

That suggestion to use Debug is misleading, because Debug is also lossy or incorrect.

Debug has the same problem of not knowing the real encoding of paths on most Unix systems (where a single path could even contain multiple different incompatible encodings when it crosses file systems). You get some escape codes, but they're a combination of misinterpreting bytes as UTF-8 where possible and byte escape codes that are not allowed in unicode strings.

The format of Debug is unspecified and unstable:

Derived Debug formats are not stable, and so may change with future Rust versions. Additionally, Debug implementations of types provided by the standard library (std, core, alloc, etc.) are not stable, and may also change with future Rust versions.

No matter what you do, Rust's strings can't contain any non-UTF-8 encoding, so this is an unsolvable problem for as long as you're using &str. You'd have to generate platform-specific code that serializes and deserializes bytes of the path, and Debug print doesn't do that.

@lukaslueg lukaslueg merged commit e8b9e55 into lukaslueg:master Dec 10, 2024
7 checks passed
@kornelski kornelski deleted the fmt-improvements branch December 10, 2024 23:16
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 this pull request may close these issues.

Assumes specific Debug print output
2 participants