From 383f0ae3588b329843b8ab73f80015c005396b79 Mon Sep 17 00:00:00 2001 From: pantonshire Date: Wed, 14 Sep 2022 15:08:39 +0100 Subject: [PATCH] strings: add CappedString::clear method This patch implements `CappedString::clear`, which provides an easy way for users to clear the contents of a `CappedString`. --- src/strings/capped.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/strings/capped.rs b/src/strings/capped.rs index 7c5bda0..3bf87cd 100644 --- a/src/strings/capped.rs +++ b/src/strings/capped.rs @@ -307,6 +307,28 @@ impl CappedString { unsafe { self.append_bytes(src, len); } } + /// Empties the contents of this `CappedString`. + /// + /// ``` + /// # use libshire::strings::CappedString; + /// # fn main() -> Result<(), libshire::strings::capped::CapacityError> { + /// let mut s = CappedString::<16>::new("hello")?; + /// + /// assert!(!s.is_empty()); + /// assert_eq!(&*s, "hello"); + /// + /// s.clear(); + /// + /// assert!(s.is_empty()); + /// assert_eq!(&*s, ""); + /// # Ok(()) + /// # } + /// ``` + #[inline] + pub fn clear(&mut self) { + self.len = 0; + } + /// Returns a string slice pointing to the underlying string data. #[inline] #[must_use]