Skip to content

ACP: Implement PartialEq<str> for Path, PathBuf and vice-versa #151

Closed
@Kixunil

Description

@Kixunil

Proposal

Problem statement

When testing functions that return PathBuf one might want to use assert_eq! with a string literal in tests. This is currently impossible because of lacking impls.

Motivation, use-cases

For my use case, the function in question processes program arguments assuming zeroth argument is the path to the program, so it returns PathBuf which should be the most appropriate. In the test I mock the arguments providing a different iterator with known zeroth argument. I wish to compare them to ensure the function returns the correct value.

I would be surprised if there weren't any other tested existing crates that do some path computation.

Solution sketches

impl PartialEq<str> for Path {
    fn eq(&self, other: &str) -> bool {
        let other: &Path = other.as_ref();
        self == other
    }
}

// other impls can call into the one above

I believe this is the obvious, idiomatic solution. I don't see any meaningful downsides.

Links and related work

N/A, will send a PR if this is approved.

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions