Skip to content

Rollup of 11 pull requests #127400

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
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af9f6f7
out_of_scope_macro_calls: Detect calls inside attributes more precisely
petrochenkov Jun 26, 2024
9e12d91
Improve well known value check-cfg diagnostic for the standard library
Urgau Jul 1, 2024
57e76d4
impl PathBuf::add_extension and Path::with_added_extension
tisonkun Jul 4, 2024
0adb825
Improve dead code analysis
mu001999 Jul 4, 2024
4abc51a
Improve readability of some fmt code examples
GuillaumeGomez Jul 5, 2024
7dca61b
Use `ControlFlow` results for visitors that are only looking for a si…
oli-obk Jul 5, 2024
5fd5b65
Rename edition 2021 fail test
Jules-Bertholet Jul 5, 2024
291ed59
Added dots at the sentence ends of rustc AST doc
YohDeadfall Jul 5, 2024
5a35fc4
Match ergonomics 2024: `&` matches `&mut` on old editions
Jules-Bertholet Jul 5, 2024
76da7ae
Match ergonomics 2024: test type inference
Jules-Bertholet Jul 5, 2024
a0f4114
update comments
tisonkun Jul 5, 2024
3aa2abd
add unit tests for extra extension feature
tisonkun Jul 5, 2024
8e9a366
Use verbose style for argument removal suggestion
estebank Jul 5, 2024
27588d1
Split SolverDelegate back out from InferCtxtLike
compiler-errors Jul 5, 2024
9f66af4
Remove clubby789 from review rotation
clubby789 Jul 5, 2024
50aa50a
Rollup merge of #123600 - tisonkun:path_with_extension, r=dtolnay
workingjubilee Jul 5, 2024
03a9b01
Rollup merge of #126987 - petrochenkov:atvisord2, r=pnkfelix
workingjubilee Jul 5, 2024
968df26
Rollup merge of #127107 - mu001999-contrib:dead/enhance-2, r=pnkfelix
workingjubilee Jul 5, 2024
e9daf66
Rollup merge of #127221 - Urgau:check-cfg-std-diag, r=pnkfelix
workingjubilee Jul 5, 2024
a33d82e
Rollup merge of #127333 - compiler-errors:infer_ctxt_like-again, r=lcnr
workingjubilee Jul 5, 2024
2ecc6be
Rollup merge of #127363 - GuillaumeGomez:improve-fmt-code-readability…
workingjubilee Jul 5, 2024
13d015c
Rollup merge of #127366 - oli-obk:falliblevisitor, r=compiler-errors
workingjubilee Jul 5, 2024
34f4857
Rollup merge of #127368 - YohDeadfall:dots-in-docs, r=fmease
workingjubilee Jul 5, 2024
36b4d20
Rollup merge of #127369 - Jules-Bertholet:match-ergonomics-2021, r=Na…
workingjubilee Jul 5, 2024
cad0135
Rollup merge of #127383 - estebank:arg-removal, r=compiler-errors
workingjubilee Jul 5, 2024
97949a7
Rollup merge of #127393 - clubby789:unreview, r=workingjubilee
workingjubilee Jul 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
impl PathBuf::add_extension and Path::with_added_extension
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Jul 4, 2024
commit 57e76d45960770e45e20a76019c96643b7ef0845
97 changes: 97 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,77 @@ impl PathBuf {
true
}

/// Append [`self.extension`] with `extension`.
///
/// Returns `false` and does nothing if [`self.file_name`] is [`None`],
/// returns `true` and updates the extension otherwise.
///
/// If [`self.extension`] is [`None`], the extension is added; otherwise
/// it is appended.
///
/// # Caveats
///
/// The appended `extension` may contain dots and will be used in its entirety,
/// but only the part after the final dot will be reflected in
/// [`self.extension`].
///
/// See the examples below.
///
/// [`self.file_name`]: Path::file_name
/// [`self.extension`]: Path::extension
///
/// # Examples
///
/// ```
/// #![feature(path_add_extension)]
///
/// use std::path::{Path, PathBuf};
///
/// let mut p = PathBuf::from("/feel/the");
///
/// p.add_extension("formatted");
/// assert_eq!(Path::new("/feel/the.formatted"), p.as_path());
///
/// p.add_extension("dark.side");
/// assert_eq!(Path::new("/feel/the.formatted.dark.side"), p.as_path());
///
/// p.set_extension("cookie");
/// assert_eq!(Path::new("/feel/the.formatted.dark.cookie"), p.as_path());
///
/// p.set_extension("");
/// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
///
/// p.add_extension("");
/// assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
/// ```
#[unstable(feature = "path_add_extension", issue = "127292")]
pub fn add_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
self._add_extension(extension.as_ref())
}

fn _add_extension(&mut self, extension: &OsStr) -> bool {
let file_name = match self.file_name() {
None => return false,
Some(f) => f.as_encoded_bytes(),
};

let new = extension;
if !new.is_empty() {
// truncate until right after the file name
// this is necessary for trimming the trailing slash
let end_file_name = file_name[file_name.len()..].as_ptr().addr();
let start = self.inner.as_encoded_bytes().as_ptr().addr();
self.inner.truncate(end_file_name.wrapping_sub(start));

// append the new extension
self.inner.reserve_exact(new.len() + 1);
self.inner.push(OsStr::new("."));
self.inner.push(new);
}

true
}

/// Yields a mutable reference to the underlying [`OsString`] instance.
///
/// # Examples
Expand Down Expand Up @@ -2656,6 +2727,32 @@ impl Path {
new_path
}

/// Creates an owned [`PathBuf`] like `self` but with an extra extension.
///
/// See [`PathBuf::add_extension`] for more details.
///
/// # Examples
///
/// ```
/// #![feature(path_add_extension)]
///
/// use std::path::{Path, PathBuf};
///
/// let path = Path::new("foo.rs");
/// assert_eq!(path.with_added_extension("txt"), PathBuf::from("foo.rs.txt"));
///
/// let path = Path::new("foo.tar.gz");
/// assert_eq!(path.with_added_extension(""), PathBuf::from("foo.tar.gz"));
/// assert_eq!(path.with_added_extension("xz"), PathBuf::from("foo.tar.gz.xz"));
/// assert_eq!(path.with_added_extension("").with_added_extension("txt"), PathBuf::from("foo.tar.gz.txt"));
/// ```
#[unstable(feature = "path_add_extension", issue = "127292")]
pub fn with_added_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
let mut new_path = self.to_path_buf();
new_path.add_extension(extension);
new_path
}

/// Produces an iterator over the [`Component`]s of the path.
///
/// When parsing the path, there is a small amount of normalization:
Expand Down
Loading