Skip to content

Commit c21425b

Browse files
String: add as_mut_vec_view
1 parent c8881a5 commit c21425b

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/string.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<const N: usize> String<N> {
314314
///
315315
/// unsafe {
316316
/// let vec = s.as_mut_vec();
317-
/// assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
317+
/// assert_eq!(&b"hello", &vec);
318318
///
319319
/// vec.reverse();
320320
/// }
@@ -325,6 +325,37 @@ impl<const N: usize> String<N> {
325325
&mut self.vec
326326
}
327327

328+
/// Returns a mutable reference to the contents of this `String`.
329+
///
330+
/// # Safety
331+
///
332+
/// This function is unsafe because it does not check that the bytes passed
333+
/// to it are valid UTF-8. If this constraint is violated, it may cause
334+
/// memory unsafety issues with future users of the `String`, as the rest of
335+
/// the library assumes that `String`s are valid UTF-8.
336+
///
337+
/// # Examples
338+
///
339+
/// Basic usage:
340+
///
341+
/// ```
342+
/// use heapless::String;
343+
///
344+
/// let mut s: String<8> = String::try_from("hello")?;
345+
///
346+
/// unsafe {
347+
/// let vec = s.as_mut_vec_view();
348+
/// assert_eq!(&b"hello", &vec);
349+
///
350+
/// vec.reverse();
351+
/// }
352+
/// assert_eq!(s, "olleh");
353+
/// # Ok::<(), ()>(())
354+
/// ```
355+
pub unsafe fn as_mut_vec_view(&mut self) -> &mut VecView<u8> {
356+
&mut self.vec
357+
}
358+
328359
/// Appends a given string slice onto the end of this `String`.
329360
///
330361
/// # Examples
@@ -592,7 +623,7 @@ impl StringView {
592623
///
593624
/// unsafe {
594625
/// let vec = s.as_mut_vec();
595-
/// assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
626+
/// assert_eq!(&b"hello", &vec);
596627
///
597628
/// vec.reverse();
598629
/// }

0 commit comments

Comments
 (0)