Skip to content
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

Allow obtaining &mut OsStr #70048

Merged
merged 6 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
21 changes: 21 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ impl ops::Index<ops::RangeFull> for OsString {
}
}

#[stable(feature = "mut_osstr", since = "1.44.0")]
impl ops::IndexMut<ops::RangeFull> for OsString {
#[inline]
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
OsStr::from_inner_mut(self.inner.as_mut_slice())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for OsString {
type Target = OsStr;
Expand All @@ -389,6 +397,14 @@ impl ops::Deref for OsString {
}
}

#[stable(feature = "mut_osstr", since = "1.44.0")]
impl ops::DerefMut for OsString {
#[inline]
fn deref_mut(&mut self) -> &mut OsStr {
&mut self[..]
}
}

#[stable(feature = "osstring_default", since = "1.9.0")]
impl Default for OsString {
/// Constructs an empty `OsString`.
Expand Down Expand Up @@ -512,6 +528,11 @@ impl OsStr {
unsafe { &*(inner as *const Slice as *const OsStr) }
}

#[inline]
fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
TyPR124 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
///
/// This conversion may entail doing a check for UTF-8 validity.
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ impl Buf {
unsafe { mem::transmute(self.inner.as_slice()) }
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut Slice {
unsafe { mem::transmute(self.inner.as_mut_slice()) }
}

pub fn into_string(self) -> Result<String, Buf> {
self.inner.into_string().map_err(|buf| Buf { inner: buf })
}
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/sys_common/os_str_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ impl Buf {
unsafe { mem::transmute(&*self.inner) }
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut Slice {
TyPR124 marked this conversation as resolved.
Show resolved Hide resolved
unsafe { mem::transmute(&mut *self.inner) }
}

pub fn into_string(self) -> Result<String, Buf> {
String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
}
Expand Down