Skip to content

Commit

Permalink
More docs improvements
Browse files Browse the repository at this point in the history
Fixed `panic` invocation after 2018->2021 migration
  • Loading branch information
rodrimati1992 committed Oct 14, 2023
1 parent 88d792f commit cd1ebfc
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 96 deletions.
79 changes: 36 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`]:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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.<br>
The `"derive"` feature enables the [`ConstDebug`] macro,
and the `"fmt"` feature.<br>
[`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.<br>
The `"assertc"` feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros,
and the `"fmt"` feature.<br>
These macros are like the standard library assert macros, but evaluated at compile-time.


# Examples

### Concatenation of primitive types
Expand All @@ -116,16 +114,14 @@ 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

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)]
Expand All @@ -148,21 +144,19 @@ 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

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;
Expand All @@ -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:
Expand All @@ -200,8 +195,6 @@ You can't put pineapple on pizza, Bob
```



<div id="macro-limitations"></div>

# Limitations
Expand All @@ -220,6 +213,7 @@ so while a `Type::<u8>::FOO` argument would be fine,
So `#[doc = "foobar"]` cannot be replaced with `#[doc = concatcp!("foo", "bar") ]`.

<span id="integer-args"></span>

### Integer arguments

Integer arguments must have a type inferrable from context.
Expand All @@ -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
Expand All @@ -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.<br>
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.<br>
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.<br>
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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion const_format/src/const_debug_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion const_format/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//!
Expand Down
2 changes: 1 addition & 1 deletion const_format/src/for_assert_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion const_format/src/for_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
35 changes: 18 additions & 17 deletions const_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.<br>
//! The `"derive"` feature enables the [`ConstDebug`] macro,
//! and the `"fmt"` feature.<br>
//! [`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.<br>
//! The `"assertc"` feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros,
//! and the `"fmt"` feature.<br>
//! These macros are like the standard library assert macros, but evaluated at compile-time.
//!
//! # Examples
//!
//! ### Concatenation of primitive types
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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.<br>
//! 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.<br>
//! 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.<br>
//! 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
//!
Expand Down
Loading

0 comments on commit cd1ebfc

Please sign in to comment.