Skip to content

define new_unchecked #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.2.5"
edition = "2021"
authors = ["Midas Lambrichts <midaslamb@gmail.com>"]
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 `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 {
Expand Down
Loading