Skip to content

Commit

Permalink
impl FromStr and refactoring Clone impl
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Nov 15, 2023
1 parent 48c4e7e commit 79ceae1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
31 changes: 10 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl fmt::Display for StrBufError {
}
}

#[derive(Copy, Clone)]
///Stack based string.
///
///It's size is `mem::size_of::<T>() + mem::size_of::<u8>()`, but remember that it can be padded.
Expand Down Expand Up @@ -376,27 +377,6 @@ impl<const S: usize> core::fmt::Debug for StrBuf<S> {
}
}

impl<const S: usize> Clone for StrBuf<S> {
#[inline]
fn clone(&self) -> Self {
let mut result = Self::new();
unsafe {
result.push_str_unchecked(self.as_str())
}
result
}

#[inline]
fn clone_from(&mut self, source: &Self) {
self.clear();
unsafe {
self.push_str_unchecked(source.as_str());
}
}
}

impl<const S: usize> Copy for StrBuf<S> {}

impl<const S: usize> AsRef<[u8]> for StrBuf<S> {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
Expand Down Expand Up @@ -483,3 +463,12 @@ impl<const S: usize> core::convert::TryFrom<&str> for StrBuf<S> {
Self::from_str_checked(text)
}
}

impl<const S: usize> core::str::FromStr for StrBuf<S> {
type Err = StrBufError;

#[inline(always)]
fn from_str(text: &str) -> Result<Self, Self::Err> {
Self::from_str_checked(text)
}
}
3 changes: 3 additions & 0 deletions tests/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ fn should_correctly_truncate_by_char_boundary() {
assert_eq!(buf.push_str("i"), 1);
assert_eq!(buf, "ロri");
assert_eq!(buf.push_str("."), 0);

let copy = buf;
assert_eq!(copy, buf);
}

0 comments on commit 79ceae1

Please sign in to comment.