From cd1ebfc6e8795b30fba7a67d3edef627416c4567 Mon Sep 17 00:00:00 2001 From: rodrimati1992 Date: Sat, 14 Oct 2023 00:02:09 -0300 Subject: [PATCH] More docs improvements Fixed `panic` invocation after 2018->2021 migration --- README.md | 79 +++++++++---------- const_format/src/const_debug_derive.rs | 2 +- const_format/src/fmt.rs | 2 +- const_format/src/for_assert_macros.rs | 2 +- const_format/src/for_examples.rs | 2 +- const_format/src/lib.rs | 35 ++++---- .../src/macros/assertions/assertc_macros.rs | 31 ++++---- .../src/macros/assertions/assertcp_macros.rs | 30 +++---- 8 files changed, 87 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index a540555..2b0a5d1 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ This crate provides types and macros for formatting strings at compile-time. # Rust versions -There are some features that require a variety of stable Rust versions and -others that Rust nightly, +There are some features that require a variety of stable Rust versions and +others that require Rust nightly, the sections below describe the features that are available for each version. ### Rust 1.57.0 @@ -26,16 +26,16 @@ Concatenates `integers`, `bool`, `char`, and `&str` constants into a `&'static s [`format`]-like formatting which takes `integers`, `bool`, `char`, and `&str` constants, and emits a `&'static str` constant. -- [`str_get`]: -Indexes a `&'static str` constant, returning `None` when the index is out of bounds. +- [`str_get`]: +Indexes a `&'static str` constant, returning `None` when the index is out of bounds. -- [`str_index`]: -Indexes a `&'static str` constant. +- [`str_index`]: +Indexes a `&'static str` constant. -- [`str_repeat`]: +- [`str_repeat`]: Creates a `&'static str` by repeating a `&'static str` constant `times` times. -- [`str_splice`]: +- [`str_splice`]: Replaces a substring in a `&'static str` constant. - [`map_ascii_case`]: @@ -47,8 +47,8 @@ Replaces all the instances of a pattern in a `&'static str` constant with another `&'static str` constant. -The "assertcp" feature enables the [`assertcp`], [`assertcp_eq`], -and [`assertcp_ne`] macros. +The "assertcp" feature enables the [`assertcp`], [`assertcp_eq`], +and [`assertcp_ne`] macros. These macros are like the standard library assert macros, but evaluated at compile-time, with the limitation that they can only have primitive types as arguments @@ -60,12 +60,11 @@ The `"rust_1_64"` feature enables these macros: - [`str_split`]: splits a string constant - ### Rust nightly By enabling the "fmt" feature, you can use a [`std::fmt`]-like API. -This requires the nightly compiler because it uses mutable references in const fn, +This requires the nightly compiler, because it uses mutable references in const fn, which have not been stabilized as of writing these docs. All the other features of this crate are implemented on top of the [`const_format::fmt`] API: @@ -81,16 +80,15 @@ a `&'static str` constant. [`write`]-like macro that can format many standard library and user defined types into a type that implements [`WriteMarker`]. -The "derive" feature enables the [`ConstDebug`] macro, -and the "fmt" feature.
+The `"derive"` feature enables the [`ConstDebug`] macro, +and the `"fmt"` feature.
[`ConstDebug`] derives the [`FormatMarker`] trait, and implements an inherent `const_debug_fmt` method for compile-time debug formatting. -The "assertc" feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros, -and the "fmt" feature.
+The `"assertc"` feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros, +and the `"fmt"` feature.
These macros are like the standard library assert macros, but evaluated at compile-time. - # Examples ### Concatenation of primitive types @@ -116,7 +114,6 @@ const FOO: &str = formatcp!("{NAME}, age {}!", compute_age(NAME)); assert_eq!(FOO, "John, age 24!"); const fn compute_age(s: &str) -> usize { s.len() * 6 } - ``` ### Formatting custom types @@ -124,8 +121,7 @@ const fn compute_age(s: &str) -> usize { s.len() * 6 } This example demonstrates how you can use the [`ConstDebug`] derive macro, and then format the type into a `&'static str` constant. -This example requires Rust nightly, and the "derive" feature. - +This example requires Rust nightly, and the `"derive"` feature. ```rust #![feature(const_mut_refs)] @@ -148,12 +144,11 @@ const MSG: Message = Message{ const FOO: &str = formatc!("{:?}", MSG); -fn main(){ - assert_eq!( - FOO, - "Message { ip: [Octet(127), Octet(0), Octet(0), Octet(1)], value: \"Hello, World!\" }" - ); -} +assert_eq!( + FOO, + "Message { ip: [Octet(127), Octet(0), Octet(0), Octet(1)], value: \"Hello, World!\" }" +); + ``` ### Formatted const assertions @@ -161,8 +156,7 @@ fn main(){ This example demonstrates how you can use the [`assertcp_ne`] macro to do compile-time inequality assertions with formatted error messages. -This requires the "assertcp" feature, -because using the `panic` macro at compile-time requires Rust 1.57.0. +This requires the `"assertcp"` feature. ```rust, compile_fail use const_format::assertcp_ne; @@ -181,6 +175,7 @@ macro_rules! check_valid_pizza{ check_valid_pizza!("John", "salami"); check_valid_pizza!("Dave", "sausage"); check_valid_pizza!("Bob", "pineapple"); + ``` This is the compiler output: @@ -200,8 +195,6 @@ You can't put pineapple on pizza, Bob ``` - -
# Limitations @@ -220,6 +213,7 @@ so while a `Type::::FOO` argument would be fine, So `#[doc = "foobar"]` cannot be replaced with `#[doc = concatcp!("foo", "bar") ]`. + ### Integer arguments Integer arguments must have a type inferrable from context. @@ -235,8 +229,7 @@ assert_eq!(const_format::concatcp!(2u32, 2 + 1u8, 3u8 + 1), "234"); ``` Example of what does not compile: - -```compile_fail +```rust,compile_fail assert_eq!(const_format::concatcp!(1 + 1, 2 + 1), "23"); ``` # Plans @@ -247,39 +240,40 @@ None right now. All function-like macros from `const_format` can be used when the crate is renamed. -The [`ConstDebug`] derive macro has the `#[cdeb(crate = "foo::bar")]` attribute to +The [`ConstDebug`] derive macro has the `#[cdeb(crate = "foo::bar")]` attribute to tell it where to find the `const_format` crate. Example of renaming the `const_format` crate in the Cargo.toml file: ```toml +[dependencies] cfmt = {version = "0.*", package = "const_format"} ``` # Cargo features -- "fmt": Enables the [`std::fmt`]-like API, +- `"fmt"`: Enables the [`std::fmt`]-like API, requires Rust nightly because it uses mutable references in const fn.
This feature includes the [`formatc`]/[`writec`] formatting macros. -- "derive": requires Rust nightly, implies the "fmt" feature, +- `"derive"`: requires Rust nightly, implies the `"fmt"` feature, provides the [`ConstDebug`] derive macro to format user-defined types at compile-time.
This implicitly uses the `syn` crate, so clean compiles take a bit longer than without the feature. -- "assertc": requires Rust nightly, implies the "fmt" feature, +- `"assertc"`: requires Rust nightly, implies the `"fmt"` feature, enables the [`assertc`], [`assertc_eq`], and [`assertc_ne`] assertion macros.
-This feature was previously named "assert", -but it was renamed to avoid confusion with the "assertcp" feature. +This feature was previously named `"assert"`, +but it was renamed to avoid confusion with the `"assertcp"` feature. -- "assertcp": +- `"assertcp"`: Enables the [`assertcp`], [`assertcp_eq`], and [`assertcp_ne`] assertion macros. -- "rust_1_64": Enables the [`str_split`] macro. +- `"rust_1_64"`: Enables the [`str_split`] macro. Allows the `as_bytes_alt` methods and `slice_up_to_len_alt` methods to run -in constant time, rather than linear time proportional to the truncated part of the slice. +in constant time, rather than linear time (proportional to the truncated part of the slice). # No-std support -`const_format` is `#![no_std]`, it can be used anywhere Rust can be used. +`const_format` is unconditionally `#![no_std]`, it can be used anywhere Rust can be used. # Minimum Supported Rust Version @@ -288,7 +282,6 @@ in constant time, rather than linear time proportional to the truncated part of Features that require newer versions of Rust, or the nightly compiler, need to be explicitly enabled with cargo features. - [`assertc`]: https://docs.rs/const_format/0.2.*/const_format/macro.assertc.html [`assertc_eq`]: https://docs.rs/const_format/0.2.*/const_format/macro.assertc_eq.html diff --git a/const_format/src/const_debug_derive.rs b/const_format/src/const_debug_derive.rs index b70883d..9444569 100644 --- a/const_format/src/const_debug_derive.rs +++ b/const_format/src/const_debug_derive.rs @@ -7,7 +7,7 @@ /// /// This derive macro is only available with the "derive" feature, /// and the nightly compiler, -/// because at the time of writing these docs (2021-08-XX) mutable references in const fn +/// because at the time of writing these docs (2023-10-XX) mutable references in const fn /// require the unstable /// [`const_mut_refs`](https://github.com/rust-lang/rust/issues/57349) feature. /// diff --git a/const_format/src/fmt.rs b/const_format/src/fmt.rs index d060ca8..b6fd005 100644 --- a/const_format/src/fmt.rs +++ b/const_format/src/fmt.rs @@ -4,7 +4,7 @@ //! # Features //! //! This module requires the "fmt" feature to be exported, and the nightly compiler, -//! because at the time of writing these docs (2021-08-XX) mutable references in const fn +//! because at the time of writing these docs (2023-10-XX) mutable references in const fn //! require the unstable //! [`const_mut_refs`](https://github.com/rust-lang/rust/issues/57349) feature. //! diff --git a/const_format/src/for_assert_macros.rs b/const_format/src/for_assert_macros.rs index 8acca18..fab1506 100644 --- a/const_format/src/for_assert_macros.rs +++ b/const_format/src/for_assert_macros.rs @@ -5,7 +5,7 @@ use crate::pargument::PArgument; #[track_caller] pub const fn assert_(cond: bool, message: &'static str) { if cond { - panic!(message) + panic!("{}", message) } } diff --git a/const_format/src/for_examples.rs b/const_format/src/for_examples.rs index ce760e8..098c083 100644 --- a/const_format/src/for_examples.rs +++ b/const_format/src/for_examples.rs @@ -3,7 +3,7 @@ //! # Features //! //! This module is only exported with the "fmt" feature, and the nightly compiler, -//! because at the time of writing these docs (2021-08-XX) mutable references in const fn +//! because at the time of writing these docs (2023-10-XX) mutable references in const fn //! require the unstable //! [`const_mut_refs`](https://github.com/rust-lang/rust/issues/57349) feature. diff --git a/const_format/src/lib.rs b/const_format/src/lib.rs index fada230..2bb90a4 100644 --- a/const_format/src/lib.rs +++ b/const_format/src/lib.rs @@ -5,7 +5,7 @@ //! # Rust versions //! //! There are some features that require a variety of stable Rust versions and -//! others that Rust nightly, +//! others that require Rust nightly, //! the sections below describe the features that are available for each version. //! //! ### Rust 1.57.0 @@ -57,7 +57,7 @@ //! //! By enabling the "fmt" feature, you can use a [`std::fmt`]-like API. //! -//! This requires the nightly compiler because it uses mutable references in const fn, +//! This requires the nightly compiler, because it uses mutable references in const fn, //! which have not been stabilized as of writing these docs. //! //! All the other features of this crate are implemented on top of the [`const_format::fmt`] API: @@ -73,14 +73,15 @@ //! [`write`]-like macro that can format many standard library and user defined types //! into a type that implements [`WriteMarker`]. //! -//! The "derive" feature enables the [`ConstDebug`] macro, -//! and the "fmt" feature.
+//! The `"derive"` feature enables the [`ConstDebug`] macro, +//! and the `"fmt"` feature.
//! [`ConstDebug`] derives the [`FormatMarker`] trait, //! and implements an inherent `const_debug_fmt` method for compile-time debug formatting. //! -//! The "assertc" feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros, -//! and the "fmt" feature.
+//! The `"assertc"` feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros, +//! and the `"fmt"` feature.
//! These macros are like the standard library assert macros, but evaluated at compile-time. +//! //! # Examples //! //! ### Concatenation of primitive types @@ -114,7 +115,7 @@ //! This example demonstrates how you can use the [`ConstDebug`] derive macro, //! and then format the type into a `&'static str` constant. //! -//! This example requires Rust nightly, and the "derive" feature. +//! This example requires Rust nightly, and the `"derive"` feature. //! #![cfg_attr(feature = "derive", doc = "```rust")] #![cfg_attr(not(feature = "derive"), doc = "```ignore")] @@ -150,8 +151,7 @@ //! This example demonstrates how you can use the [`assertcp_ne`] macro to //! do compile-time inequality assertions with formatted error messages. //! -//! This requires the "assertcp" feature, -//! because using the `panic` macro at compile-time requires Rust 1.57.0. +//! This requires the `"assertcp"` feature. //! #![cfg_attr(feature = "assertcp", doc = "```compile_fail")] #![cfg_attr(not(feature = "assertcp"), doc = "```ignore")] @@ -239,30 +239,31 @@ //! //! Example of renaming the `const_format` crate in the Cargo.toml file: //! ```toml +//! [dependencies] //! cfmt = {version = "0.*", package = "const_format"} //! ``` //! //! # Cargo features //! -//! - "fmt": Enables the [`std::fmt`]-like API, +//! - `"fmt"`: Enables the [`std::fmt`]-like API, //! requires Rust nightly because it uses mutable references in const fn.
//! This feature includes the [`formatc`]/[`writec`] formatting macros. //! -//! - "derive": requires Rust nightly, implies the "fmt" feature, +//! - `"derive"`: requires Rust nightly, implies the `"fmt"` feature, //! provides the [`ConstDebug`] derive macro to format user-defined types at compile-time.
//! This implicitly uses the `syn` crate, so clean compiles take a bit longer than without the feature. //! -//! - "assertc": requires Rust nightly, implies the "fmt" feature, +//! - `"assertc"`: requires Rust nightly, implies the `"fmt"` feature, //! enables the [`assertc`], [`assertc_eq`], and [`assertc_ne`] assertion macros.
-//! This feature was previously named "assert", -//! but it was renamed to avoid confusion with the "assertcp" feature. +//! This feature was previously named `"assert"`, +//! but it was renamed to avoid confusion with the `"assertcp"` feature. //! -//! - "assertcp": +//! - `"assertcp"`: //! Enables the [`assertcp`], [`assertcp_eq`], and [`assertcp_ne`] assertion macros. //! -//! - "rust_1_64": Enables the [`str_split`] macro. +//! - `"rust_1_64"`: Enables the [`str_split`] macro. //! Allows the `as_bytes_alt` methods and `slice_up_to_len_alt` methods to run -//! in constant time, rather than linear time proportional to the truncated part of the slice. +//! in constant time, rather than linear time (proportional to the truncated part of the slice). //! //! # No-std support //! diff --git a/const_format/src/macros/assertions/assertc_macros.rs b/const_format/src/macros/assertions/assertc_macros.rs index f38f0b7..7b2d866 100644 --- a/const_format/src/macros/assertions/assertc_macros.rs +++ b/const_format/src/macros/assertions/assertc_macros.rs @@ -75,7 +75,7 @@ with_shared_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// #![feature(const_mut_refs)] @@ -94,14 +94,13 @@ with_shared_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions.rs:132:10 + /// --> const_format/src/macros/assertions/assertc_macros.rs:109:10 /// | - /// 12 | assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L, R); + /// 11 | assertc!(L + R == 5, "{} plus {} isn't 5, buddy", L, R); /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed. /// 2 plus 2 isn't 5, buddy - /// ', src/macros/assertions.rs:12:10 - /// + /// ', const_format/src/macros/assertions/assertc_macros.rs:11:10 /// ``` /// #[cfg_attr(feature = "__docsrs", doc(cfg(feature = "assertc")))] @@ -175,7 +174,7 @@ assert_eq_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// #![feature(const_mut_refs)] @@ -193,14 +192,13 @@ assert_eq_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions.rs:296:13 - /// | - /// 9 | assertc_eq!(size_of::(), size_of::()); - /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' + /// --> const_format/src/macros/assertions/assertc_macros.rs:222:13 + /// | + /// 10 | assertc_eq!(size_of::(), size_of::()); + /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed: `(left == right)` /// left: `4` - /// right: `1`', src/macros/assertions.rs:9:13 - /// + /// right: `1`', const_format/src/macros/assertions/assertc_macros.rs:10:13 /// ``` /// /// ### Comparing user-defined types @@ -302,7 +300,7 @@ assert_eq_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// #![feature(const_mut_refs)] @@ -322,14 +320,13 @@ assert_eq_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions.rs:465:13 + /// --> const_format/src/macros/assertions/assertc_macros.rs:350:13 /// | - /// 11 | assertc_ne!(size_of::(), size_of::()); + /// 12 | assertc_ne!(size_of::(), size_of::()); /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed: `(left != right)` /// left: `4` - /// right: `4`', src/macros/assertions.rs:11:13 - /// + /// right: `4`', const_format/src/macros/assertions/assertc_macros.rs:12:13 /// ``` /// /// ### Comparing user-defined types diff --git a/const_format/src/macros/assertions/assertcp_macros.rs b/const_format/src/macros/assertions/assertcp_macros.rs index d4c6e8a..d0e71e0 100644 --- a/const_format/src/macros/assertions/assertcp_macros.rs +++ b/const_format/src/macros/assertions/assertcp_macros.rs @@ -18,7 +18,7 @@ macro_rules! with_shared_docs {( /// /// This macro uses the same syntax /// for the format string and formatting arguments as the - /// [`formatcp`] macro. + /// [`formatcp`](crate::formatcp) macro. /// $(#[$after_syntax])* /// # Limitations @@ -75,7 +75,7 @@ with_shared_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// use const_format::assertcp; @@ -92,13 +92,13 @@ with_shared_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions/assertcp_macros.rs:124:11 - /// | - /// 10 | assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R)); - /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' + /// --> const_format/src/macros/assertions/assertcp_macros.rs:116:11 + /// | + /// 9 | assertcp!(L.pow(R) == 64, "{L} to the {R} isn't 64, it's {}", L.pow(R)); + /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed. /// 2 to the 5 isn't 64, it's 32 - /// ', src/macros/assertions/assertcp_macros.rs:10:11 + /// ', const_format/src/macros/assertions/assertcp_macros.rs:9:11 /// /// ``` /// @@ -192,7 +192,7 @@ with_shared_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// use const_format::assertcp_eq; @@ -210,15 +210,15 @@ with_shared_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions/assertcp_macros.rs:226:14 + /// --> const_format/src/macros/assertions/assertcp_macros.rs:235:14 /// | - /// 11 | assertcp_eq!(size_of::(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large"); + /// 12 | assertcp_eq!(size_of::(), size_of::<[u16; 2]>(), "Whoops, `Type` is too large"); /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed: `(left == right)` /// left: `6` /// right: `4` /// Whoops, `Type` is too large - /// ', src/macros/assertions/assertcp_macros.rs:11:14 + /// ', const_format/src/macros/assertions/assertcp_macros.rs:12:14 /// /// ``` /// @@ -258,7 +258,7 @@ with_shared_docs! { /// ### Failing assertion /// /// This example demonstrates a failing assertion, - /// and how the compiler error looks like as of 2021-09-18. + /// and how the compiler error looks like as of 2023-10-14. /// /// ```compile_fail /// use const_format::assertcp_ne; @@ -272,15 +272,15 @@ with_shared_docs! { /// /// ```text /// error[E0080]: evaluation of constant value failed - /// --> src/macros/assertions/assertcp_macros.rs:298:14 + /// --> const_format/src/macros/assertions/assertcp_macros.rs:297:14 /// | - /// 7 | assertcp_ne!(NAME, "", "NAME must not be empty!"); + /// 8 | assertcp_ne!(NAME, "", "NAME must not be empty!"); /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at ' /// assertion failed: `(left != right)` /// left: `""` /// right: `""` /// NAME must not be empty! - /// ', src/macros/assertions/assertcp_macros.rs:7:14 + /// ', const_format/src/macros/assertions/assertcp_macros.rs:8:14 /// ``` /// #[cfg_attr(feature = "__docsrs", doc(cfg(feature = "assertcp")))]