Skip to content

Commit 19e35bc

Browse files
authored
define new_unchecked
1 parent 36affb4 commit 19e35bc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,24 @@ mod trait_impls;
2525
#[repr(transparent)]
2626
pub struct NonEmptyString(String);
2727

28-
#[allow(clippy::len_without_is_empty)] // is_empty would always returns false so it seems a bit silly to have it.
28+
#[allow(clippy::len_without_is_empty, reason = "is_empty would always returns false so it seems a bit silly to have it.")]
2929
impl NonEmptyString {
30-
/// Attempts to create a new NonEmptyString.
30+
/// Attempts to create a new `NonEmptyString`.
3131
/// If the given `string` is empty, `Err` is returned, containing the original `String`, `Ok` otherwise.
32-
pub fn new(string: String) -> Result<NonEmptyString, String> {
32+
pub fn new(string: String) -> Result<Self, String> {
3333
if string.is_empty() {
3434
Err(string)
3535
} else {
3636
Ok(NonEmptyString(string))
3737
}
3838
}
39+
/// Creates a new `NonEmptyString`, assuming it's not empty.
40+
///
41+
/// # Safety
42+
/// If the given `string` is empty, it'll be undefined behavior.
43+
pub unsafe fn new_unchecked(string: String) -> Self {
44+
Self::new(string).unwrap_unchecked()
45+
}
3946

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

0 commit comments

Comments
 (0)