Skip to content

Commit fc79ad4

Browse files
authored
Merge pull request #17 from Rudxain/uncheck
define `new_unchecked`
2 parents 36affb4 + cc674ab commit fc79ad4

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "non-empty-string"
3-
version = "0.2.4"
3+
version = "0.2.5"
44
edition = "2021"
55
authors = ["Midas Lambrichts <[email protected]>"]
66
license = "MIT OR Apache-2.0"

src/lib.rs

+10-3
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 `string` is 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)