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

ACP: Add OsStr::display (similar to Path::display) #326

Closed
riverbl opened this issue Jan 7, 2024 · 2 comments
Closed

ACP: Add OsStr::display (similar to Path::display) #326

riverbl opened this issue Jan 7, 2024 · 2 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@riverbl
Copy link

riverbl commented Jan 7, 2024

Proposal

Problem statement

Sometimes it is desirable to display an OsStr with invalid Unicode sequences replaced with . Currently, the most obvious way to do this is with OsStr::to_string_lossy, however this incurs an allocation and additional copy when the OsStr contains invalid Unicode. It would be useful to have an obvious and efficient way to do this.

Motivating examples or use cases

Lapce has a few cases of needing to display an OsStr, for example here:

let (svg, color) = config.file_svg(&path);
(
    svg,
    color,
    format!(
        "{} (Diff)",
        path.file_name()
            .unwrap_or_default()
            .to_string_lossy()
    ),
    is_pristine,
)

Solution sketch

Add a display method to OsStr, similar to the existing Path::display method. The method would return a type that references the OsStr and implements fmt::Display.

The implementation could reuse the logic currently used for Path::display. See the most recent commit on https://github.com/riverbl/rust/tree/os-str-display for an example implementation.

Alternatives

OsStr::to_string_lossy allows displaying an OsStr, but is less efficient.

OsStr implements AsRef<Path>, so Path::display could be used and would be just as efficient as this new method. The main issue I see with using Path::display is that it is confusing to anyone reading the code that an OsStr, the contents of which may not relate to a path, is being converted to a Path before being displayed.

A crate could provide this functionality, either using Path::display or a custom implementation on top of OsStr::as_encoded_bytes.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@riverbl riverbl added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jan 7, 2024
@tgross35
Copy link

tgross35 commented Jan 9, 2024

Path's display actually wraps the private OsStr::display which could probably be exposed pretty easily. Probably move Display to std::ffi and make std::path::Display a type alias for it.

CStr needs something like this too but will need a different implementation.

@Amanieu
Copy link
Member

Amanieu commented Jan 16, 2024

We discussed this in the libs-api meeting today. We're happy to accept this ACP, and discussed some of the finer API details:

  • The type returned by OsStr::display should be in std::ffi::os_str::Display (the os_str module should become public, OsStr should be re-exported into std::ffi from there).
  • std::path::Display should stay as a separate type, which wraps std::ffi::os_str::Display.

Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature.

@Amanieu Amanieu closed this as completed Jan 16, 2024
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jan 16, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 28, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 28, 2024
Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 28, 2024
Rollup merge of rust-lang#120051 - riverbl:os-str-display, r=m-ou-se

Add `display` method to `OsStr`

Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode.

Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`.

This change also makes the `std::ffi::os_str` module public (see rust-lang/libs-team#326 (comment)).

- ACP: rust-lang/libs-team#326
- Tracking issue: rust-lang#120048
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants