|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -//! Cross-platform file path handling (re-write) |
| 11 | +/*! |
| 12 | +
|
| 13 | +Cross-platform path support |
| 14 | +
|
| 15 | +This module implements support for two flavors of paths. `PosixPath` represents |
| 16 | +a path on any unix-like system, whereas `WindowsPath` represents a path on |
| 17 | +Windows. This module also exposes a typedef `Path` which is equal to the |
| 18 | +appropriate platform-specific path variant. |
| 19 | +
|
| 20 | +Both `PosixPath` and `WindowsPath` implement a trait `GenericPath`, which |
| 21 | +contains the set of methods that behave the same for both paths. They each also |
| 22 | +implement some methods that could not be expressed in `GenericPath`, yet behave |
| 23 | +identically for both path flavors, such as `::from_str()` or |
| 24 | +`.component_iter()`. |
| 25 | +
|
| 26 | +The three main design goals of this module are 1) to avoid unnecessary |
| 27 | +allocation, 2) to behave the same regardless of which flavor of path is being |
| 28 | +used, and 3) to support paths that cannot be represented in UTF-8 (as Linux has |
| 29 | +no restriction on paths beyond disallowing NUL). |
| 30 | +
|
| 31 | +## Usage |
| 32 | +
|
| 33 | +Usage of this module is fairly straightforward. Unless writing platform-specific |
| 34 | +code, `Path` should be used to refer to the platform-native path, and methods |
| 35 | +used should be restricted to those defined in `GenericPath`, and those methods |
| 36 | +that are declared identically on both `PosixPath` and `WindowsPath`. |
| 37 | +
|
| 38 | +Creation of a path is typically done with either `Path::from_str(some_str)` or |
| 39 | +`Path::from_vec(some_vec)`. This path can be modified with `.push()` and |
| 40 | +`.pop()` (and other setters). The resulting Path can either be passed to another |
| 41 | +API that expects a path, or can be turned into a &[u8] with `.as_vec()` or a |
| 42 | +Option<&str> with `.as_str()`. Similarly, attributes of the path can be queried |
| 43 | +with methods such as `.filename()`. There are also methods that return a new |
| 44 | +path instead of modifying the receiver, such as `.join()` or `.dir_path()`. |
| 45 | +
|
| 46 | +When rendering a path to some form of display, there is a method `.display()` |
| 47 | +which is compatible with the `format!()` parameter `{}`. This will render the |
| 48 | +path as a string, replacing all non-utf8 sequences with the Replacement |
| 49 | +Character (U+FFFD). As such it is not suitable for passing to any API that |
| 50 | +actually operates on the path; it is only intended for display. |
| 51 | +
|
| 52 | +## Example |
| 53 | +
|
| 54 | +```rust |
| 55 | +let mut path = Path::from_str("/tmp/path"); |
| 56 | +debug2!("path: {}", path.display()); |
| 57 | +path.set_filename_str("foo"); |
| 58 | +path.push_str("bar"); |
| 59 | +debug2!("new path: {}", path.display()); |
| 60 | +let b = std::os::path_exists(&path); |
| 61 | +debug2!("path exists: {}", b); |
| 62 | +``` |
| 63 | +
|
| 64 | +*/ |
12 | 65 |
|
13 | 66 | use container::Container;
|
14 | 67 | use c_str::CString;
|
|
0 commit comments