Skip to content

Commit ef2957d

Browse files
committed
allowing getting &mut OsStr from OsString
1 parent 8e6de32 commit ef2957d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/libstd/ffi/os_str.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ impl ops::Index<ops::RangeFull> for OsString {
379379
}
380380
}
381381

382+
#[stable(feature = "mut_osstr", since = "1.44.0")]
383+
impl ops::IndexMut<ops::RangeFull> for OsString {
384+
#[inline]
385+
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
386+
OsStr::from_inner_mut(self.inner.as_mut_slice())
387+
}
388+
}
389+
382390
#[stable(feature = "rust1", since = "1.0.0")]
383391
impl ops::Deref for OsString {
384392
type Target = OsStr;
@@ -389,6 +397,14 @@ impl ops::Deref for OsString {
389397
}
390398
}
391399

400+
#[stable(feature = "mut_osstr", since = "1.44.0")]
401+
impl ops::DerefMut for OsString {
402+
#[inline]
403+
fn deref_mut(&mut self) -> &mut OsStr {
404+
&mut self[..]
405+
}
406+
}
407+
392408
#[stable(feature = "osstring_default", since = "1.9.0")]
393409
impl Default for OsString {
394410
/// Constructs an empty `OsString`.
@@ -512,6 +528,11 @@ impl OsStr {
512528
unsafe { &*(inner as *const Slice as *const OsStr) }
513529
}
514530

531+
#[inline]
532+
fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
533+
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
534+
}
535+
515536
/// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
516537
///
517538
/// This conversion may entail doing a check for UTF-8 validity.

src/libstd/sys/windows/os_str.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ impl Buf {
8080
unsafe { mem::transmute(self.inner.as_slice()) }
8181
}
8282

83+
#[inline]
84+
pub fn as_mut_slice(&mut self) -> &mut Slice {
85+
unsafe { mem::transmute(self.inner.as_mut_slice()) }
86+
}
87+
8388
pub fn into_string(self) -> Result<String, Buf> {
8489
self.inner.into_string().map_err(|buf| Buf { inner: buf })
8590
}

src/libstd/sys_common/os_str_bytes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ impl Buf {
109109
unsafe { mem::transmute(&*self.inner) }
110110
}
111111

112+
#[inline]
113+
pub fn as_mut_slice(&mut self) -> &mut Slice {
114+
unsafe { mem::transmute(&mut *self.inner) }
115+
}
116+
112117
pub fn into_string(self) -> Result<String, Buf> {
113118
String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
114119
}

0 commit comments

Comments
 (0)