1
1
//! Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
2
2
//!
3
- //! This provides the definition of `home_dir` used by Cargo and
4
- //! rustup, as well functions to find the correct value of
5
- //! `CARGO_HOME` and `RUSTUP_HOME`.
6
- //!
7
- //! See also the [`dirs`](https://docs.rs/dirs) crate.
8
- //!
9
- //! _Note that as of 2019/08/06 it appears that cargo uses this crate. And
10
- //! rustup has used this crate since 2019/08/21._
11
- //!
12
3
//! The definition of `home_dir` provided by the standard library is
13
4
//! incorrect because it considers the `HOME` environment variable on
14
5
//! Windows. This causes surprising situations where a Rust program
17
8
//! rustup use the standard libraries definition - they use the
18
9
//! definition here.
19
10
//!
20
- //! This crate further provides two functions, `cargo_home` and
11
+ //! This crate provides two additional functions, `cargo_home` and
21
12
//! `rustup_home`, which are the canonical way to determine the
22
- //! location that Cargo and rustup store their data.
13
+ //! location that Cargo and rustup use to store their data.
14
+ //! The `env` module contains utilities for mocking the process environment
15
+ //! by Cargo and rustup.
23
16
//!
24
17
//! See also this [discussion].
25
18
//!
19
+ //! See also the [`dirs`](https://docs.rs/dirs) crate.
20
+ //!
26
21
//! [discussion]: https://github.com/rust-lang/rust/pull/46799#issuecomment-361156935
27
22
28
23
#![ doc( html_root_url = "https://docs.rs/home/0.5.5" ) ]
@@ -36,29 +31,34 @@ mod windows;
36
31
use std:: io;
37
32
use std:: path:: { Path , PathBuf } ;
38
33
39
- /// Returns the path of the current user's home directory if known.
34
+ /// Returns the path of the current user's home directory using environment
35
+ /// variables or OS-specific APIs.
40
36
///
41
37
/// # Unix
42
38
///
43
39
/// Returns the value of the `HOME` environment variable if it is set
44
- /// and not equal to the empty string. Otherwise, it tries to determine the
45
- /// home directory by invoking the `getpwuid_r` function on the UID of the
46
- /// current user.
40
+ /// **even** if it is an empty string. Otherwise, it tries to determine the
41
+ /// home directory by invoking the [`getpwuid_r`][getpwuid] function with
42
+ /// the UID of the current user.
43
+ ///
44
+ /// [getpwuid]: https://linux.die.net/man/3/getpwuid_r
47
45
///
48
46
/// # Windows
49
47
///
50
- /// Returns the value of the `USERPROFILE` environment variable if it
51
- /// is set and not equal to the empty string. If both do not exist,
52
- /// [`SHGetFolderPathW`][msdn] is used to return the appropriate path.
48
+ /// Returns the value of the `USERPROFILE` environment variable if it is set
49
+ /// **and** it is not an empty string. Otherwise, it tries to determine the
50
+ /// home directory by invoking the [`SHGetFolderPathW`][shgfp] function with
51
+ /// [`CSIDL_PROFILE`][csidl].
53
52
///
54
- /// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
53
+ /// [shgfp]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
54
+ /// [csidl]: https://learn.microsoft.com/en-us/windows/win32/shell/csidl
55
55
///
56
56
/// # Examples
57
57
///
58
58
/// ```
59
59
/// match home::home_dir() {
60
- /// Some(path) => println!("{}", path.display()),
61
- /// None => println!("Impossible to get your home dir!"),
60
+ /// Some(path) if !path.as_os_str().is_empty() => println!("{}", path.display()),
61
+ /// _ => println!("Unable to get your home dir!"),
62
62
/// }
63
63
/// ```
64
64
pub fn home_dir ( ) -> Option < PathBuf > {
0 commit comments