Skip to content

Commit 7a778d1

Browse files
Merge pull request #5 from rodrimati1992/assertions
0.2.5 release!
2 parents 80baf3d + 6329127 commit 7a778d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2680
-405
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ matrix:
2424

2525
- cargo test --features "testing"
2626
- cargo test --features "testing fmt"
27+
- cargo test --features "testing assert"
2728
- cargo test --features "testing derive"
2829
- cargo test --features "testing constant_time_as_str"
2930
- cargo test --features "testing derive constant_time_as_str"
31+
- cargo test --features "testing derive constant_time_as_str assert"
3032

3133
- MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)
3234
- echo "Installing latest nightly with Miri"
@@ -41,9 +43,11 @@ matrix:
4143

4244
- cargo miri test --features "testing"
4345
- cargo miri test --features "testing fmt"
46+
- cargo miri test --features "testing assert"
4447
- cargo miri test --features "testing derive"
4548
- cargo miri test --features "testing constant_time_as_str"
4649
- cargo miri test --features "testing derive constant_time_as_str"
50+
- cargo miri test --features "testing derive constant_time_as_str assert"
4751

4852

4953
script:

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
members=[
33
"const_format",
44
"const_format_proc_macros",
5+
"print_errors",
56
]

Changelog.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ This is the changelog,summarising changes in each version(some minor changes may
22

33
# 0.2
44

5+
### 0.2.5
6+
7+
Added the "assert" cargo feature,
8+
defining the `assertc`/`assertc_eq`/`assertc_ne` macros for
9+
compile-time assertions with formatting.
10+
11+
Added custom formatting support in the `const_format::fmt`-based formatting macros,
12+
by prefixing any argument with `|identifier|`,
13+
accessing a `Formatter` to format that argument however one wants.
14+
15+
Added `concatc` macro for concatenating std/user-defined types into a `&'static str` constant.
16+
17+
Added `const_format::Result` alias for `std::result::Result<(), const_format::Error>`.
18+
19+
Added `const_format::fmt::ToResult` type for converting
20+
`()` and `const_format::Result` to `const_format::Result`.
21+
22+
Added `Pwrapper::const_eq` methods for comparing many std types in
23+
the `assertc_eq`/`assertc_ne` macros.
24+
25+
Added `Pwrapper::const_display_fmt` methods for `NonZero*` types.
26+
27+
Added support for passing `concat!(....)` as the format string.
28+
29+
### 0.2.0
30+
531
Every single new item added requires Rust nightly to use, with at least the "fmt" cargo feature enabled.
632

733
Defined a `core::fmt`-like API with these these types:

README.md

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@ which have not been stabilized as of writing these docs.
3434

3535
All the other features of this crate are implemented on top of the [`const_format::fmt`] API:
3636

37+
- [`concatc`]:
38+
Concatenates many standard library and user defined types into a `&'static str` constant.
39+
3740
- [`formatc`]:
38-
[`format`]-like macro that can format many standard library and user defined types.
41+
[`format`]-like macro that can format many standard library and user defined types into
42+
a `&'static str` constant.
3943

4044
- [`writec`]:
4145
[`write`]-like macro that can format many standard library and user defined types
4246
into a type that implements [`WriteMarker`].
4347

44-
4548
The "derive" feature enables the [`ConstDebug`] macro, and the "fmt" feature.<br>
4649
[`ConstDebug`] derives the [`FormatMarker`] trait,
4750
and implements an inherent `const_debug_fmt` method for compile-time debug formatting.
4851

52+
The "assert" feature enables the [`assertc`], [`assertc_eq`], [`assertc_ne`] macros,
53+
and the "fmt" feature.<br>
54+
These macros are like the standard library assert macros, but evaluated at compile-time.
4955

5056
# Examples
5157

@@ -116,102 +122,90 @@ assert_eq!(
116122
```
117123

118124

125+
119126
### Formatted const panics
120127

121-
This example demonstrates how you can use a [`StrWriter`] to format
122-
a compile-time panic message.
128+
This example demonstrates how you can use the [`assertc_ne`] macro to
129+
do compile-time inequality assertions with formatted error messages.
123130

124-
As of writing these docs (2020-08-29), panicking at compile-time requires a
125-
nightly feature, and only supports passing a `&'static str` argument,
126-
so this only works in the initialization block of `const` items.
131+
This requires the "assert" feature,because as of writing these docs (2020-09-XX),
132+
panicking at compile-time requires a nightly feature.
127133

128134
```rust
129-
#![feature(const_mut_refs)]
130-
#![feature(const_panic)]
135+
#![feature(const_mut_refs)]
131136

132-
use const_format::{StrWriter, strwriter_as_str, writec};
137+
use const_format::{StrWriter, assertc_ne, strwriter_as_str, writec};
133138
use const_format::utils::str_eq;
134139

135-
struct PizzaError;
136-
137-
const fn write_message(
138-
buffer: &mut StrWriter,
139-
bought_by: &str,
140-
topping: &str,
141-
) -> Result<(), PizzaError> {
142-
buffer.clear();
143-
let mut writer = buffer.as_mut();
144-
if str_eq(topping, "pineapple") {
145-
let _ = writec!(
146-
writer,
147-
"\n{SEP}\n\nYou can't put pineapple on pizza, {}.\n\n{SEP}\n",
148-
bought_by,
149-
SEP = "----------------------------------------------------------------"
140+
macro_rules! check_valid_pizza{
141+
($user:expr, $topping:expr) => {
142+
assertc_ne!(
143+
$topping,
144+
"pineapple",
145+
"You can't put pineapple on pizza, {}",
146+
$user,
150147
);
151-
return Err(PizzaError);
152148
}
153-
Ok(())
154149
}
155150

156-
const CAP: usize = 256;
157-
// Defined a `const fn` as a workaround for mutable references not
158-
// being allowed in `const`ants.
159-
const fn message_and_result(
160-
bought_by: &str,
161-
topping: &str,
162-
) -> (StrWriter<[u8; CAP]>, Result<(), PizzaError>) {
163-
let mut buffer = StrWriter::new([0; CAP]);
164-
let res = write_message(&mut buffer, bought_by, topping);
165-
(buffer, res)
166-
}
167-
168-
const _: () = {
169-
if let (buffer, Err(_)) = message_and_result("Bob", "pineapple") {
170-
let promoted: &'static StrWriter = &{buffer};
171-
let message = strwriter_as_str!(promoted);
172-
panic!(message);
173-
}
174-
};
151+
check_valid_pizza!("John", "salami");
152+
check_valid_pizza!("Dave", "sausage");
153+
check_valid_pizza!("Bob", "pineapple");
175154

155+
# fn main(){}
176156
```
177157

178-
This is what it prints in rust nightly :
158+
This is the compiler output,
159+
the first compilation error is there to have an indicator of what assertion failed,
160+
and the second is the assertion failure:
179161

180-
```
162+
```text
181163
error: any use of this value will cause an error
182-
--> src/lib.rs:166:9
164+
--> src/lib.rs:140:1
183165
|
184-
43 | / const _: () = {
185-
44 | | if let (buffer, Err(_)) = message_and_result("Bob", "pineapple") {
186-
45 | | let promoted: &'static StrWriter = &{buffer};
187-
46 | | let message = strwriter_as_str!(promoted);
188-
47 | | panic!(message);
189-
| | ^^^^^^^^^^^^^^^^ the evaluated program panicked at '
190-
----------------------------------------------------------------
191-
192-
You can't put pineapple on pizza, Bob.
193-
194-
----------------------------------------------------------------
195-
', src/lib.rs:47:9
196-
48 | | }
197-
49 | | };
198-
| |__-
166+
22 | check_valid_pizza!("Bob", "pineapple");
167+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
199168
|
200169
= note: `#[deny(const_err)]` on by default
201170
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
202171
172+
error[E0080]: could not evaluate constant
173+
--> /const_format/src/panicking.rs:32:5
174+
|
175+
32 | .
176+
| ^ the evaluated program panicked at '
177+
--------------------------------------------------------------------------------
178+
module_path: rust_out
179+
line: 22
180+
181+
assertion failed: LEFT != RIGHT
182+
183+
left: "pineapple"
184+
right: "pineapple"
185+
186+
You can't put pineapple on pizza, Bob
187+
--------------------------------------------------------------------------------
188+
', /const_format/src/panicking.rs:31:1
189+
|
190+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
191+
192+
error: aborting due to 2 previous errors
193+
203194
```
204195

205196

197+
198+
206199
<div id="macro-limitations"></div>
207200

208201
# Limitations
209202

210203
All of the macros from `const_format` have these limitations:
211204

212-
- The formatting macros that expand to
213-
`&'static str`s can only use constants of concrete types,
214-
so while `Type::<u8>::FOO` is fine,`Type::<T>::FOO` is not (`T` being a type parameter).
205+
- The formatting macros that expand to
206+
`&'static str`s can only use constants from concrete types,
207+
so while a `Type::<u8>::FOO` argument would be fine,
208+
`Type::<T>::FOO` would not be (`T` being a type parameter).
215209

216210
- Integer arguments must have a type inferrable from context,
217211
[more details in the Integer arguments section](#integer-args).
@@ -253,14 +247,26 @@ This feature includes the `formatc`/`writec` formatting macros.
253247
provides the `ConstDebug` derive macro to format user-defined types at compile-time.<br>
254248
This implicitly uses the `syn` crate, so clean compiles take a bit longer than without the feature.
255249

250+
- "assert": implies the "fmt" feature,
251+
enables the assertion macros.<br>
252+
This is a separate cargo feature because:
253+
- It uses nightly Rust features that are less stable than the "fmt" feature does.<br>
254+
- It requires the `std` crate, because `core::panic` requires a string literal argument.
255+
256+
256257
- "constant_time_as_str": implies the "fmt" feature.
257258
An optimization that requires a few additional nightly features,
258259
allowing the `as_bytes_alt` methods and `slice_up_to_len_alt` methods to run
259260
in constant time, rather than linear time proportional to the truncated part of the slice.
260261

262+
263+
261264
# No-std support
262265

263-
`const_format` is unconditionally `#![no_std]`, it can be used anywhere Rust can be used.
266+
`const_format` is `#![no_std]`, it can be used anywhere Rust can be used.
267+
268+
Caveat: The opt-in "assert" feature uses the `std::panic` macro to panic,
269+
as of 2020-09-06 `core::panic` requires the argument to be a literal.
264270

265271
# Minimum Supported Rust Version
266272

@@ -270,6 +276,12 @@ Features that require newer versions of Rust, or the nightly compiler,
270276
need to be explicitly enabled with cargo features.
271277

272278

279+
[`assertc`]: https://docs.rs/const_format/0.2.*/const_format/macro.assertc.html
280+
281+
[`assertc_eq`]: https://docs.rs/const_format/0.2.*/const_format/macro.assertc_eq.html
282+
283+
[`assertc_ne`]: https://docs.rs/const_format/0.2.*/const_format/macro.assertc_ne.html
284+
273285
[`concatcp`]: https://docs.rs/const_format/0.2.*/const_format/macro.concatcp.html
274286

275287
[`formatcp`]: https://docs.rs/const_format/0.2.*/const_format/macro.formatcp.html
@@ -280,6 +292,8 @@ need to be explicitly enabled with cargo features.
280292

281293
[`const_format::fmt`]: https://docs.rs/const_format/0.2.*/const_format/fmt/index.html
282294

295+
[`concatc`]: https://docs.rs/const_format/0.2.*/const_format/macro.concatc.html
296+
283297
[`formatc`]: https://docs.rs/const_format/0.2.*/const_format/macro.formatc.html
284298

285299
[`writec`]: https://docs.rs/const_format/0.2.*/const_format/macro.writec.html

const_format/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const_format"
3-
version = "0.2.4"
3+
version = "0.2.5"
44
authors = ["rodrimati1992 <rodrimatt1985@gmail.com>"]
55
edition = "2018"
66
license = "Zlib"
@@ -24,16 +24,17 @@ travis-ci = { repository = "rodrimati1992/const_format_crates/" }
2424
default = []
2525
fmt = []
2626
derive = ["fmt", "const_format_proc_macros/derive"]
27+
assert = ["fmt"]
2728
constant_time_as_str = ["fmt"]
2829

2930
# "private" features
3031
debug = ["const_format_proc_macros/debug"]
3132
testing = []
3233
only_new_tests = ["testing"]
33-
all = ["fmt", "derive", "constant_time_as_str"]
34+
all = ["fmt", "derive", "constant_time_as_str", "assert"]
3435

3536
[dependencies.const_format_proc_macros]
36-
version = "=0.2.0"
37+
version = "=0.2.5"
3738
path = "../const_format_proc_macros"
3839

3940
[dev-dependencies]

0 commit comments

Comments
 (0)