Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "non-empty-string"
version = "0.2.4"
version = "0.3.0"
edition = "2021"
authors = ["Midas Lambrichts <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,24 @@ mod trait_impls;
#[repr(transparent)]
pub struct NonEmptyString(String);

#[allow(clippy::len_without_is_empty)] // is_empty would always returns false so it seems a bit silly to have it.
#[allow(clippy::len_without_is_empty, reason = "is_empty would always returns false so it seems a bit silly to have it.")]
impl NonEmptyString {
/// Attempts to create a new NonEmptyString.
/// Attempts to create a new `NonEmptyString`.
/// If the given `string` is empty, `Err` is returned, containing the original `String`, `Ok` otherwise.
pub fn new(string: String) -> Result<NonEmptyString, String> {
pub fn new(string: String) -> Result<Self, String> {
if string.is_empty() {
Err(string)
} else {
Ok(NonEmptyString(string))
}
}
/// Creates a new `NonEmptyString`, assuming it's not empty.
///
/// # Safety
/// If the given `string` is empty, it'll be undefined behavior.
pub unsafe fn new_unchecked(string: String) -> Self {
Self::new(string).unwrap_unchecked()
}

/// Returns a reference to the contained value.
pub fn get(&self) -> &str {
Expand Down
Loading