Skip to content

Commit

Permalink
Merge pull request rust-lang#7 from waywardmonkeys/add-is-empty
Browse files Browse the repository at this point in the history
clippy: Add (and test) is_empty method.
  • Loading branch information
matklad authored Oct 12, 2018
2 parents 1702c33 + 5644a67 commit 28b4ec7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl SmolStr {
pub fn len(&self) -> usize {
self.0.len()
}

#[inline(always)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

impl Deref for SmolStr {
Expand Down Expand Up @@ -180,6 +185,15 @@ impl Repr {
}
}

fn is_empty(&self) -> bool {
match self {
Repr::Heap(data) => data.is_empty(),
Repr::Inline { len, .. } => *len == 0,
// A substring isn't created for an empty string.
Repr::Substring { .. } => false,
}
}

fn as_str(&self) -> &str {
match self {
Repr::Heap(data) => &*data,
Expand Down
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn check_props(s: &str) -> Result<(), proptest::test_runner::TestCaseError> {
let smol = SmolStr::new(s);
prop_assert_eq!(smol.as_str(), s);
prop_assert_eq!(smol.len(), s.len());
prop_assert_eq!(smol.is_empty(), s.is_empty());
Ok(())
}

Expand Down

0 comments on commit 28b4ec7

Please sign in to comment.