Repository: mgeisler/textwrap (language: rust)

Issue:
Compile-time constant evaluation?
Evaluate at compile-time, zero-cost and no runtime cost at the cost of longer compile time. Of course, lazy-static and similar crates would be an option.

https://github.com/rust-lang/rfcs/blob/master/text/0246-const-vs-static.md


Discussion / hints:
Hi @pickfire That's an interesting idea — if it's relatively easy to implement with the existing functions?

I always assumed that the cost of wrapping strings would be very low. Also, I expect most programs will spend more time waiting for I/O to complete than waiting for strings to be wrapped.

Do you have a different use case?
@mgeisler Yes, it it supposed to be very low. But then is it useful to have it it runtime when we could do it during compile time? Of course some parts still won't be able to use this feature when it involves `String` or `?Sized`, but no reason we should not do it for `&'static str` which does not change. The best thing I see is, why not do it **just because we can**! :D
Thanks to @althonos, this is now possible. Please see his `textwrap-macros` crate: https://github.com/althonos/textwrap-macros. It let's you indent, dedent, and wrap strings — all at compile time.
Nice, but should we mention it in README and the first page in rustdoc to make it more discoverable? Or maybe pull it into this crate behind a feature flag?
Yeah, mentioning it in the documentation is a great idea!

I don't think `textwrap` should have a dependency on `textwrap-macros` — since `textwrap-macros` already has a dependency back to `textwrap`. It seems simpler to me to have the layering work like this:

    textwrap-macros
        |
        v
    textwrap-macros-impl
        |
        v
    textwrap

Then there is no doubt as to what version is in play at any point.

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ef9d9075..e96d6def 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,11 @@
 This file lists the most important changes made in each release of
 `textwrap`.
 
+## Unreleased
+
+* Fixed [#176][issue-176]: Compile-time wrapping via the
+  [`textwrap-macros` crate].
+
 ## Version 0.12.0 — June 26th, 2020
 
 The code has been updated to the [Rust 2018 edition][rust-2018] and
@@ -143,6 +148,8 @@ Introduced `Wrapper` struct. Added support for wrapping on hyphens.
 First public release with support for wrapping strings on whitespace.
 
 [rust-2018]: https://doc.rust-lang.org/edition-guide/rust-2018/
+[`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros
+
 [issue-13]: https://github.com/mgeisler/textwrap/issues/13
 [issue-14]: https://github.com/mgeisler/textwrap/issues/14
 [issue-19]: https://github.com/mgeisler/textwrap/issues/19
diff --git a/README.md b/README.md
index b219298e..20673a9a 100644
--- a/README.md
+++ b/README.md
@@ -102,6 +102,12 @@ application. If you need hyphenation for other languages, you need to
 download a [precompiled `.bincode` file][bincode] and load it
 yourself. Please see the [`hyphenation` documentation] for details.
 
+## Wrapping Strings at Compile Time
+
+If your strings are known at compile time, please take a look at the
+procedural macros from the [`textwrap-macros` crate].
+
+
 ## Examples
 
 The library comes with some small example programs that shows various
@@ -206,6 +212,7 @@ Contributions will be accepted under the same license.
 [appveyor]: https://ci.appveyor.com/project/mgeisler/textwrap
 [codecov]: https://codecov.io/gh/mgeisler/textwrap
 [py-textwrap]: https://docs.python.org/library/textwrap
+[`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros
 [`hyphenation` example]: https://github.com/mgeisler/textwrap/blob/master/examples/hyphenation.rs
 [`termwidth` example]: https://github.com/mgeisler/textwrap/blob/master/examples/termwidth.rs
 [patterns]: https://github.com/tapeinosyne/hyphenation/tree/master/patterns-tex
diff --git a/src/lib.rs b/src/lib.rs
index 69da14d6..e747d635 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -47,6 +47,11 @@
 //! ping text.
 //! ```
 //!
+//! # Wrapping Strings at Compile Time
+//!
+//! If your strings are known at compile time, please take a look at
+//! the procedural macros from the [`textwrap-macros` crate].
+//!
 //! # Displayed Width vs Byte Size
 //!
 //! To word wrap text, one must know the width of each word so one can
@@ -76,6 +81,7 @@
 //!   [hyphenation][] crate. See the [`WordSplitter`] trait for
 //!   details.
 //!
+//! [`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros
 //! [unicode-width]: https://docs.rs/unicode-width/
 //! [terminal_size]: https://crates.io/crates/terminal_size
 //! [hyphenation]: https://crates.io/crates/hyphenation


Test patch: