Skip to content

Path has no Default #78858

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

Closed
Nicholas-Baron opened this issue Nov 7, 2020 · 7 comments
Closed

Path has no Default #78858

Nicholas-Baron opened this issue Nov 7, 2020 · 7 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Nicholas-Baron
Copy link
Contributor

Nicholas-Baron commented Nov 7, 2020

Ran into this doing a pull request.

The Path struct has no impl Default documented, but a value of Path::new("") seems to be a good default in my mind. (Update: it is not, I have been corrected)

This may be the incorrect way to ask for such a thing, but I would like to know why there is no impl Default for Path and whether there could be one in the near future.

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 7, 2020
@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2020

Error: Label libs-impl can only be set by Rust team members

Please let @rust-lang/release know if you're having trouble with this bot.

@jonas-schievink
Copy link
Contributor

What does an empty path mean? Does it refer to the current directory?

@Nicholas-Baron
Copy link
Contributor Author

@jonas-schievink Currently, the empty path refers to (at least on Linux) a nonexistent file or directory.
Code to test this

use std::path::Path;

fn main() {
    let path = Path::new("");

    println!("Empty string path");
    println!("{}", path.display());
    println!("Exists: {}", path.exists());
    match path.canonicalize() {
        Ok(p) => println!("{}", p.display()),
        Err(e) => println!("error {}", e),
    }
}

@jonas-schievink
Copy link
Contributor

Hmm, I'm not sure why it would be a good default then

@Nicholas-Baron
Copy link
Contributor Author

Nicholas-Baron commented Nov 7, 2020

So the question shifts from "is "" a good default" to "is there any good default for Path". (Second part of initial issue)
Even if there are multiple candidates, we can simply select the best of them and make that be the impl Default for Path.

@CryZe
Copy link
Contributor

CryZe commented Nov 7, 2020

If there's no clear default, then it probably should not have a Default impl at all, though maybe "." could work and be clear enough? Not sure.

@Nicholas-Baron
Copy link
Contributor Author

Nicholas-Baron commented Nov 7, 2020

This issue seems to have been just me exploring why there is no impl Default for Path.
In summary,

  1. "" is an invalid path.
  2. The current directory can be accessed differently (see std::env::current_dir()).
  3. The root directory is unsuitable (Windows does not have a concept of "root", usually protected on Unix).
  4. Any other directory would be arbitrary and should use Path::new

I thank everyone for their contributions and apologize for wasting time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. 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

5 participants