Skip to content

Commit

Permalink
String: Add as_(mut_)_view methods to
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jun 28, 2024
1 parent 2bff448 commit efbb9f1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,48 @@ impl<const N: usize> String<N> {
pub fn into_bytes(self) -> Vec<u8, N> {
self.vec
}

/// Get a reference to the `String`, erasing the `N` const-generic.
///
///
/// ```rust
/// # use heapless::string::{String, StringView};
/// let s: String<10> = String::try_from("hello").unwrap();
/// let view: &StringView = s.as_view();
/// ```
///
/// It is often preferable to do the same through type coerction, since `String<N>` implements `Unsize<StringView>`:
///
/// ```rust
/// # use heapless::string::{String, StringView};
/// let s: String<10> = String::try_from("hello").unwrap();
/// let view: &StringView = &s;
/// ```
#[inline]
pub fn as_view(&self) -> &StringView {
self
}

/// Get a mutable reference to the `String`, erasing the `N` const-generic.
///
///
/// ```rust
/// # use heapless::string::{String, StringView};
/// let mut s: String<10> = String::try_from("hello").unwrap();
/// let view: &mut StringView = s.as_mut_view();
/// ```
///
/// It is often preferable to do the same through type coerction, since `String<N>` implements `Unsize<StringView>`:
///
/// ```rust
/// # use heapless::string::{String, StringView};
/// let mut s: String<10> = String::try_from("hello").unwrap();
/// let view: &mut StringView = &mut s;
/// ```
#[inline]
pub fn as_mut_view(&mut self) -> &mut StringView {
self
}
}

impl<S: Storage> StringInner<S> {
Expand Down

0 comments on commit efbb9f1

Please sign in to comment.