String: add as_mut_vec

This commit is contained in:
samlich 2020-07-24 12:26:22 +00:00 committed by Emil Fresk
parent 0364e05766
commit 6ba462eb93
2 changed files with 30 additions and 0 deletions

View File

@ -178,6 +178,34 @@ where
unsafe { str::from_utf8_unchecked_mut(self.0.vec.as_mut_slice()) }
}
/// Returns a mutable reference to the contents of this `String`.
///
/// # Safety
///
/// This function is unsafe because it does not check that the bytes passed
/// to it are valid UTF-8. If this constraint is violated, it may cause
/// memory unsafety issues with future users of the `String`, as the rest of
/// the library assumes that `String`s are valid UTF-8.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut s = String::from("hello");
///
/// unsafe {
/// let vec = s.as_mut_vec();
/// assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
///
/// vec.reverse();
/// }
/// assert_eq!(s, "olleh");
/// ```
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, N> {
&mut *(&mut self.0.vec as *mut crate::i::Vec<GenericArray<u8, N>> as *mut Vec<u8, N>)
}
/// Appends a given string slice onto the end of this `String`.
///
/// # Examples

View File

@ -160,6 +160,8 @@ where
/// }
/// assert_eq!(vec, [7, 1, 2, 3]);
/// ```
// repr(transparent) is needed for [`String::as_mut_vec`]
#[repr(transparent)]
pub struct Vec<T, N>(#[doc(hidden)] pub crate::i::Vec<GenericArray<T, N>>)
where
N: ArrayLength<T>;