Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9273373

Browse files
authoredJun 25, 2024
Rollup merge of rust-lang#126885 - Borgerr:rm_internal_pathbuf_asmutvec, r=workingjubilee
Remove internal `PathBuf::as_mut_vec` closes rust-lang#126333
2 parents 46074aa + e6c45e4 commit 9273373

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed
 

‎std/src/ffi/os_str.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,20 @@ impl OsString {
552552
OsStr::from_inner_mut(self.inner.leak())
553553
}
554554

555-
/// Part of a hack to make PathBuf::push/pop more efficient.
555+
/// Provides plumbing to core `Vec::truncate`.
556+
/// More well behaving alternative to allowing outer types
557+
/// full mutable access to the core `Vec`.
556558
#[inline]
557-
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
558-
self.inner.as_mut_vec_for_path_buf()
559+
pub(crate) fn truncate(&mut self, len: usize) {
560+
self.inner.truncate(len);
561+
}
562+
563+
/// Provides plumbing to core `Vec::extend_from_slice`.
564+
/// More well behaving alternative to allowing outer types
565+
/// full mutable access to the core `Vec`.
566+
#[inline]
567+
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
568+
self.inner.extend_from_slice(other);
559569
}
560570
}
561571

‎std/src/path.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,6 @@ pub struct PathBuf {
11631163
}
11641164

11651165
impl PathBuf {
1166-
#[inline]
1167-
fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1168-
self.inner.as_mut_vec_for_path_buf()
1169-
}
1170-
11711166
/// Allocates an empty `PathBuf`.
11721167
///
11731168
/// # Examples
@@ -1290,7 +1285,8 @@ impl PathBuf {
12901285

12911286
fn _push(&mut self, path: &Path) {
12921287
// in general, a separator is needed if the rightmost byte is not a separator
1293-
let mut need_sep = self.as_mut_vec().last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
1288+
let buf = self.inner.as_encoded_bytes();
1289+
let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
12941290

12951291
// in the special case of `C:` on Windows, do *not* add a separator
12961292
let comps = self.components();
@@ -1304,7 +1300,7 @@ impl PathBuf {
13041300

13051301
// absolute `path` replaces `self`
13061302
if path.is_absolute() || path.prefix().is_some() {
1307-
self.as_mut_vec().truncate(0);
1303+
self.inner.truncate(0);
13081304

13091305
// verbatim paths need . and .. removed
13101306
} else if comps.prefix_verbatim() && !path.inner.is_empty() {
@@ -1349,7 +1345,7 @@ impl PathBuf {
13491345
// `path` has a root but no prefix, e.g., `\windows` (Windows only)
13501346
} else if path.has_root() {
13511347
let prefix_len = self.components().prefix_remaining();
1352-
self.as_mut_vec().truncate(prefix_len);
1348+
self.inner.truncate(prefix_len);
13531349

13541350
// `path` is a pure relative path
13551351
} else if need_sep {
@@ -1382,7 +1378,7 @@ impl PathBuf {
13821378
pub fn pop(&mut self) -> bool {
13831379
match self.parent().map(|p| p.as_u8_slice().len()) {
13841380
Some(len) => {
1385-
self.as_mut_vec().truncate(len);
1381+
self.inner.truncate(len);
13861382
true
13871383
}
13881384
None => false,
@@ -1510,15 +1506,14 @@ impl PathBuf {
15101506
// truncate until right after the file stem
15111507
let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
15121508
let start = self.inner.as_encoded_bytes().as_ptr().addr();
1513-
let v = self.as_mut_vec();
1514-
v.truncate(end_file_stem.wrapping_sub(start));
1509+
self.inner.truncate(end_file_stem.wrapping_sub(start));
15151510

15161511
// add the new extension, if any
1517-
let new = extension.as_encoded_bytes();
1512+
let new = extension;
15181513
if !new.is_empty() {
1519-
v.reserve_exact(new.len() + 1);
1520-
v.push(b'.');
1521-
v.extend_from_slice(new);
1514+
self.inner.reserve_exact(new.len() + 1);
1515+
self.inner.push(OsStr::new("."));
1516+
self.inner.push(new);
15221517
}
15231518

15241519
true
@@ -2645,18 +2640,18 @@ impl Path {
26452640
None => {
26462641
// Enough capacity for the extension and the dot
26472642
let capacity = self_len + extension.len() + 1;
2648-
let whole_path = self_bytes.iter();
2643+
let whole_path = self_bytes;
26492644
(capacity, whole_path)
26502645
}
26512646
Some(previous_extension) => {
26522647
let capacity = self_len + extension.len() - previous_extension.len();
2653-
let path_till_dot = self_bytes[..self_len - previous_extension.len()].iter();
2648+
let path_till_dot = &self_bytes[..self_len - previous_extension.len()];
26542649
(capacity, path_till_dot)
26552650
}
26562651
};
26572652

26582653
let mut new_path = PathBuf::with_capacity(new_capacity);
2659-
new_path.as_mut_vec().extend(slice_to_copy);
2654+
new_path.inner.extend_from_slice(slice_to_copy);
26602655
new_path.set_extension(extension);
26612656
new_path
26622657
}

‎std/src/sys/os_str/bytes.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,20 @@ impl Buf {
202202
self.as_slice().into_rc()
203203
}
204204

205-
/// Part of a hack to make PathBuf::push/pop more efficient.
205+
/// Provides plumbing to core `Vec::truncate`.
206+
/// More well behaving alternative to allowing outer types
207+
/// full mutable access to the core `Vec`.
206208
#[inline]
207-
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
208-
&mut self.inner
209+
pub(crate) fn truncate(&mut self, len: usize) {
210+
self.inner.truncate(len);
211+
}
212+
213+
/// Provides plumbing to core `Vec::extend_from_slice`.
214+
/// More well behaving alternative to allowing outer types
215+
/// full mutable access to the core `Vec`.
216+
#[inline]
217+
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
218+
self.inner.extend_from_slice(other);
209219
}
210220
}
211221

‎std/src/sys/os_str/wtf8.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,20 @@ impl Buf {
165165
self.as_slice().into_rc()
166166
}
167167

168-
/// Part of a hack to make PathBuf::push/pop more efficient.
168+
/// Provides plumbing to core `Vec::truncate`.
169+
/// More well behaving alternative to allowing outer types
170+
/// full mutable access to the core `Vec`.
169171
#[inline]
170-
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
171-
self.inner.as_mut_vec_for_path_buf()
172+
pub(crate) fn truncate(&mut self, len: usize) {
173+
self.inner.truncate(len);
174+
}
175+
176+
/// Provides plumbing to core `Vec::extend_from_slice`.
177+
/// More well behaving alternative to allowing outer types
178+
/// full mutable access to the core `Vec`.
179+
#[inline]
180+
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
181+
self.inner.extend_from_slice(other);
172182
}
173183
}
174184

‎std/src/sys_common/wtf8.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,13 @@ impl Wtf8Buf {
474474
Wtf8Buf { bytes: bytes.into_vec(), is_known_utf8: false }
475475
}
476476

477-
/// Part of a hack to make PathBuf::push/pop more efficient.
477+
/// Provides plumbing to core `Vec::extend_from_slice`.
478+
/// More well behaving alternative to allowing outer types
479+
/// full mutable access to the core `Vec`.
478480
#[inline]
479-
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
480-
// FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants
481-
// For now, simply assume that is about to happen.
482-
self.is_known_utf8 = false;
483-
&mut self.bytes
481+
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
482+
self.bytes.extend_from_slice(other);
483+
self.is_known_utf8 = self.is_known_utf8 || self.next_surrogate(0).is_none();
484484
}
485485
}
486486

0 commit comments

Comments
 (0)
Failed to load comments.