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

Add bool::is_false #534

Closed
joseph-gio opened this issue Feb 6, 2025 · 5 comments
Closed

Add bool::is_false #534

joseph-gio opened this issue Feb 6, 2025 · 5 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@joseph-gio
Copy link

Proposal

Problem statement

There should be a straightforward and readable way of checking of a boolean value is false. In some situations, using ! (not) is unergonomic or less readable than alternatives.

Motivating examples or use cases

Checking if a file does not exist.

std::fs::exists("some_path").is_ok_and(bool::is_false)

Solution sketch

impl bool {
    pub fn is_false(self) -> bool {
        !self
    }
}

Alternatives

Don't add this. You can use a closure or Not::not to achieve the same thing, but it is slightly less readable.

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.
@joseph-gio joseph-gio added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Feb 6, 2025
@scottmcm
Copy link
Member

scottmcm commented Feb 7, 2025

Checking if a file does not exist.

std::fs::exists("some_path").is_ok_and(bool::is_false)

If that's a good motivating example, shouldn't there be bool::is_true too for check that it does exist?

Though I think that really wants

std::fs::exists("some_path") is Ok(false)

à la rust-lang/rfcs#3573, rather than is_false.

(Which can be spelled with matches! today.)

@pitaj
Copy link

pitaj commented Feb 7, 2025

If this is already in a conditional, you can use if let as well

if let Ok(false) = std::fs::exists("some_path") {
   // ...
}

I think == Ok(false) would also work in this case.

@joseph-gio
Copy link
Author

I think == Ok(false) would also work in this case.

that requires the Error variant type to also implement PartialEq

@kennytm
Copy link
Member

kennytm commented Feb 7, 2025

Checking if a file does not exist.

std::fs::exists("some_path").is_ok_and(bool::is_false)

The motivation to ACP feels like there's an XY problem here. If you need to check if a file directory is accessible and the file does not exist, chances are you'll later put a new file or directory in that place, which you should just use File::create_new or fs::create_dir directly to prevent TOCTOU issue.

If it is more complicated than that I don't think std::fs::exists("some_path").is_ok_and(|exists| !exists) or matches!(std::fs::exists("some_path"), Ok(false)) is really the pain point compared with the rest of the logic.

@joshtriplett
Copy link
Member

We discussed this one in today's @rust-lang/libs-api meeting.

We had a strong consensus that the right way to write this was matches!, if let, ==, or a future is, and that if none of those suffice, a closure would be clearer than having this method.

@joshtriplett joshtriplett closed this as not planned Won't fix, can't repro, duplicate, stale Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

5 participants