Skip to content

Commit

Permalink
Merge pull request #2 from rodrimati1992/0_2
Browse files Browse the repository at this point in the history
0.2.0 release
  • Loading branch information
rodrimati1992 authored Nov 20, 2021
2 parents 98eecfe + 4a6a9e4 commit 0fb9d08
Show file tree
Hide file tree
Showing 45 changed files with 4,453 additions and 613 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ jobs:
rustup override set ${{ matrix.rust }}
cargo update
cd "${{github.workspace}}/const_panic_proc_macros/"
cargo test
cd "${{github.workspace}}/"
cargo build --no-default-features
cargo build
cargo test --features "test"
cargo test --no-default-features --features "test "
cargo test --no-default-features --features "test non_basic"
cargo test --no-default-features --features "test non_basic derive"
17 changes: 14 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "const_panic"
version = "0.1.1"
version = "0.2.0"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2021"
license = "Zlib"
Expand All @@ -17,15 +17,26 @@ include = [
"LICENSE-ZLIB.md",
]

[dependencies]
[workspace]

[dependencies.const_panic_proc_macros]
version = "=0.2.0"
path = "./const_panic_proc_macros/"
optional = true

[dev-dependencies.rand]
version = "0.8.4"
default_features = false
features = ["small_rng"]

[features]
default = ["non_basic"]
non_basic = []
docsrs = []
derive = ["const_panic_proc_macros"]

# private feature
test = []

[package.metadata.docs.rs]
features = ["non_basic", "docsrs"]
features = ["derive", "docsrs"]
33 changes: 33 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
This changelog is a summary of the changes made in each release.

# 0.2

### 0.2.0

Added `concat_assert` macro.

Added `PanicFmt` derive macro.

Added `"derive"` crate feature, to enable the `PanicFmt` derive.

Made breaking changes to `impl_panicfmt` to allow generic implementations with type and const parameters.

Added `NumberFmt` enum, for choosing how numbers are formatted.

Added `FmtArg::{BIN, ALT_BIN, HEX. ALT_HEX}` associated constants.

Added `FmtArg::{set_hex, set_bin}` methods.

Added `PanicVal::from_short_str` constructor.

Added support for binary and hexadecimal formatting in macros.

Added `PackedFmtArg` type (which requires the non_basic feature).

Added `const_panic::fmt::SHORT_STRING_CAP` constant with the capacity of a `ShortString`.


Changed `PanicVal` such that only strings can be left or right padded.

Removed the `PanicVal::{set_leftpad, set_rightpad}` methods.

Declared `const_panic_proc_macros` crate, depended by `const_panic` when the `"derive"` feature is enabled.

# 0.1

### 0.1.1
Expand Down
67 changes: 29 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@ All of the types that implement the [`PanicFmt`] trait can be formatted in panic

### Basic

```rust,compile_fail
use const_panic::concat_panic;
```compile_fail
use const_panic::concat_assert;
const FOO: u32 = 10;
const BAR: u32 = 0;
const _: () = assert_non_zero(FOO, BAR);
#[track_caller]
const fn assert_non_zero(foo: u32, bar: u32) {
if foo == 0 || bar == 0 {
concat_panic!("\nneither foo nor bar can be zero!\nfoo: ", foo, "\nbar: ", bar)
concat_assert!{
foo != 0 && bar != 0,
"\nneither foo nor bar can be zero!\nfoo: ", foo, "\nbar: ", bar
}
}
```
The above code fails to compile with this error:
```text
error[E0080]: evaluation of constant value failed
--> src/lib.rs:17:15
--> src/lib.rs:20:15
|
8 | const _: () = assert_non_zero(FOO, BAR);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
neither foo nor bar can be zero!
foo: 10
bar: 0', src/lib.rs:8:15
```

When called at runtime
```should_panic
use const_panic::concat_panic;
Expand Down Expand Up @@ -69,16 +71,18 @@ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Panic formatting for custom types can be done in these ways
(in increasing order of verbosity):
- Using the [`PanicFmt` derive] macro
(requires the opt-in `"derive"` feature)
- Using the [`impl_panicfmt`] macro
(requires the default-enabled `"non_basic"` feature)
- Using the [`flatten_panicvals`] macro
(requires the default-enabled `"non_basic"` feature)
- Manually implementing the [`PanicFmt`] trait as described in its docs.

This example uses the [`impl_panicfmt`] approach.
This example uses the [`PanicFmt` derive] approach.

```rust,compile_fail
use const_panic::concat_panic;
```compile_fail
use const_panic::{PanicFmt, concat_panic};
const LAST: u8 = {
Foo{
Expand Down Expand Up @@ -110,46 +114,23 @@ impl Foo<'_> {
}
}
#[derive(PanicFmt)]
struct Foo<'a> {
x: &'a [u8],
y: Bar,
z: Qux,
}
// You need to replace non-static lifetimes with `'_` here.
const_panic::impl_panicfmt! {
impl Foo<'_>;
struct Foo {
x: &[u8],
y: Bar,
z: Qux,
}
}
#[derive(PanicFmt)]
struct Bar(bool, bool);
const_panic::impl_panicfmt! {
impl Bar;
struct Bar(bool, bool);
}
#[derive(PanicFmt)]
enum Qux {
Up,
Down { x: u32, y: u32 },
Left(u64),
}
const_panic::impl_panicfmt!{
impl Qux;
enum Qux {
Up,
Down { x: u32, y: u32 },
Left(u64),
}
}
```
The above code fails to compile with this error:
```text
Expand All @@ -162,7 +143,7 @@ error[E0080]: evaluation of constant value failed
10 | | z: Qux::Left(23),
11 | | }.pop().1
| |___________^ the evaluated program panicked at '
expected a non-empty Foo, found:
expected a non-empty Foo, found:
Foo {
x: [],
y: Bar(
Expand All @@ -174,6 +155,7 @@ Foo {
),
}', src/lib.rs:11:7
```

# Limitations
Expand All @@ -184,6 +166,11 @@ because `const_panic` macros use duck typing to call methods on those arguments.
One effect of that limitation is that you will have to pass suffixed
integer literals (eg: `100u8`) when those integers aren't inferred to be a concrete type.

### Panic message length

The panic message can only be up to [`MAX_PANIC_MSG_LEN`] long,
after which it is truncated.

# Cargo features

- `"non_basic"`(enabled by default):
Expand All @@ -192,9 +179,12 @@ Enables support for formatting structs, enums, and arrays.
Without this feature, you can effectively only format primitive types
(custom types can manually implement formatting with more difficulty).

- `"derive"`(disabled by default):
Enables the [`PanicFmt` derive] macro.

# Plans

Adding a derive macro, under an opt-in "derive" feature.
None for now

# No-std support

Expand All @@ -204,7 +194,8 @@ Adding a derive macro, under an opt-in "derive" feature.

This requires Rust 1.57.0, because it uses the `panic` macro in a const context.


[`PanicFmt` derive]: https://docs.rs/const_panic/*/const_panic/derive.PanicFmt.html
[`PanicFmt`]: https://docs.rs/const_panic/*/const_panic/fmt/trait.PanicFmt.html
[`impl_panicfmt`]: https://docs.rs/const_panic/*/const_panic/macro.impl_panicfmt.html
[`flatten_panicvals`]: https://docs.rs/const_panic/*/const_panic/macro.flatten_panicvals.html
[`flatten_panicvals`]: https://docs.rs/const_panic/*/const_panic/macro.flatten_panicvals.html
[`MAX_PANIC_MSG_LEN`]: https://docs.rs/const_panic/*/const_panic/constant.MAX_PANIC_MSG_LEN.html
31 changes: 31 additions & 0 deletions const_panic_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "const_panic_proc_macros"
version = "0.2.0"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2021"
license = "Zlib"
description = "Implementation detail of the `const_panic` crate"
keywords = []
categories = []
repository = "https://github.com/rodrimati1992/const_panic/"
include = [
"Cargo.toml",
"src/**/*.rs",
"../README.md",
"LICENSE-ZLIB.md",
]

[badges]
travis-ci = { repository = "rodrimati1992/const_format_crates/" }

[lib]
proc-macro = true

[dependencies]
quote = "1.0.7"
proc-macro2 = "1.0.19"
unicode-xid = "0.2"

[dependencies.syn]
version = "1.0.38"

17 changes: 17 additions & 0 deletions const_panic_proc_macros/LICENSE-ZLIB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) 2021 Matias Rodriguez.

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Loading

0 comments on commit 0fb9d08

Please sign in to comment.