diff --git a/Cargo.toml b/Cargo.toml index c269606..69261d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "non-empty-string" -version = "0.2.4" +version = "0.2.5" edition = "2021" authors = ["Midas Lambrichts "] license = "MIT OR Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 61fe38e..915ab88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + pub fn new(string: String) -> Result { if string.is_empty() { Err(string) } else { Ok(NonEmptyString(string)) } } + /// Creates a new `NonEmptyString`, assuming `string` is 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 {