Skip to content

Implemented macro for compile time non empty string creation #19

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

Closed
wants to merge 2 commits into from

Conversation

patskovn
Copy link
Contributor

This is more convenient for constant strings in the codebase that are known to be non empty.

Proper tests for failures would require importing special libraries for macro tests. I do not expect this to be too so I assume it is better to keep this lib smaller and not to add unit tests for compilation failures. Thankfully doctests validate the behaviour, but to be sure I tested it manually via:

diff --git a/src/macros.rs b/src/macros.rs
index a1720fd..0b411cc 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -41,4 +41,9 @@ mod tests {
         let s = non_empty!("Test String");
         assert_eq!(s, NonEmptyString::try_from("Test String").unwrap());
     }
+
+    #[test]
+    fn test_non_empty_string_macro_empty_runtime() {
+        let _ = non_empty!("");
+    }
 }

And then

$ cargo t --features macro

error[E0080]: evaluation of constant value failed
  --> src/macros.rs:48:17
   |
48 |         let _ = non_empty!("");
   |                 ^^^^^^^^^^^^^^ the evaluated program panicked at 'String cannot be empty', src/macros.rs:48:17
   |
   = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `non_empty` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0080`.
error: could not compile `non-empty-string` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

This is more convenient for constant strings in the codebase that are
known to be non empty.
@mhedgpeth
Copy link

+1 to this PR; I went looking for this functionality, copied in the macro, and this really helped me not make the crate to difficult to use when I had obviously non-empty static strings.

MidasLamb added a commit that referenced this pull request Apr 9, 2025
The majority of the work has been done by @patskovn in #19, I'm just cleaning up and preparing a release,
since he has used his branch to publish his own package, so I can't just merge it in unfortunately
@MidasLamb MidasLamb mentioned this pull request Apr 9, 2025
MidasLamb added a commit that referenced this pull request Apr 9, 2025
The majority of the work has been done by @patskovn in #19, I'm just cleaning up and preparing a release,
since he has used his branch to publish his own package, so I can't just merge it in unfortunately
@github-actions github-actions bot mentioned this pull request Apr 9, 2025
@MidasLamb
Copy link
Owner

Thanks for the work @patskovn ! I've branched off on your initial commit and merged it in through #20 (You've waited long enough for any feedback on this PR, didn't want to make you wait any longer just to revert your second commit).

@MidasLamb MidasLamb closed this Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants