1
Fork 0

PathBuf::as_mut_vec removed and verified for UEFI and Windows platforms #126333

This commit is contained in:
ash 2024-06-25 07:34:35 -06:00
parent 7e187e8e4b
commit aa46a3368e
5 changed files with 44 additions and 29 deletions

View file

@ -552,16 +552,21 @@ impl OsString {
OsStr::from_inner_mut(self.inner.leak()) OsStr::from_inner_mut(self.inner.leak())
} }
/// Part of a hack to make PathBuf::push/pop more efficient. /// Provides plumbing to core `Vec::truncate`.
#[inline] /// More well behaving alternative to allowing outer types
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> { /// full mutable access to the core `Vec`.
self.inner.as_mut_vec_for_path_buf()
}
#[inline] #[inline]
pub(crate) fn truncate(&mut self, len: usize) { pub(crate) fn truncate(&mut self, len: usize) {
self.inner.truncate(len); self.inner.truncate(len);
} }
/// Provides plumbing to core `Vec::extend_from_slice`.
/// More well behaving alternative to allowing outer types
/// full mutable access to the core `Vec`.
#[inline]
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
self.inner.extend_from_slice(other);
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View file

@ -1163,11 +1163,6 @@ pub struct PathBuf {
} }
impl PathBuf { impl PathBuf {
#[inline]
fn as_mut_vec(&mut self) -> &mut Vec<u8> {
self.inner.as_mut_vec_for_path_buf()
}
/// Allocates an empty `PathBuf`. /// Allocates an empty `PathBuf`.
/// ///
/// # Examples /// # Examples
@ -2645,18 +2640,18 @@ impl Path {
None => { None => {
// Enough capacity for the extension and the dot // Enough capacity for the extension and the dot
let capacity = self_len + extension.len() + 1; let capacity = self_len + extension.len() + 1;
let whole_path = self_bytes.iter(); let whole_path = self_bytes;
(capacity, whole_path) (capacity, whole_path)
} }
Some(previous_extension) => { Some(previous_extension) => {
let capacity = self_len + extension.len() - previous_extension.len(); let capacity = self_len + extension.len() - previous_extension.len();
let path_till_dot = self_bytes[..self_len - previous_extension.len()].iter(); let path_till_dot = &self_bytes[..self_len - previous_extension.len()];
(capacity, path_till_dot) (capacity, path_till_dot)
} }
}; };
let mut new_path = PathBuf::with_capacity(new_capacity); let mut new_path = PathBuf::with_capacity(new_capacity);
new_path.as_mut_vec().extend(slice_to_copy); new_path.inner.extend_from_slice(slice_to_copy);
new_path.set_extension(extension); new_path.set_extension(extension);
new_path new_path
} }

View file

@ -202,16 +202,21 @@ impl Buf {
self.as_slice().into_rc() self.as_slice().into_rc()
} }
/// Part of a hack to make PathBuf::push/pop more efficient. /// Provides plumbing to core `Vec::truncate`.
#[inline] /// More well behaving alternative to allowing outer types
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> { /// full mutable access to the core `Vec`.
&mut self.inner
}
#[inline] #[inline]
pub(crate) fn truncate(&mut self, len: usize) { pub(crate) fn truncate(&mut self, len: usize) {
self.inner.truncate(len); self.inner.truncate(len);
} }
/// Provides plumbing to core `Vec::extend_from_slice`.
/// More well behaving alternative to allowing outer types
/// full mutable access to the core `Vec`.
#[inline]
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
self.inner.extend_from_slice(other);
}
} }
impl Slice { impl Slice {

View file

@ -165,10 +165,20 @@ impl Buf {
self.as_slice().into_rc() self.as_slice().into_rc()
} }
/// Part of a hack to make PathBuf::push/pop more efficient. /// Provides plumbing to core `Vec::truncate`.
/// More well behaving alternative to allowing outer types
/// full mutable access to the core `Vec`.
#[inline] #[inline]
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> { pub(crate) fn truncate(&mut self, len: usize) {
self.inner.as_mut_vec_for_path_buf() self.inner.truncate(len);
}
/// Provides plumbing to core `Vec::extend_from_slice`.
/// More well behaving alternative to allowing outer types
/// full mutable access to the core `Vec`.
#[inline]
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
self.inner.extend_from_slice(other);
} }
} }

View file

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