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

std::path::Path::Display allocates internally #38879

Closed
jethrogb opened this issue Jan 6, 2017 · 5 comments
Closed

std::path::Path::Display allocates internally #38879

jethrogb opened this issue Jan 6, 2017 · 5 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-help-wanted Call for participation: Help is requested to fix this issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jethrogb
Copy link
Contributor

jethrogb commented Jan 6, 2017

It should be possible to display a Path without doing any additional allocations, by having the lossy conversion use Formatter directly.

@brson brson added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. E-help-wanted Call for participation: Help is requested to fix this issue. labels Jan 11, 2017
@martinhath
Copy link
Contributor

I would like to try this out 😄

@martinhath
Copy link
Contributor

martinhath commented Jan 11, 2017

So I've done some digging. The allocation comes from path::Display::fmts self.path.to_string_lossy(), which eventually gets down to os_str::Slice::to_string_lossy, which calls String::from_utf8_lossy, yes? (and since String::from_utf8_lossy returns Cow<str>, the allocation is only done if the path contains invalid Unicode sequeces?)

However, I am a little confused by what you mean about "having the lossy conversion use Formatter directly" means. Do you suggest giving the Formatter the Path directly, and do the non-Unicode sequence conversion in the Formatter?

@jethrogb
Copy link
Contributor Author

jethrogb commented Jan 11, 2017

I think that's what I mean? You'll need to loop through the bytes and get valid chars. You can then write those chars using fmt::Write::write_char

@martinhath
Copy link
Contributor

Some more questions:

Or is there some hidden thing I haven't seen yet?

@jethrogb
Copy link
Contributor Author

jethrogb commented Jan 12, 2017

The to_string_lossy documentation is specifically talking about non-unicode byte sequences in the “Unicode Transformation Format (8 bits)” (UTF-8). So yes, that module would be useful. And I think it's fine to use it in std internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-help-wanted Call for participation: Help is requested to fix this issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants