about summary refs log tree commit diff
path: root/library/std/src/path.rs
AgeCommit message (Collapse)AuthorLines
2023-01-08Rollup merge of #103104 - SUPERCILEX:sep-ref, r=dtolnayYuki Okushi-1/+1
Stabilize `main_separator_str` See reasoning here: https://github.com/rust-lang/rust/issues/94071#issuecomment-1279872605. Closes #94071.
2023-01-02Rollup merge of #104298 - tbu-:pr_set_extension_caveats, r=m-ou-seMichael Goulet-3/+30
Add notes and examples about non-intuitive `PathBuf::set_extension` behavior Basically, passing the empty string will actually remove the extension instead of setting it to the empty string. This might change what is considered to be an extension. Additionally, passing an extension that contains dots will make the path only consider the last part of it to be the new extension.
2022-12-31Rollup merge of #106280 - Ezrashaw:path-join-docs-better, r=thomccMatthias Krüger-0/+3
docs: add link to `Path::join` in `PathBuf::push` Fixes #106219 Hopefully my wording is alright.
2022-12-31Add notes and examples about non-intuitive `PathBuf::set_extension` behaviorTobias Bucher-3/+30
Basically, passing the empty string will actually remove the extension instead of setting it to the empty string. This might change what is considered to be an extension. Additionally, passing an extension that contains dots will make the path only consider the last part of it to be the new extension.
2022-12-30Replace libstd, libcore, liballoc in line comments.jonathanCogan-1/+1
2022-12-30docs: add link to `Path::join` in `PathBuf::push`Ezra Shaw-0/+3
2022-12-16Realistic `Path::as_mut_os_str` doctestAndres Suarez-3/+3
2022-12-16Auto merge of #105018 - zertosh:path_buf_deref_mut, r=dtolnaybors-0/+14
Implement DerefMut for PathBuf Without this, there's no way to get a `&mut Path` from `PathBuf` without going through `into_boxed_path`. This is relevant now that #105002 adds `PathBuf::as_mut_os_string` and `Path::as_mut_os_str`.
2022-11-28Add `as_mut_os_string` to `&mut PathBuf` and `as_mut_os_str` to `&mut Path`Andres Suarez-0/+46
Implements rust-lang/libs-team#140
2022-11-28Implement DerefMut for PathBufAndres Suarez-0/+14
2022-11-11Document `Path::parent` behavior around relative pathsTobias Bucher-1/+12
A relative path with just one component will return `Some("")` as its parent, which wasn't clear to me from the documentation. The parent of `""` is `None`, which was missing from the documentation as well.
2022-10-15Stabilize `main_separator_str`Alex Saveau-1/+1
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-10-09Add basename and dirname aliasesAriel Davis-0/+2
2022-09-20Clarify Path::extension() semantics in docs abstractMahmoud Al-Qudsi-1/+1
State up-front and center what shape the returned extension will have, without making the user read through the description and examples. Rationale: Various frameworks and libraries for different platforms have their different conventions as to whether an "extension" is ".ext" or just "ext" and anyone that's had to deal with this ambiguity in the past is always double- or triple-checking to make sure the function call returns an extension that matches the expected semantics. Offer the answer to this important question right off the bat instead of making them dig to find it.
2022-06-20Rollup merge of #97912 - Kixunil:stabilize_path_try_exists, r=dtolnayYuki Okushi-6/+11
Stabilize `Path::try_exists()` and improve doc This stabilizes the `Path::try_exists()` method which returns `Result<bool, io::Error>` instead of `bool` allowing handling of errors unrelated to the file not existing. (e.g permission errors) Along with the stabilization it also: * Warns that the `exists()` method is error-prone and suggests to use the newly stabilized one. * Suggests it instead of `metadata()` to handle errors. * Mentions TOCTOU bugs to avoid false assumption that `try_exists()` is completely safe fixed version of `exists()`. * Renames the feature of still-unstable `std::fs::try_exists()` to `fs_try_exists` to avoid name conflict. The tracking issue #83186 remains open to track `fs_try_exists`.
2022-06-16std: Stabilize feature try_reserve_2Xuanwo-2/+2
Signed-off-by: Xuanwo <github@xuanwo.io>
2022-06-14Stabilize `Path::try_exists()` and improve docMartin Habovstiak-6/+11
This stabilizes the `Path::try_exists()` method which returns `Result<bool, io::Error>` instead of `bool` allowing handling of errors unrelated to the file not existing. (e.g permission errors) Along with the stabilization it also: * Warns that the `exists()` method is error-prone and suggests to use the newly stabilized one. * Suggests it instead of `metadata()` to handle errors. * Mentions TOCTOU bugs to avoid false assumption that `try_exists()` is completely safe fixed version of `exists()`. * Renames the feature of still-unstable `std::fs::try_exists()` to `fs_try_exists` to avoid name conflict. The tracking issue #83186 remains open to track `fs_try_exists`.
2022-05-08fix panic in Path::strip_prefixname1e5s-1/+1
2022-04-17Remove unnecessary functiondylni-20/+18
2022-04-17Improve Windows path prefix parsingdylni-2/+2
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-2/+2
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-17Rollup merge of #93976 - SUPERCILEX:separator_str, r=yaahcMatthias Krüger-0/+6
Add MAIN_SEPARATOR_STR Currently, if someone needs access to the path separator as a str, they need to go through this mess: ```rust unsafe { std::str::from_utf8_unchecked(slice::from_ref(&(MAIN_SEPARATOR as u8))) } ``` This PR just re-exports an existing path separator str API.
2022-02-17Rollup merge of #89869 - kpreid:from-doc, r=yaahcMatthias Krüger-3/+3
Add documentation to more `From::from` implementations. For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * The new documentation for construction of maps and sets from arrays of keys mentions the handling of duplicates. Future work could be to do this for *all* code paths that convert an iterable to a map or set. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2022-02-16Add MAIN_SEPARATOR_STRAlex Saveau-0/+6
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-02-13Auto merge of #91673 - ChrisDenton:path-absolute, r=Mark-Simulacrumbors-1/+77
`std::path::absolute` Implements #59117 by adding a `std::path::absolute` function that creates an absolute path without reading the filesystem. This is intended to be a drop-in replacement for [`std::fs::canonicalize`](https://doc.rust-lang.org/std/fs/fn.canonicalize.html) in cases where it isn't necessary to resolve symlinks. It can be used on paths that don't exist or where resolving symlinks is unwanted. It can also be used to avoid circumstances where `canonicalize` might otherwise fail. On Windows this is a wrapper around [`GetFullPathNameW`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew). On Unix it partially implements the POSIX [pathname resolution](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13) specification, stopping just short of actually resolving symlinks.
2022-02-12Auto merge of #93697 - the8472:fix-windows-path-hash, r=Mark-Simulacrumbors-9/+17
Fix hashing for windows paths containing a CurDir component * the logic only checked for / but not for \ * verbatim paths shouldn't skip items at all since they don't get normalized * the extra branches get optimized out on unix since is_sep_byte is a trivial comparison and is_verbatim is always-false * tests lacked windows coverage for these cases That lead to equal paths not having equal hashes and to unnecessary collisions.
2022-02-08Fix `absolute` issuesChris Denton-2/+2
2022-02-08`std::path::absolute`Chris Denton-1/+77
2022-02-06Fix hashing for windows paths containing a CurDir componentThe 8472-9/+17
* the logic only checked for / but not for \ * verbatim paths shouldn't skip items at all since they don't get normalized * the extra branches get optimized out on unix since is_sep_byte is a trivial comparison and is_verbatim is always-false * tests lacked windows coverage for these cases That lead to equal paths not having equal hashes and to unnecessary collisions.
2022-02-01Link `try_exists` docs to `Path::exists`Chris Denton-1/+3
2022-01-24Rollup merge of #92513 - Xuanwo:path-buf, r=dtolnayMatthias Krüger-0/+19
std: Implement try_reserve and try_reserve_exact on PathBuf Part of https://github.com/rust-lang/rust/issues/91789 Signed-off-by: Xuanwo <github@xuanwo.io>
2022-01-20Correct docs in `Arc` and `Rc`.Jakob Degen-3/+5
A number of trait implementations incorrectly claimed to be zero cost.
2022-01-14Typos fixMaxwase-1/+1
2022-01-03std: Implement try_reserve and try_reserve_exact on PathBufXuanwo-0/+19
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-11Correct since attribute for featureMaxwase-1/+1
2021-12-04Add documentation to more `From::from` implementations.Kevin Reid-3/+3
For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2021-12-02Document file path case sensitivityChris Denton-0/+7
2021-11-11`Prefix` can be case-insensitive, delegate to its Hash impl instead of ↵The8472-0/+8
trying to hash the raw bytes This should have 0 performance overhead on unix since Prefix is always None.
2021-11-09remove redundant .iter() call since zip() takes an IntoIterator argumentThe8472-6/+5
2021-11-09add fast path on Path::eq for exact equalityThe8472-1/+20
2021-11-09optimize Hash for PathThe8472-2/+28
Hashing does not have to use the whole Components parsing machinery because we only need it to match the normalizations that Components does. * stripping redundant separators -> skipping separators * stripping redundant '.' directories -> skipping '.' following after a separator That's all it takes. And instead of hashing individual slices for each component we feed the bytes directly into the hasher which avoids hashing the length of each component in addition to its contents.
2021-10-31Rollup merge of #90431 - jkugelman:must-use-std-o-through-z, r=joshtriplettMatthias Krüger-0/+15
Add #[must_use] to remaining std functions (O-Z) I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from O-Z. `panicking::take_hook` has a side effect: it unregisters the current panic hook, returning it. I almost ignored it, but the documentation example shows `let _ = panic::take_hook();`, so following suit I went ahead and added a `#[must_use]`. ```rust std::panicking fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>; ``` I added these functions that clippy did not flag: ```rust std::path::Path fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool; std::path::Path fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool; std::path::Path fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf; std::path::Path fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf; ``` Parent issue: #89692 r? `@joshtriplett`
2021-10-31Rollup merge of #89677 - maxwase:is-symlink-stabilization, r=joshtriplettMatthias Krüger-3/+8
Stabilize `is_symlink()` for `Metadata` and `Path` I'm not fully sure about `since` version, correct me if I'm wrong Needs update after stabilization: [cargo-test-support](https://github.com/rust-lang/cargo/blob/8063672238a5b6c3a901c0fc17f3164692d0be85/crates/cargo-test-support/src/paths.rs#L202) Linked issue: #85748
2021-10-30Add #[must_use] to remaining std functions (O-Z)John Kugelman-0/+15
2021-10-14Ensure that pushing empty path works as beforeSean Young-1/+4
Fixes: https://github.com/rust-lang/rust/issues/89658
2021-10-13Rollup merge of #89794 - jkugelman:must-use-to_value-conversions, r=joshtriplettYuki Okushi-0/+6
Add #[must_use] to to_value conversions `NonNull<T>::cast` snuck in when I wasn't looking. What a scamp! Parent issue: #89692 r? ````@joshtriplett````
2021-10-13Merge branch 'master' into is-symlink-stabilizationMax Wase-0/+20
2021-10-12Rollup merge of #89797 - jkugelman:must-use-is_condition-tests, r=joshtriplettthe8472-0/+8
Add #[must_use] to is_condition tests I threw in `std::path::Path::has_root` for funsies. A continuation of #89718. Parent issue: #89692 r? ```@joshtriplett```
2021-10-12Rollup merge of #89796 - jkugelman:must-use-non-mutating-verb-methods, ↵the8472-0/+2
r=joshtriplett Add #[must_use] to non-mutating verb methods These are methods that could be misconstrued to mutate their input, similar to #89694. I gave each one a different custom message. I wrote that `upgrade` and `downgrade` don't modify the input pointers. Logically they don't, but technically they do... Parent issue: #89692 r? ```@joshtriplett```